From: Mike Bayer Date: Sat, 27 Mar 2021 12:36:32 +0000 (-0400) Subject: Commentary; run criteria.params() if statement isn't cached? X-Git-Tag: rel_1_4_4~13^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b619fbcbb277dd168c223be6b3ff960355f36b0c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 --- 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)