]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
cache key share; support correct traverse of 'of'
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 27 Jun 2024 22:17:47 +0000 (18:17 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 27 Jun 2024 22:24:32 +0000 (18:24 -0400)
Fixed caching issue where the
:paramref:`_sql.Select.with_for_update.key_share` element of
:meth:`_sql.Select.with_for_update` was not considered as part of the cache
key, leading to incorrect caching if different variations of this parameter
were used with an otherwise identical statement.

Also repairs a traversal issue where the ``of`` element of
``ForUpdateArg`` when set to ``None`` cannot be compared against a
non-None element because the traversal defines it as a clauselist.
Traversal in this case is adjusted to accommodate for this case so that
we dont need to create a risky-to-backport change to ``ForUpdateArg``
itself.

Fixes: #11544
Change-Id: Ie8a50716df06977af58b0c22a8c10e1b64d972b9
(cherry picked from commit 6d2f43e14f2fe25cdc811355b7bd6d11f8eee381)

lib/sqlalchemy/sql/selectable.py
lib/sqlalchemy/sql/traversals.py
test/sql/test_compare.py

index a178458480b48474b3944b9ca1d86e8f539414f9..aae1e47e0909c2af3c112085e1aca9e3ed9aad6f 100644 (file)
@@ -3081,6 +3081,7 @@ class ForUpdateArg(ClauseElement):
         ("nowait", InternalTraversal.dp_boolean),
         ("read", InternalTraversal.dp_boolean),
         ("skip_locked", InternalTraversal.dp_boolean),
+        ("key_share", InternalTraversal.dp_boolean),
     ]
 
     of: Optional[Sequence[ClauseElement]]
index 3ca3caf9e2c728ac8b2947afd7f13a4bed99c9fb..8bb2939cb31d2d4f8cd6407fd4c21f95df2006e7 100644 (file)
@@ -562,6 +562,8 @@ class TraversalComparatorStrategy(HasTraversalDispatch, util.MemoizedSlots):
                         return False
                     else:
                         continue
+                elif right_child is None:
+                    return False
 
                 comparison = dispatch(
                     left_attrname, left, left_child, right, right_child, **kw
index c1f6e7f11368800ce62130ac4af81c0eb0e9af84..2a7e41387bf0ca34347cb42748c0756f5e336439 100644 (file)
@@ -478,6 +478,21 @@ class CoreFixtures:
             select(table_a.c.a)
             .where(table_a.c.b == 5)
             .with_for_update(nowait=True),
+            select(table_a.c.a)
+            .where(table_a.c.b == 5)
+            .with_for_update(nowait=True, skip_locked=True),
+            select(table_a.c.a)
+            .where(table_a.c.b == 5)
+            .with_for_update(nowait=True, read=True),
+            select(table_a.c.a)
+            .where(table_a.c.b == 5)
+            .with_for_update(of=table_a.c.a),
+            select(table_a.c.a)
+            .where(table_a.c.b == 5)
+            .with_for_update(of=table_a.c.b),
+            select(table_a.c.a)
+            .where(table_a.c.b == 5)
+            .with_for_update(nowait=True, key_share=True),
             select(table_a.c.a).where(table_a.c.b == 5).correlate(table_b),
             select(table_a.c.a)
             .where(table_a.c.b == 5)