Commit Graph

2119 Commits

Author SHA1 Message Date
Kaan 7e28adbe0c Implement GROUPS frame spec for window functions
Implemented support for the GROUPS frame specification in window functions
by adding :paramref:`_sql.over.groups` option to :func:`_sql.over`
and :meth:`.FunctionElement.over`. Pull request courtesy Kaan Dikmen.

Fixes: #12450
Closes: #12445
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12445
Pull-request-sha: c0808e135f

Change-Id: I9ff504a9c9650485830c4a0eaf44162898a3a2ad
2025-03-27 13:34:52 +00:00
Michael Bayer dabd77992d Merge "Cast empty PostgreSQL ARRAY from the type specified to array()" into main 2025-03-20 01:41:54 +00:00
Michael Bayer 0db547f6ab Merge "skip FROM disambiguation for immediate alias of table" into main 2025-03-19 23:58:23 +00:00
Denis Laxalde 588cc6ed8e Cast empty PostgreSQL ARRAY from the type specified to array()
When building a PostgreSQL ``ARRAY`` literal using
:class:`_postgresql.array` with an empty ``clauses`` argument, the
:paramref:`_postgresql.array.type_` parameter is now significant in that it
will be used to render the resulting ``ARRAY[]`` SQL expression with a
cast, such as ``ARRAY[]::INTEGER``. Pull request courtesy Denis Laxalde.

Fixes: #12432
Closes: #12435
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12435
Pull-request-sha: 9633d3c15d

Change-Id: I29ed7bd0562b82351d22de0658fb46c31cfe44f6
2025-03-19 19:32:09 -04:00
Mike Bayer 9ea3be0681 skip FROM disambiguation for immediate alias of table
Fixed regression caused by 🎫`7471` leading to a SQL compilation
issue where name disambiguation for two same-named FROM clauses with table
aliasing in use at the same time would produce invalid SQL in the FROM
clause with two "AS" clauses for the aliased table, due to double aliasing.

Fixes: #12451
Change-Id: I981823f8f2cdf3992d65ace93a21fc20d1d74cda
2025-03-19 18:30:21 -04:00
Mike Bayer c86ebb0a99 implement use_descriptor_defaults for dataclass defaults
A significant behavioral change has been made to the behavior of the
:paramref:`_orm.mapped_column.default` and
:paramref:`_orm.relationship.default` parameters, when used with
SQLAlchemy's :ref:`orm_declarative_native_dataclasses` feature introduced
in 2.0, where the given value (assumed to be an immutable scalar value) is
no longer passed to the ``@dataclass`` API as a real default, instead a
token that leaves the value un-set in the object's ``__dict__`` is used, in
conjunction with a descriptor-level default.  This prevents an un-set
default value from overriding a default that was actually set elsewhere,
such as in relationship / foreign key assignment patterns as well as in
:meth:`_orm.Session.merge` scenarios.   See the full writeup in the
:ref:`whatsnew_21_toplevel` document which includes guidance on how to
re-enable the 2.0 version of the behavior if needed.

This adds a new implicit default field to ScalarAttributeImpl
so that we can have defaults that are not in the dictionary but
are instead passed through to the class-level descriptor, effectively
allowing custom defaults that are not used in INSERT or merge

Fixes: #12168
Change-Id: Ia327d18d6ec47c430e926ab7658e7b9f0666206e
2025-03-19 15:14:37 -04:00
Michael Bayer a385bd9b50 Merge "remove deprecated features" into main 2025-03-18 16:12:15 +00:00
Federico Caselli 1ebd8c525b remove deprecated features
Remove feature deprecates in 1.3 and before

Fixes: #12441
Change-Id: Ice3d35ec02988ce94cdeb9db41cb684db2fb5d8d
2025-03-18 09:17:07 -04:00
Michael Bayer fd6c08f403 Merge "add postgresql distinct_on (patch 4)" into main 2025-03-18 13:00:56 +00:00
Mike Bayer 5f8ac70996 add postgresql distinct_on (patch 4)
Added syntax extension :func:`_postgresql.distinct_on` to build ``DISTINCT
ON`` clauses. The old api, that passed columns to
:meth:`_sql.Select.distinct`, is now deprecated.

Fixes: #12342
Change-Id: Ia6a7e647a11e57b6ac2f50848778c20dc55eaf54
2025-03-17 15:51:21 -04:00
Denis Laxalde 39bb17442c Support column list for foreign key ON DELETE SET actions on PostgreSQL
Added support for specifying a list of columns for ``SET NULL`` and ``SET
DEFAULT`` actions of ``ON DELETE`` clause of foreign key definition on
PostgreSQL.  Pull request courtesy Denis Laxalde.

Fixes: #11595
Closes: #12421
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12421
Pull-request-sha: d0394db706

Change-Id: I036a559ae4a8efafe9ba64d776a840bd785a7397
2025-03-17 14:02:24 -04:00
Mike Bayer ec20f346a6 anonymize CRUD params if visiting_cte is present
Fixed issue in :class:`.CTE` constructs involving multiple DDL
:class:`.Insert` statements with multiple VALUES parameter sets where the
bound parameter names generated for these parameter sets would conflict,
generating a compile time error.

Fixes: #12363
Change-Id: If8344ff725d4e0ec58d3ff61f38a0edcfc5bdebd
2025-03-14 10:33:22 -04:00
Mike Bayer d5d4189ef6 restate all upsert in terms of statement extensions (patch 3)
Change-Id: I0595ba8e2bd930e22f4c06d7a813bcd23060cb7a
2025-03-06 18:28:18 +00:00
Mike Bayer 8be3b096a6 syntax extensions (patch 1)
Added the ability to create custom SQL constructs that can define new
clauses within SELECT, INSERT, UPDATE, and DELETE statements without
needing to modify the construction or compilation code of of
:class:`.Select`, :class:`.Insert`, :class:`.Update`, or :class:`.Delete`
directly.  Support for testing these constructs, including caching support,
is present along with an example test suite.  The use case for these
constructs is expected to be third party dialects for NewSQL or other novel
styles of database that introduce new clauses to these statements.   A new
example suite is included which illustrates the ``QUALIFY`` SQL construct
used by several NewSQL databases which includes a cachable implementation
as well as a test suite.

Since these extensions start to make it a bit crowded with how many
kinds of "options" we have on statements, did some naming /
documentation changes with existing constructs on Executable, in
particular to distinguish ExecutableOption from SyntaxExtension.

Fixes: #12195
Change-Id: I4a44ee5bbc3d8b1b640837680c09d25b1b7077af
2025-03-05 16:03:16 -05:00
Federico Caselli 9b9e1e127f test related fixes
improve test error logging
add order to test

Change-Id: I2003f256a2690ee5673c72e2f1cb1340af750f83
2025-03-05 20:41:48 +01:00
Mike Bayer d6f11d9030 allow control of constraint isolation w/ add/drop constraint
Added new parameters :paramref:`.AddConstraint.isolate_from_table` and
:paramref:`.DropConstraint.isolate_from_table`, defaulting to True, which
both document and allow to be controllable the long-standing behavior of
these two constructs blocking the given constraint from being included
inline within the "CREATE TABLE" sequence, under the assumption that
separate add/drop directives were to be used.

Fixes: #12382
Change-Id: I53c4170ccb5803f69945ba7aa3d3a143131508eb
2025-02-27 14:48:03 -05:00
Mike Bayer 42ddb1fd5f check that two CTEs aren't just annotated forms of the same thing
Fixed issue where using :func:`_orm.aliased` around a :class:`.CTE`
construct could cause inappropriate "duplicate CTE" errors in cases where
that aliased construct appeared multiple times in a single statement.

Fixes: #12364
Change-Id: I9625cd83e9baf5312cdc644b38951353708d3b86
2025-02-20 13:33:44 -05:00
Michael Bayer 576ac6ad7e Merge "Default python_type impl now returns object" into main 2025-02-13 16:31:56 +00:00
Federico Caselli 3a998cbb52 Default python_type impl now returns object
The default implementation of :attr:`_sql.TypeEngine.python_type` now
returns ``object`` instead of ``NotImplementedError``, since that's the
base for all types in Python3.
The ``python_type`` of :class:`_sql.JSON` no longer returns ``dict``,
but instead fallbacks to the generic implementation.

Fixes: #10646
Change-Id: I2233e4a3d35a67b520a860d70afba8e5b22fd72d
2025-02-12 21:42:41 +01:00
Michael Bayer 48b2adf63b Merge "remove None exception in IN" into main 2025-02-06 15:14:43 +00:00
Mike Bayer 79505b03b6 remove None exception in IN
Fixed SQL composition bug which impacted caching where using a ``None``
value inside of an ``in_()`` expression would bypass the usual "expanded
bind parameter" logic used by the IN construct, which allows proper caching
to take place.

Fixes: #12314
References: #12312
Change-Id: I0d2fc4e15c73407379ba368dd4ee32660fc66259
2025-02-05 14:08:51 -05:00
Mike Bayer 3cd9a5b42f reorganize column collection init to be local
Reorganized the internals by which the `.c` collection on a
:class:`.FromClause` gets generated so that it is resilient against the
collection being accessed in concurrent fashion.   An example is creating a
:class:`.Alias` or :class:`.Subquery` and accessing it as a module level
variable.  This impacts the Oracle dialect which uses such module-level
global alias objects but is of general use as well.

Fixes: #12302
Change-Id: I30cb07c286affce24e2d85e49f9df5b787438d86
2025-02-05 14:03:49 -05:00
Michael Bayer 860ca0b9d2 Merge "Separate Numeric and Float" into main 2024-12-09 21:47:48 +00:00
Mike Bayer 311e142ea6 Separate Numeric and Float
the :class:`.Numeric` and :class:`.Float` SQL types have been separated out
so that :class:`.Float` no longer inherits from :class:`.Numeric`; instead,
they both extend from a common mixin :class:`.NumericCommon`.  This
corrects for some architectural shortcomings where numeric and float types
are typically separate, and establishes more consistency with
:class:`.Integer` also being a distinct type.   The change should not have
any end-user implications except for code that may be using
``isinstance()`` to test for the :class:`.Numeric` datatype; third party
dialects which rely upon specific implementation types for numeric and/or
float may also require adjustment to maintain compatibility.

Fixes:  #5252
Change-Id: Iadc841340b3d97e3eb5f7e63f0a0cc3cb4e30f74
2024-12-09 14:29:26 -05:00
Federico Caselli d539bc3a0f update the format_docs_code to also work on python files
Change-Id: I0a6c9610b3fd85365ed4c2c199e3cad87ee64022
2024-11-30 21:10:21 +01:00
Mike Bayer 0cd1104323 remove _implicit_subquery and all derived functions
The ``.c`` and ``.columns`` attributes on the :class:`.Select` and
:class:`.TextualSelect` constructs, which are not instances of
:class:`.FromClause`, have been removed completely, in addition to the
``.select()`` method as well as other codepaths which would implicitly
generate a subquery from a :class:`.Select` without the need to explicitly
call the :meth:`.Select.subquery` method.

In the case of ``.c`` and ``.columns``, these attributes were never useful
in practice and have caused a great deal of confusion, hence were
deprecated back in version 1.4, and have emitted warnings since that
version.   Accessing the columns that are specific to a :class:`.Select`
construct is done via the :attr:`.Select.selected_columns` attribute, which
was added in version 1.4 to suit the use case that users often expected
``.c`` to accomplish.  In the larger sense, implicit production of
subqueries works against SQLAlchemy's modern practice of making SQL
structure as explicit as possible.

Note that this is **not related** to the usual :attr:`.FromClause.c` and
:attr:`.FromClause.columns` attributes, common to objects such as
:class:`.Table` and :class:`.Subquery`,  which are unaffected by this
change.

Fixes: #10236
Change-Id: If241b8674ccacce7e860bfed25b5d266bfe1aca7
2024-11-17 08:19:53 -05:00
Mike Bayer 5bbefc41b7 dont leak mutating bindparams list into AnalyzedFunction
Fixed issue in "lambda SQL" feature where the tracking of bound parameters
could be corrupted if the same lambda were evaluated across multiple
compile phases, including when using the same lambda across multiple engine
instances or with statement caching disabled.

Fixes: #12084
Change-Id: I327aa93ce7feb2326a22113164bd834b96b6b889
2024-11-13 11:22:41 -05:00
Michael Bayer fa76fbda4a Merge "Float and Numeric aren't set as autoincrement" into main 2024-11-12 18:45:59 +00:00
Federico Caselli 2adc79c077 Removed support for Python 3.8 since it's EOL.
Fixes: #12029
Change-Id: Ibb4efec9bab0225d03f6bf3fed661a3f2fc72cc7
2024-11-05 21:23:34 +01:00
Federico Caselli 9d5f1c0f53 Float and Numeric aren't set as autoincrement
The :class:`.Float` and :class:`.Numeric` types are no longer automatically
considered as auto-incrementing columns when the
:paramref:`_schema.Column.autoincrement` parameter is left at its default
of ``"auto"`` on a :class:`_schema.Column` that is part of the primary key.
When the parameter is set to ``True``, a :class:`.Numeric` type will be
accepted as an auto-incrementing datatype for primary key columns, but only
if its scale is explicitly given as zero; otherwise, an error is raised.
This is a change from 2.0 where all numeric types including floats were
automatically considered as "autoincrement" for primary key columns.

Fixes: #11811
Change-Id: Icdfe084d425166199d6647335c5b53ea5b4b416e
2024-10-31 22:04:01 +01:00
Mike Bayer aaddd7c840 refine in_() check to use proper duck-typing for __clause_element__
Fixed regression caused by an internal code change in response to recent
Mypy releases that caused the very unusual case of a list of ORM-mapped
attribute expressions passed to :meth:`.ColumnOperators.in_` to no longer
be accepted.

in this commit we had to revisit d8dd28c42e where mypy typing
didn't accept ColumnOperartors.   the type here is the _HasClauseElement[_T]
protocol which means we need to use a duck type for a runtime check.

Fixes: #12019
Change-Id: Ib378e9cb8defb49d5ac4d726ec93d6bdc581b6a9
2024-10-21 10:19:27 -04:00
Mike Bayer fa56821578 add tests for pickling types inside an expression, some reduce methods
Fixed regression from 1.4 where some datatypes such as those derived from
:class:`.TypeDecorator` could not be pickled when they were part of a
larger SQL expression composition due to internal supporting structures
themselves not being pickleable.

Fixes: #12002
Change-Id: I016e37b0c62071413f24c9aac35f6ecf475becaa
2024-10-15 12:00:44 -04:00
Mike Bayer 858eba6156 _Binary as generic to LargeBinary
Datatypes that are binary based such as :class:`.VARBINARY` will resolve to
:class:`.LargeBinary` when the :meth:`.TypeEngine.as_generic()` method is
called.

Fixes: #11978
Change-Id: I2e0586324fb0f1c367da61f0074b35c96fbe2fd0
2024-10-10 11:52:28 -04:00
Mike Bayer 22cbc7dcb4 pass to_metadata argument to Enum.copy()
Fixed bug where the ``metadata`` element of an ``Enum`` datatype would not
be transferred to the new :class:`.MetaData` object when the type had been
copied via a :meth:`.Table.to_metadata` operation, leading to inconsistent
behaviors within create/drop sequences.

Fixes: #11802
Change-Id: Ibbc93aa31bdfde0d67a9530f41a08e826c17d58e
2024-08-29 11:49:50 -04:00
Mike Bayer a9747467a8 restore generative to with_statement_hint
Fixed regression in :meth:`_sql.Select.with_statement_hint` and others
where the generative behavior of the method stopped producing a copy of the
object.

Fixes: #11703
Change-Id: Ia4482f91f76fae9982dc6b075bf5cfec7042ffa6
2024-08-07 12:19:05 -04:00
Federico Caselli 881be0a216 add CTE cache elements for CompoundSelect, more verify tests
Follow up of 🎫`11471` to fix caching issue where using the
:meth:`.CompoundSelectState.add_cte` method of the
:class:`.CompoundSelectState` construct would not set a correct cache key
which distinguished between different CTE expressions. Also added tests
that would detect issues similar to the one fixed in 🎫`11544`.

Fixes: #11471
Change-Id: Iae6a91077c987d83cd70ea826daff42855491330
2024-07-31 11:09:43 -04:00
Mike Bayer 96f1172812 include nulls_first, nulls_last in order_by_label_element
Fixed bug where the :meth:`.Operators.nulls_first()` and
:meth:`.Operators.nulls_last()` modifiers would not be treated the same way
as :meth:`.Operators.desc()` and :meth:`.Operators.asc()` when determining
if an ORDER BY should be against a label name already in the statement. All
four modifiers are now treated the same within ORDER BY.

Fixes: #11592
Change-Id: I1de1aff679c56af1abfdfd07f9bcbc45ecc5a8cc
2024-07-10 11:40:24 -04:00
Michael Bayer 8f47774de9 Merge "alter the collation of string type for collate()" into main 2024-07-07 02:49:30 +00:00
Mike Bayer 35c178c405 alter the collation of string type for collate()
Fixed issue where the :func:`_sql.collate` construct, which explicitly sets
a collation for a given expression, would maintain collation settings for
the underlying type object from the expression, causing SQL expressions to
have both collations stated at once when used in further expressions for
specific dialects that render explicit type casts, such as that of asyncpg.
The :func:`_sql.collate` construct now assigns its own type to explicitly
include the new collation, assuming it's a string type.

Fixes: #11576
Change-Id: I6fc8904d2bcbc21f11bbca57e4a451ed0edbd879
2024-07-06 16:02:34 -04:00
Michael Bayer 89ea870661 Merge "use driver col names" into main 2024-07-05 14:10:40 +00:00
Federico Caselli 37b20999a3 Merge "Allow flat for join with name" into main 2024-07-04 20:30:24 +00:00
Eric Atkin 0e40962bf3 Allow flat for join with name
The :paramref:`_orm.aliased.name` parameter to :func:`_orm.aliased` may now
be combined with the :paramref:`_orm.aliased.flat` parameter, producing
per-table names based on a name-prefixed naming convention.  Pull request
courtesy Eric Atkin.

Fixes: #11575
Closes: #11531
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11531
Pull-request-sha: f85535464b

Change-Id: If79679c7a9598fffe99c033894b7dffecef13939
2024-07-04 13:53:41 -04:00
Mike Bayer b3105b7e3a use driver col names
Added new execution option
:paramref:`_engine.Connection.execution_options.driver_column_names`. This
option disables the "name normalize" step that takes place against the
DBAPI ``cursor.description`` for uppercase-default backends like Oracle,
and will cause the keys of a result set (e.g. named tuple names, dictionary
keys in :attr:`.Row._mapping`, etc.) to be exactly what was delivered in
cursor.description.   This is mostly useful for plain textual statements
using :func:`_sql.text` or :meth:`_engine.Connection.exec_driver_sql`.

Fixes: #10789
Change-Id: Ib647b25bb53492fa839af04dd032d9f061e630af
2024-07-04 10:50:19 -04:00
Mike Bayer fb47dbbc74 handle DBAPI error for fetchall()
Fixed issue in "insertmanyvalues" feature where a particular call to
``cursor.fetchall()`` were not wrapped in SQLAlchemy's exception wrapper,
which apparently can raise a database exception during fetch when using
pyodbc.

Fixes: #11532
Change-Id: Ic07d3e79dd597e18d87a56b45ddffa25e762beb9
2024-07-01 09:17:24 -04:00
Mike Bayer 6d2f43e14f cache key share; support correct traverse of 'of'
Fixed caching issue where the
:paramref:`_sql.Select.with_for_update.key_share` element of
:meth:`_sql.Select.with_for_update` was not considered as part of the cache
key, leading to incorrect caching if different variations of this parameter
were used with an otherwise identical statement.

Also repairs a traversal issue where the ``of`` element of
``ForUpdateArg`` when set to ``None`` cannot be compared against a
non-None element because the traversal defines it as a clauselist.
Traversal in this case is adjusted to accommodate for this case so that
we dont need to create a risky-to-backport change to ``ForUpdateArg``
itself.

Fixes: #11544
Change-Id: Ie8a50716df06977af58b0c22a8c10e1b64d972b9
2024-06-27 18:23:49 -04:00
Michael Bayer c0bb145305 Merge "use a new ClauseElement for Over.range_ / Over.rows" into main 2024-06-25 13:57:14 +00:00
Mike Bayer f522e43cc7 use a new ClauseElement for Over.range_ / Over.rows
Enhanced the caching structure of the :paramref:`.over.rows` and
:paramref:`.over.range` so that different numerical values for the rows /
range fields are cached on the same cache key, to the extent that the
underlying SQL does not actually change (i.e. "unbounded", "current row",
negative/positive status will still change the cache key).  This prevents
the use of many different numerical range/rows value for a query that is
otherwise identical from filling up the SQL cache.

Note that the semi-private compiler method ``_format_frame_clause()``
is removed by this fix, replaced with a new method
``visit_frame_clause()``.  Third party dialects which may have referred
to this method will need to change the name and revise the approach to
rendering the correct SQL for that dialect.

This patch introduces a new ClauseElement called _FrameClause which
stores the integer range values separately and within cache-compatible
BindParameter objects from the "type" which
can be unbounded, current, preceding, or following, represented by
a _FrameClauseType enum.    The negative
sign is also stripped from the integer and represented within the
_FrameClauseType.  Tests from #11514 are adapted to include
a test for SQL Server's "literal_execute" flag taking effect so
that literal numeric values aren't stored in the cache.

Fixes: #11515
Change-Id: I8aad368ffef9f06cb5c3f8c4e971fadef029ffd5
2024-06-24 15:21:18 -04:00
Mike Bayer dffd96e754 create JoinedDispatcher subclasses up front
Fixed additional issues in the event system triggered by unpickling of a
:class:`.Enum` datatype, continuing from 🎫`11365` and
🎫`11360`,  where dynamically generated elements of the event
structure would not be present when unpickling in a new process.

Fixes: #11530
Change-Id: Ie1f2b3453d4891051f8719f6d3f6703302d5a86e
2024-06-24 15:13:41 -04:00
Mike Bayer faecebc9df include HasCTE traversal elements in TextualSelect
Fixed caching issue where using the :meth:`.TextualSelect.add_cte` method
of the :class:`.TextualSelect` construct would not set a correct cache key
which distinguished between different CTE expressions.

Fixes: #11471
Change-Id: Ia9ce2c8cfd128f0f130aa9b26448dc23d994c324
2024-06-10 22:59:49 -04:00
Michael Bayer 6f704cac01 Merge "Add missing function element methods" into main 2024-06-03 14:24:46 +00:00