]> 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:23:49 +0000 (18:23 -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

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

index 1e06754e6f2f54a2c5f6950dca1ce02181fb8c4e..a9ef7fd0301b71ca29345bcc779b66144dc20825 100644 (file)
@@ -3086,6 +3086,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 d8947ab67b73fb6d9fbb8da9eb7d5cdf039ccab7..a43ea70e1091351378e0d753749c165951a2f253 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)