Commit Graph

1542 Commits

Author SHA1 Message Date
Mike Bayer 7dc411dc63 Render table hints in generic SQL
Added :meth:`.Select.with_hint` output to the generic SQL string that is
produced when calling ``str()`` on a statement.  Previously, this clause
would be omitted under the assumption that it was dialect specific.
The hint text is presented within brackets to indicate the rendering
of such hints varies among backends.

Fixes: #5353
References: #4667
Change-Id: I01d97d6baa993e495519036ec7ecd5ae62856c16
2020-05-27 10:21:08 -04:00
Mike Bayer 6930dfc032 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
2020-05-25 13:56:37 -04:00
Mike Bayer dce8c7a125 Unify Query and select() , move all processing to compile phase
Convert Query to do virtually all compile state computation
in the _compile_context() phase, and organize it all
such that a plain select() construct may also be used as the
source of information in order to generate ORM query state.
This makes it such that Query is not needed except for
its additional methods like from_self() which are all to
be deprecated.

The construction of ORM state will occur beyond the
caching boundary when the new execution model is integrated.

future select() gains a working join() and filter_by() method.
as we continue to rebase and merge each commit in the steps,
callcounts continue to bump around.  will have to look at
the final result when it's all in.

References: #5159
References: #4705
References: #4639
References: #4871
References: #5010

Change-Id: I19e05b3424b07114cce6c439b05198ac47f7ac10
2020-05-24 11:54:08 -04:00
mike bayer 906bb6533d Merge "Correctly apply self_group in type_coerce element." 2020-05-23 21:21:39 +00:00
Mike Bayer fcbd03e48a Add immutabledict C code
Start trying to convert fundamental objects to
C as we now rely on a fairly small core of things,
and 1.4 is having problems with complexity added being
slower than the performance gains we are trying to build in.

immutabledict here does seem to bench as twice as fast as the
Python one, see below.  However, it does not appear to be
used prominently enough to make any dent in the performance
tests.

at the very least it may provide us some more lift-and-copy
code for more C extensions.

import timeit

from sqlalchemy.util._collections import not_immutabledict, immutabledict

def run(dict_cls):
    for i in range(1000000):
        d1 = dict_cls({"x": 5, "y": 4})

        d2 = d1.union({"x": 17, "new key": "some other value"}, None)

        assert list(d2) == ["x", "y", "new key"]

print(
    timeit.timeit(
        "run(d)", "from __main__ import run, not_immutabledict as d", number=1
    )
)
print(
    timeit.timeit(
        "run(d)", "from __main__ import run, immutabledict as d", number=1
    )
)

output:

python: 1.8799766399897635
C code: 0.8880784640205093

Change-Id: I29e7104dc21dcc7cdf895bf274003af2e219bf6d
2020-05-23 00:05:13 -04:00
Federico Caselli d163088de1 Correctly apply self_group in type_coerce element.
The type coerce element did not correctly apply grouping rules when using
in an expression

Fixes: #5344
Change-Id: Id67b0e60ac54f8992f931aaed62731672f60c96c
2020-05-22 23:56:50 +02:00
Mike Bayer 4550983e0c Performance fixes for new result set
A few small mistakes led to huge callcounts.   Additionally,
the warn-on-get behavior which is attempting to warn for
deprecated access in SQLAlchemy 2.0 is very expensive; it's not clear
if its feasible to have this warning or to somehow alter how it
works.

Fixes: #5340
Change-Id: I73bdd2d7b6f1b25cc0222accabd585cf761a5af4
2020-05-21 14:16:03 -04:00
Mike Bayer af0e59ff16 Disable "check unicode returns" under Python 3
Disabled the "unicode returns" check that runs on dialect startup when
running under Python 3, which for many years has occurred in order to test
the current DBAPI's behavior for whether or not it returns Python Unicode
or Py2K strings for the VARCHAR and NVARCHAR datatypes.  The check still
occurs by default under Python 2, however the mechanism to test the
behavior will be removed in SQLAlchemy 2.0 when Python 2 support is also
removed.

This logic was very effective when it was needed, however now that Python 3
is standard, all DBAPIs are expected to return Python 3 strings for
character datatypes.  In the unlikely case that a third party DBAPI does
not support this, the conversion logic within :class:`.String` is still
available and the third party dialect may specify this in its upfront
dialect flags by setting the dialect level flag ``returns_unicode_strings``
to one of :attr:`.String.RETURNS_CONDITIONAL` or
:attr:`.String.RETURNS_BYTES`, both of which will enable Unicode conversion
even under Python 3.

As part of this change, disabling testing of the doctest tutorials under
Python 2.

Fixes: #5315
Change-Id: I1260e894611409d3b7fe1a92bd90c52043bbcf19
2020-05-19 13:53:39 -04:00
Mike Bayer 53af60b353 Streamline visitors.iterate
This method might be used more significantly in the
ORM refactor, so further refine it.

* all get_children() methods now work entirely based on iterators.
  Basically only select() was sensitive to this anymore and it now
  chains the iterators together

* remove all kinds of flags like column_collections, schema_visitor
  that apparently aren't used anymore.

* remove the "depthfirst" visitors as these don't seem to be
  used either.

* make sure select() yields its columns first as these will be used
  to determine the current mapper.

Change-Id: I05273a2d5306a57c2d1b0979050748cf3ac964bf
2020-05-18 16:21:54 -04:00
mike bayer d4182d4da9 Merge "Add 'schema' parameter to table" 2020-05-15 17:11:20 +00:00
Dylan Modesitt 187a3a27cf Add 'schema' parameter to table
Added a "schema" parameter to the :func:`_expression.table` construct,
allowing ad-hoc table expressions to also include a schema name.
Pull request courtesy Dylan Modesitt.

Fixes: #5309
Closes: #5310
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5310
Pull-request-sha: ce85681050
Change-Id: I32015d593e1ee1121c7426fbffdcc565d025fad1
2020-05-10 11:59:19 +02:00
Mike Bayer 8782469b78 Warn when sorted_tables is not actually sorting
A warning is emitted when making use of the :attr:`.MetaData.sorted_tables`
attribute as well as the :func:`_schema.sort_tables` function, and the
given tables cannot be correctly sorted due to a cyclic dependency between
foreign key constraints. In this case, the functions will no longer sort
the involved tables by foreign key, and a warning will be emitted. Other
tables that are not part of the cycle will still be returned in dependency
order. Previously, the sorted_table routines would return a collection that
would unconditionally omit all foreign keys when a cycle was detected, and
no warning was emitted.

Fixes: #5316
Change-Id: I14f72ccf39cb568bc77e8da16d0685718b2b9960
2020-05-07 19:56:32 -04:00
mike bayer 3c16173720 Merge "Handle non-string, non column expression in index for deferred attach" 2020-05-02 03:33:18 +00:00
Mike Bayer f247bb2000 Documentation updates for ResultProxy -> Result
This is based off of
I8091919d45421e3f53029b8660427f844fee0228 and includes
all documentation-only changes as a separate merge,
once the parent is merged.

Change-Id: I711adea23df0f9f0b1fe7c76210bd2de6d31842d
2020-05-01 22:37:59 -04:00
Mike Bayer 45a334e38c Handle non-string, non column expression in index for deferred attach
Fixed issue where an :class:`.Index` that is deferred in being associated
with a table, such as as when it contains a :class:`.Column` that is not
associated with any :class:`.Table` yet,  would fail to attach correctly if
it also contained a non table-oriented expession.

Fixes: #5298
Change-Id: I0111c400f6bd4a9f31bf00a9957816c7a3fac783
2020-05-01 20:58:42 -04:00
Mike Bayer aded39f68c Propose Result as immediate replacement for ResultProxy
As progress is made on the _future.Result, including breaking
it out such that DBAPI behaviors are local to specific
implementations, it becomes apparent that the Result object
is a functional superset of ResultProxy and that basic
operations like fetchone(), fetchall(), and fetchmany()
behave pretty much exactly the same way on the new object.
Reorganize things so that ResultProxy is now referred to
as LegacyCursorResult, which subclasses CursorResult
that represents the DBAPI-cursor version of Result,
making use of a multiple inheritance pattern so that
the functionality of Result is also available in non-DBAPI
contexts, as will be necessary for some ORM
patterns.

Additionally propose the composition system for Result
that will form the basis for ORM-alternative result
systems such as horizontal sharding and dogpile cache.
As ORM results will soon be coming directly from
instances of Result, these extensions will instead
build their own ResultFetchStrategies that perform
the special steps to create composed or cached
result sets.

Also considering at the moment not emitting deprecation
warnings for fetchXYZ() methods; the immediate issue
is Keystone tests are calling upon it, but as the
implementations here are proving to be not in any
kind of conflict with how Result works, there's
not too much issue leaving them around and deprecating
at some later point.

References: #5087
References: #4395
Fixes: #4959
Change-Id: I8091919d45421e3f53029b8660427f844fee0228
2020-05-01 16:09:24 -04:00
mike bayer f911e191d6 Merge "Deprecate `DISTINCT ON` when not targeting PostgreSQL" 2020-04-20 17:15:54 +00:00
Federico Caselli 07d6d211f2 Deprecate `DISTINCT ON` when not targeting PostgreSQL
Deprecate usage of ``DISTINCT ON`` in dialect other than PostgreSQL.
Previously this was silently ignored.
Deprecate old usage of string distinct in MySQL dialect

Fixes: #4002
Change-Id: I38fc64aef75e77748083c11d388ec831f161c9c9
2020-04-20 11:28:51 -04:00
Gord Thompson 50cee43fbc Clean up .execute in test/sql/test_functions.py
Change-Id: I2bc7a50893f90c6ea7e119a8558731ee32965871
2020-04-20 07:46:24 -06:00
Mike Bayer 2f617f56f2 Create initial 2.0 engine implementation
Implemented the SQLAlchemy 2 :func:`.future.create_engine` function which
is used for forwards compatibility with SQLAlchemy 2.   This engine
features always-transactional behavior with autobegin.

Allow execution options per statement execution.  This includes
that the before_execute() and after_execute() events now accept
an additional dictionary with these options, empty if not
passed; a legacy event decorator is added for backwards compatibility
which now also emits a deprecation warning.

Add some basic tests for execution, transactions, and
the new result object.   Build out on a new testing fixture
that swaps in the future engine completely to start with.

Change-Id: I70e7338bb3f0ce22d2f702537d94bb249bd9fb0a
Fixes: #4644
2020-04-16 13:35:55 -04:00
Mike Bayer 8725d89aba Pass connection to TablesTest.insert_data()
towards the goal of reducing verbosity and repetition
in test fixtures as well as that we are moving to
connection only for execution, move the insert_data()
classmethod to accept a connection and adjust all
fixtures to use it.

Change-Id: I3bf534acca0d5f4cda1d4da8ae91f1155b829b09
2020-04-15 11:12:59 -04:00
mike bayer c9a27eac31 Merge "Clean up .execute in test/sql/test_returning.py" 2020-04-14 18:58:50 +00:00
mike bayer cc3c83a33a Merge "Clean up .execute in test/sql/test_query.py" 2020-04-14 18:57:34 +00:00
mike bayer 1ca0316cd7 Merge "Clean up .execute in test/sql/test_deprecations.py" 2020-04-14 18:54:24 +00:00
mike bayer 21dbd8aa0a Merge "Clean up .execute in test/sql/test_resultset.py" 2020-04-14 18:53:35 +00:00
Gord Thompson f947d744d0 Clean up .execute in test/sql/test_returning.py
Change-Id: I390b0c9926345f9f4deec06b51d1a11a18a72ca9
2020-04-14 05:59:46 -06:00
mike bayer 70fd8df5e3 Merge "Clean up .execute in test/sql/test_types.py" 2020-04-13 15:47:19 +00:00
mike bayer 2900ec572b Merge "Repair find tables" 2020-04-13 15:30:05 +00:00
Gord Thompson 4349e6ec97 Clean up .execute in test/sql/test_types.py
Change-Id: I917264ecefcc7e2fd60e7170a0a0ad83d8167571
2020-04-13 08:27:09 -06:00
Gord Thompson b4a835044d Clean up .execute in test/sql/test_query.py
Change-Id: I3f3a3b3f7a7ec67ef475a9792372e1c9a600fafb
2020-04-12 19:33:57 -06:00
Gord Thompson a731c005b8 Clean up .execute in test/sql/test_deprecations.py
Change-Id: I488f0992d5f26e164a903cbced11422046403647
2020-04-12 16:12:35 -06:00
Gord Thompson 6eb11d8777 Clean up .execute in test/sql/test_resultset.py
Change-Id: Ie4bb2b8e94361d15a3f1f1c21ce1c59cff1cf735
2020-04-12 13:37:48 -06:00
Federico Caselli 35b74d8a33 Add `comment attribute to :class:.Column __repr__` method.
Fixes #4138

Change-Id: Ia93f7d7b1d2f38509421725fbe152a2ff163d5f3
2020-04-11 20:50:37 +02:00
Mike Bayer 046934083b Repair CTE a in b tests
In I27cac9bd265c86ff2a3381ff9f844f60ef991cfc we modernized
the default tests and converted the "a in b" CTE tests to combinations,
however apparently the existing tests were not testing all
combinations and had repeats instead.   The combinations
decorator has made this much easier to spot, so use
the correct combinations that were originally intended.

Change-Id: Icd904887bff00c31525497d0b1508fabaf052dc9
2020-04-10 14:28:54 -04:00
mike bayer 1ff644d2f1 Merge "Remove code deprecated before version 1.1" 2020-04-10 17:52:47 +00:00
Mike Bayer fe59191303 Modernize test_defaults
Use modern execution patterns, goal is so that these same tests
can work for the future engine

break sequence tests into test_sequences suite

sequence tests that are testing implicit execution patterns
at least move into their own suite that will go into test_deprecations
eventually.

Change-Id: I27cac9bd265c86ff2a3381ff9f844f60ef991cfc
2020-04-10 10:47:15 -04:00
Mike Bayer 0aa38a9737 Repair find tables
The recipe given out on the ML for improving ABC
fails in 1.4 due to find_tables() missing out on subqueries,
which is due to the refactor of Alias into multiple sub-types.

Change-Id: Idd2803b26a84b28db28e0b8c328b8f24550577e4
2020-04-09 12:55:23 -04:00
Federico Caselli a9b068ae56 Remove code deprecated before version 1.1
- Remove deprecated method ``get_primary_keys` in the :class:`.Dialect` and
  :class:`.Inspector` classes.
- Remove deprecated event ``dbapi_error`` and the method ``ConnectionEvents.dbapi_error`.
- Remove support for deprecated engine URLs of the form ``postgres://``.
- Remove deprecated dialect ``mysql+gaerdbms``.
- Remove deprecated parameter ``quoting`` from :class:`.mysql.ENUM`
  and :class:`.mysql.SET` in the ``mysql`` dialect.
- Remove deprecated function ``comparable_property``. and function
  ``comparable_using`` in the declarative extension.
- Remove deprecated function ``compile_mappers``.
- Remove deprecated method ``collection.linker``.
- Remove deprecated method ``Session.prune`` and parameter ``Session.weak_identity_map``.
  This change also removes the class ``StrongInstanceDict``.
- Remove deprecated parameter ``mapper.order_by``.
- Remove deprecated parameter ``Session._enable_transaction_accounting`.
- Remove deprecated parameter ``Session.is_modified.passive``.
- Remove deprecated class ``Binary``. Please use :class:`.LargeBinary`.
- Remove deprecated methods ``Compiled.compile``, ``ClauseElement.__and__`` and
  ``ClauseElement.__or__`` and attribute ``Over.func``.
- Remove deprecated ``FromClause.count`` method.
- Remove deprecated parameter ``Table.useexisting``.
- Remove deprecated parameters ``text.bindparams`` and ``text.typemap``.

- Remove boolean support for the ``passive`` parameter in ``get_history``.
- Remove deprecated ``adapt_operator`` in ``UserDefinedType.Comparator``.

Fixes: #4643
Change-Id: Idcd390c77bf7b0e9957907716993bdaa3f1a1763
2020-04-09 00:33:22 +02:00
Mike Bayer 9243f6feea Ensure length parameter of Enum is adapted to new objects
Ensure length parameter added to Enum in
Iea05dc8cd9e33959bb968b394fb10a7dd068c873 is correctly propagated
to new enum objects adapted from this one.

Fixes: #5183

`

Change-Id: I7f20d926f73ec8260938963df87e29894c7e55e2
2020-04-06 16:19:23 -04:00
mike bayer 2d6146192f Merge "Add length parameter in Enum" 2020-04-06 20:11:20 +00:00
Federico Caselli a902660f71 Add length parameter in Enum
The `Enum` type now supports the parameter `Enum.length`
to specify the length of the VARCHAR column to create when using
non native enums by setting `Enum.native_enum` to `False`

Fixes: #5183
Change-Id: Iea05dc8cd9e33959bb968b394fb10a7dd068c873
2020-04-06 20:02:28 +02:00
mike bayer 45dd5654a1 Merge "fix typo on testing.skip_if assertion" 2020-04-06 16:51:32 +00:00
Mike Bayer 49b6c50016 Repair caching / traversals for values
The test suite wasn't running the copy_internals most fixtures,
enable that and try to get all cases working.

Set up selectable.values to do tuple conversion within compilation
step.  at the same time, disable caching for selectable.values
for the moment and make it equivalent to dml_multi_values.

fix cache / compare / copy cases for dml_values and dml_multi_values
which weren't fully tested or covered.

Change-Id: I484ca6e9cb2b66c2e6a321698f2abc0838db1460
2020-04-01 19:25:23 -04:00
Mike Bayer a9b62055bf Try to measure new style caching in the ORM, take two
Supercedes: If78fbb557c6f2cae637799c3fec2cbc5ac248aaf

Trying to see if by making the cache key memoized, we
still can have the older "identity" form of caching
which is the cheapest of all, at the same time as the
newer "cache key each time" version that is not nearly
as cheap; but still much cheaper than no caching at all.

Also needed is a per-execution update of _keymap when
we invoke from a cached select, so that Column objects
that are anonymous or otherwise adapted will match up.
this is analogous to the adaption of bound parameters
from the cache key.

Adds test coverage for the keymap / construct_params()
 changes related to caching.  Also hones performance
to a large extent for statement construction and
cache key generation.

Also includes a new memoized attribute
approach that vastly simplifies the previous approach
of "group_expirable_memoized_property" and finally
integrates cleanly with _clone(), _generate(), etc.
no more hardcoding of attributes is needed, as well
as that most _reset_memoization() calls are no longer
needed as the reset is inherent in a _generate() call;
this also has dramatic performance improvements.

Change-Id: I95c560ffcbfa30b26644999412fb6a385125f663
2020-04-01 16:12:23 -04:00
Federico Caselli c99f6781d3 fix typo on testing.skip_if assertion
Change-Id: I3f2eeffb0b59971a6025f9ce69250d6421751d16
2020-04-01 19:15:49 +02:00
mike bayer ffcc16b114 Merge "String compiler can now literal compile datetime objects" 2020-03-30 13:57:08 +00:00
mike bayer 73f7f85d9d Merge "Add a third labeling mode for SELECT statements" 2020-03-30 13:06:04 +00:00
Mike Bayer a65d5c250e Add a third labeling mode for SELECT statements
Enhanced the disambiguating labels feature of the
:func:`~.sql.expression.select` construct such that when a select statement
is used in a subquery, repeated column names from different tables are now
automatically labeled with a unique label name, without the need to use the
full "apply_labels()" feature that conbines tablename plus column name.
The disambigated labels are available as plain string keys in the .c
collection of the subquery, and most importantly the feature allows an ORM
:func:`.orm.aliased` construct against the combination of an entity and an
arbitrary subquery to work correctly, targeting the correct columns despite
same-named columns in the source tables, without the need for an "apply
labels" warning.

The existing labeling style is now called
LABEL_STYLE_TABLENAME_PLUS_COL.  This labeling style will remain used
throughout the ORM as has been the case for over a decade, however,
the new disambiguation scheme could theoretically replace this scheme
entirely.  The new scheme would dramatically alter how SQL looks
when rendered from the ORM to be more succinct but arguably harder
to read.

The tablename_columnname scheme used by Join.c is unaffected here,
as that's still hardcoded to that scheme.

Fixes: #5221
Change-Id: Ib47d9e0f35046b3afc77bef6e65709b93d0c3026
2020-03-29 22:27:41 -04:00
Federico Caselli 4bd4b483e0 Remove support for python 3.4
Also remove no longer used compat code

Change-Id: Ifda239fd84b425e43f4028cb55a5b3b8efa4dfc6
2020-03-30 00:35:33 +02:00
Federico Caselli 83eb1b23cb String compiler can now literal compile datetime objects
Add ability to literal compile a :class:`DateTime`, :class:`Date`
or :class:"Time" when using the string dialect for debugging purposes.
This change does not impact real dialect implementation that retain
their current behavior.

Fixes: #5052
Change-Id: Ia3fad2be905c6d35b0106b9a2388c7508f067e90
2020-03-29 18:08:40 -04:00