diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-11-10 17:01:58 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-11-11 15:25:35 -0500 |
| commit | 1d8833a9c1ada64cfc716109a8836b32cb8a9bd6 (patch) | |
| tree | ae5d2f6adfb578c43603b0141c27fad520c70e4f /lib/sqlalchemy/sql/elements.py | |
| parent | e3a8d198917f4246365e09fa975d55c64082cd2e (diff) | |
| download | sqlalchemy-1d8833a9c1ada64cfc716109a8836b32cb8a9bd6.tar.gz | |
ensure anon_map is passed for most annotated traversals
We can cache the annotated cache key for Table, but
for selectables it's not safe, as it fails to pass the
anon_map along and creates many redudant structures in
observed test scenario. It is likely safe for a
Column that's mapped to a Table also, however this is
not implemented here. Will have to see if that part
needs adjusting.
Fixed critical memory issue identified in cache key generation, where for
very large and complex ORM statements that make use of lots of ORM aliases
with subqueries, cache key generation could produce excessively large keys
that were orders of magnitude bigger than the statement itself. Much thanks
to Rollo Konig Brock for their very patient, long term help in finally
identifying this issue.
Also within TypeEngine objects, when we generate elements
for instance variables, skip the None elements at least.
this also saves on tuple complexity.
Fixes: #8790
Change-Id: I448ddbfb45ae0a648815be8dad4faad7d1977427
Diffstat (limited to 'lib/sqlalchemy/sql/elements.py')
| -rw-r--r-- | lib/sqlalchemy/sql/elements.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 3b70e8d4e..6a5aa7db9 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -334,6 +334,7 @@ class ClauseElement( _is_column_element = False _is_keyed_column_element = False _is_table = False + _gen_static_annotations_cache_key = False _is_textual = False _is_from_clause = False _is_returns_rows = False @@ -3224,7 +3225,7 @@ class Cast(WrapsColumnExpression[_T]): _traverse_internals: _TraverseInternalsType = [ ("clause", InternalTraversal.dp_clauseelement), - ("typeclause", InternalTraversal.dp_clauseelement), + ("type", InternalTraversal.dp_type), ] clause: ColumnElement[Any] @@ -3631,7 +3632,20 @@ class BinaryExpression(OperatorExpression[_T]): ( "type", InternalTraversal.dp_type, - ), # affects JSON CAST operators + ), + ] + + _cache_key_traversal = [ + ("left", InternalTraversal.dp_clauseelement), + ("right", InternalTraversal.dp_clauseelement), + ("operator", InternalTraversal.dp_operator), + ("modifiers", InternalTraversal.dp_plain_dict), + # "type" affects JSON CAST operators, so while redundant in most cases, + # is needed for that one + ( + "type", + InternalTraversal.dp_type, + ), ] _is_implicitly_boolean = True @@ -3816,6 +3830,10 @@ class Grouping(GroupedElement, ColumnElement[_T]): ("type", InternalTraversal.dp_type), ] + _cache_key_traversal = [ + ("element", InternalTraversal.dp_clauseelement), + ] + element: Union[TextClause, ClauseList, ColumnElement[_T]] def __init__( @@ -4322,6 +4340,11 @@ class Label(roles.LabeledColumnExprRole[_T], NamedColumn[_T]): ("_element", InternalTraversal.dp_clauseelement), ] + _cache_key_traversal = [ + ("name", InternalTraversal.dp_anon_name), + ("_element", InternalTraversal.dp_clauseelement), + ] + _element: ColumnElement[_T] name: str |
