From 6930dfc032c3f9f474e71ab4e021c0ef8384930e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 27 Apr 2020 12:58:12 -0400 Subject: Convert execution to move through Session This patch replaces the ORM execution flow with a single pathway through Session.execute() for all queries, including Core and ORM. Currently included is full support for ORM Query, Query.from_statement(), select(), as well as the baked query and horizontal shard systems. Initial changes have also been made to the dogpile caching example, which like baked query makes use of a new ORM-specific execution hook that replaces the use of both QueryEvents.before_compile() as well as Query._execute_and_instances() as the central ORM interception hooks. select() and Query() constructs alike can be passed to Session.execute() where they will return ORM results in a Results object. This API is currently used internally by Query. Full support for Session.execute()->results to behave in a fully 2.0 fashion will be in later changesets. bulk update/delete with ORM support will also be delivered via the update() and delete() constructs, however these have not yet been adapted to the new system and may follow in a subsequent update. Performance is also beginning to lag as of this commit and some previous ones. It is hoped that a few central functions such as the coercions functions can be rewritten in C to re-gain performance. Additionally, query caching is now available and some subsequent patches will attempt to cache more of the per-execution work from the ORM layer, e.g. column getters and adapters. This patch also contains initial "turn on" of the caching system enginewide via the query_cache_size parameter to create_engine(). Still defaulting at zero for "no caching". The caching system still needs adjustments in order to gain adequate performance. Change-Id: I047a7ebb26aa85dc01f6789fac2bff561dcd555d --- lib/sqlalchemy/sql/selectable.py | 64 ++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 22 deletions(-) (limited to 'lib/sqlalchemy/sql/selectable.py') diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 6a552c18c..008959aec 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1342,7 +1342,9 @@ class AliasedReturnsRows(NoInit, FromClause): raise NotImplementedError() def _init(self, selectable, name=None): - self.element = selectable + self.element = coercions.expect( + roles.ReturnsRowsRole, selectable, apply_propagate_attrs=self + ) self.supports_execution = selectable.supports_execution if self.supports_execution: self._execution_options = selectable._execution_options @@ -3026,6 +3028,7 @@ class GenerativeSelect(DeprecatedSelectBaseGenerations, SelectBase): ) +@CompileState.plugin_for("default", "compound_select") class CompoundSelectState(CompileState): @util.memoized_property def _label_resolve_dict(self): @@ -3058,7 +3061,6 @@ class CompoundSelect(HasCompileState, GenerativeSelect): """ __visit_name__ = "compound_select" - _compile_state_factory = CompoundSelectState._create _traverse_internals = [ ("selects", InternalTraversal.dp_clauseelement_list), @@ -3425,6 +3427,7 @@ class DeprecatedSelectGenerations(object): self.select_from.non_generative(self, fromclause) +@CompileState.plugin_for("default", "select") class SelectState(CompileState): class default_select_compile_options(CacheableOptions): _cache_key_traversal = [] @@ -3462,7 +3465,7 @@ class SelectState(CompileState): ) if not seen.intersection(item._cloned_set): froms.append(item) - seen.update(item._cloned_set) + seen.update(item._cloned_set) return froms @@ -3714,12 +3717,29 @@ class SelectState(CompileState): return replace_from_obj_index +class _SelectFromElements(object): + def _iterate_from_elements(self): + # note this does not include elements + # in _setup_joins or _legacy_setup_joins + + return itertools.chain( + itertools.chain.from_iterable( + [element._from_objects for element in self._raw_columns] + ), + itertools.chain.from_iterable( + [element._from_objects for element in self._where_criteria] + ), + self._from_obj, + ) + + class Select( HasPrefixes, HasSuffixes, HasHints, HasCompileState, DeprecatedSelectGenerations, + _SelectFromElements, GenerativeSelect, ): """Represents a ``SELECT`` statement. @@ -3728,7 +3748,6 @@ class Select( __visit_name__ = "select" - _compile_state_factory = SelectState._create _is_future = False _setup_joins = () _legacy_setup_joins = () @@ -4047,7 +4066,7 @@ class Select( if cols_present: self._raw_columns = [ coercions.expect( - roles.ColumnsClauseRole, c, apply_plugins=self + roles.ColumnsClauseRole, c, apply_propagate_attrs=self ) for c in columns ] @@ -4073,17 +4092,6 @@ class Select( cols = list(elem._select_iterable) return cols[0].type - def _iterate_from_elements(self): - return itertools.chain( - itertools.chain.from_iterable( - [element._from_objects for element in self._raw_columns] - ), - itertools.chain.from_iterable( - [element._from_objects for element in self._where_criteria] - ), - self._from_obj, - ) - @property def froms(self): """Return the displayed list of FromClause elements.""" @@ -4192,14 +4200,16 @@ class Select( self._raw_columns = self._raw_columns + [ coercions.expect( - roles.ColumnsClauseRole, column, apply_plugins=self + roles.ColumnsClauseRole, column, apply_propagate_attrs=self ) for column in columns ] def _set_entities(self, entities): self._raw_columns = [ - coercions.expect(roles.ColumnsClauseRole, ent, apply_plugins=self) + coercions.expect( + roles.ColumnsClauseRole, ent, apply_propagate_attrs=self + ) for ent in util.to_list(entities) ] @@ -4342,14 +4352,24 @@ class Select( self._raw_columns = rc @property - def _whereclause(self): - """Legacy, return the WHERE clause as a """ - """:class:`_expression.BooleanClauseList`""" + def whereclause(self): + """Return the completed WHERE clause for this :class:`.Select` + statement. + + This assembles the current collection of WHERE criteria + into a single :class:`_expression.BooleanClauseList` construct. + + + .. versionadded:: 1.4 + + """ return BooleanClauseList._construct_for_whereclause( self._where_criteria ) + _whereclause = whereclause + @_generative def where(self, whereclause): """return a new select() construct with the given expression added to @@ -4430,7 +4450,7 @@ class Select( self._from_obj += tuple( coercions.expect( - roles.FromClauseRole, fromclause, apply_plugins=self + roles.FromClauseRole, fromclause, apply_propagate_attrs=self ) for fromclause in froms ) -- cgit v1.2.1