From b619fbcbb277dd168c223be6b3ff960355f36b0c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 27 Mar 2021 08:36:32 -0400 Subject: [PATCH] Commentary; run criteria.params() if statement isn't cached? Considering adjustment to 56f9c7743e9083add69a10501a503f, if statement is not cached, skip the relatively expensive step of re-processing the criteria clause. However, this causes the overall cache key of the statement to come out differently which should also be avoided. Likely we would not merge the actual change, just the comment here. References: #6139 Change-Id: Idb555b78d8d7950d084315e004448f64cf59bb5c --- lib/sqlalchemy/orm/strategy_options.py | 13 +++++++++++++ test/orm/test_relationship_criteria.py | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py index 8fa79bfdb5..4827b37524 100644 --- a/lib/sqlalchemy/orm/strategy_options.py +++ b/lib/sqlalchemy/orm/strategy_options.py @@ -134,6 +134,19 @@ class Load(Generative, LoaderOption): orig_query = context.compile_state.select_statement current_query = context.query + # NOTE: while it seems like we should not do the "apply" operation + # here if orig_query is current_query, skipping it in the "optimized" + # case causes the query to be different from a cache key perspective, + # because we are creating a copy of the criteria which is no longer + # the same identity of the _extra_criteria in the loader option + # itself. cache key logic produces a different key for + # (A, copy_of_A) vs. (A, A), because in the latter case it shortens + # the second part of the key to just indicate on identity. + + # if orig_query is current_query: + # not cached yet. just do the and_() + # return and_(*self._extra_criteria) + k1 = orig_query._generate_cache_key() k2 = current_query._generate_cache_key() diff --git a/test/orm/test_relationship_criteria.py b/test/orm/test_relationship_criteria.py index d93c63fb7e..04afe34779 100644 --- a/test/orm/test_relationship_criteria.py +++ b/test/orm/test_relationship_criteria.py @@ -1043,7 +1043,12 @@ class RelationshipCriteriaTest(_Fixtures, testing.AssertsCompiledSQL): result = s.execute(stmt) return result - for value in "ed@wood.com", "ed@lala.com": + for value in ( + "ed@wood.com", + "ed@lala.com", + "ed@wood.com", + "ed@lala.com", + ): s.close() with self.sql_execution_asserter() as asserter: result = go(value) -- 2.47.2