Commit Graph

12130 Commits

Author SHA1 Message Date
Thomas Stephenson da47f40739 Code review fixes
- Removed dprint statements
- Added right-hand versions of commutative operators
- Improved docstrings
- Added changelog entry
- Added dialect documentation
2025-06-16 22:23:26 +10:00
Thomas Stephenson b4563b6389 Fix mypy error
- `count` is not accepted as a keyword argument to str.replace in python < 3.13
2025-05-17 11:48:37 +10:00
Thomas Stephenson e07097ddd2 Fix errors from initial CE run
- fix mypy and linter issues
- expand BitString tests for int and bytes conversion
- Only run BIT test types on postgresql dialect
- Fix problems in `BitString.from_int` and `BitString.from_bytes`
- Add `BitString.to_bytes` method to allow specifying the byte
  length of the returned instances
- Fixed problems testing operators in BIT type tests
2025-05-16 19:31:07 +10:00
Thomas Stephenson 7617485827 10556: Create new BitString subclass for bitstrings for PG drivers
- Added custom `BitString` implementation which inherits from `str`
  and overrides methods and operators appropriately and
  implements the bitwise operations in a manner consistent with
  postgresql. Also exposes class and instance methods ensuring
  instances can be converted to/from `int` and `bytes` sensibly.

- Added default implementations of `bind_processor` and
  `result_processor` to `postgresql.BIT` type to convert `BitString`
  to/from string for PG drivers.

- Override `bind_processor` and `result_processor` in AsyncpgBit
  in order to convert `BitString` to/from `asynpg.BitString`
  in `asyncpg` driver.

- Added support for bitwise comparators to postgresql `BIT` instances

Fixes: 10556
2025-05-15 23:21:49 +10:00
Shamil 37c5b2e3e2 Remove unused typing imports (#12568)
* Remove unused typing imports

* remove unused per file ignores

* Revert "remove unused per file ignores"

---------

Co-authored-by: Pablo Estevez <pablo22estevez@gmail.com>
2025-05-05 20:05:21 +02:00
Mike Bayer d689e465ed fix sqlite localtimestamp function
Fixed and added test support for a few SQLite SQL functions hardcoded into
the compiler most notably the "localtimestamp" function which rendered with
incorrect internal quoting.

Fixes: #12566
Change-Id: Id5bd8dc7841f0afab7df031ba5c0854dab845a1d
2025-05-01 09:43:29 -04:00
Federico Caselli 4ac02007e0 add correct typing for row getitem
The overloads were broken in 8a4c275895

Change-Id: I3736b15e95ead28537e25169a54521e991f763da
2025-04-29 22:41:30 +02:00
Federico Caselli a84444fca1 Merge "refactor (orm): remove unused variables and simplify key lookups" into main 2025-04-27 19:43:33 +00:00
Federico Caselli 2989548791 refactor (orm): remove unused variables and simplify key lookups
Redundant variables and unnecessary conditions were removed across several modules. Improved readability and reduced code complexity without changing functionality.

Closes: #12537
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12537
Pull-request-sha: ab53f8c348

Change-Id: I910d65729fdbc96933f9822c553924d37e89e201
2025-04-27 20:04:55 +02:00
Mike Bayer ce3bbfcc45 fix reference cycles/ perf in DialectKWArgs
Identified some unnecessary cycles and overhead in how
this is implemented.  since we want to add this to Select,
needs these improvements.

Change-Id: I4324db14aaf52ab87a8b7fa49ebf1b6624bc2dcb
2025-04-26 11:32:30 -04:00
Federico Caselli 0124b9449e Merge "refactor(testing-and-utils): Remove unused code and fix style issues" into main 2025-04-24 21:51:04 +00:00
Federico Caselli f847680bbc Merge "refactor: clean up unused variables in engine module" into main 2025-04-24 21:50:31 +00:00
Federico Caselli 841374be00 Merge "refactor (sql): simplify and optimize internal SQL handling" into main 2025-04-24 21:49:58 +00:00
Federico Caselli 8faf47044e Merge "refactor: simplify and clean up dialect-specific code" into main 2025-04-24 20:32:06 +00:00
Federico Caselli 571bb90932 Add pow operator support
Added support for the pow operator (``**``), with a default SQL
implementation of the ``POW()`` function.   On Oracle Database, PostgreSQL
and MSSQL it renders as ``POWER()``.   As part of this change, the operator
routes through a new first class ``func`` member :class:`_functions.pow`,
which renders on Oracle Database, PostgreSQL and MSSQL as ``POWER()``.

Fixes: #8579
Closes: #8580
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8580
Pull-request-sha: 041b2ef474

Change-Id: I371bd44ed3e58f2d55ef705aeec7d04710c97f23
2025-04-22 15:54:50 +00:00
Michael Bayer 801e1e1982 Merge "optimize @util.decorator" into main 2025-04-21 23:08:40 +00:00
Shamil 93b0be7009 refactor (sql): simplify and optimize internal SQL handling
Replaced redundant variable assignments with direct operations. Used `dict.get()` for safer dictionary lookups to streamline logic. Improves code readability and reduces unnecessary lines.

Closes: #12538
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12538
Pull-request-sha: d322d1508c

Change-Id: Ib3dfc7086ec35117fdad65e136a17aa014b96ae5
2025-04-21 12:36:21 -04:00
Shamil 64f45d0a6b refactor(testing-and-utils): Remove unused code and fix style issues
This PR includes several small refactorings and style fixes aimed at improving code cleanliness, primarily within the test suite and tooling.

Key changes:

*   Removed assignments to unused variables in various test files (`test_dialect.py`, `test_reflection.py`, `test_select.py`).
*   Removed an unused variable in the pytest plugin (`pytestplugin.py`).
*   Removed an unused variable in the topological sort utility (`topological.py`).
*   Fixed a minor style issue (removed an extra blank line) in the `cython_imports.py` script.

Closes: #12539
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12539
Pull-request-sha: 837c1e6cb1

Change-Id: Ifa37fb956bc3cacd31967f08bdaa4254e16911c2
2025-04-21 12:35:43 -04:00
Shamil d1d81f80a3 refactor: clean up unused variables in engine module
Removed unused variables to improve code clarity and maintainability. This change simplifies logic in `base.py`, `default.py`, and `result.py`. No functionality was altered.

Closes: #12535
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12535
Pull-request-sha: a9d849f3a4

Change-Id: If78b18dbd33733c631f8b5aad7d55261fbc4817b
2025-04-21 15:08:27 +00:00
Shamil bb5bfb4beb refactor: simplify and clean up dialect-specific code
**Title:** Removed unused variables and redundant functions across multiple dialects. Improves code readability and reduces maintenance complexity without altering functionality.
### Description
This pull request introduces several minor refactorings across different dialect modules:
- **MSSQL:**
    - Simplified the initialization of the `fkeys` dictionary in `_get_foreign_keys` using `util.defaultdict` directly.

- **MySQL:** Removed the unused variable in `_get_table_comment`. `rp`
- **PostgreSQL (_psycopg_common):** Removed the unused variable `cursor` in `do_ping`.
- **PostgreSQL (base):** Removed the unused variable `args` in `_get_column_info`.
- **SQLite:** Removed the unused variable `new_filename` in `generate_driver_url`.

These changes focus purely on code cleanup and simplification, removing dead code and improving clarity. They do not alter the existing logic or functionality of the dialects.
### Checklist
This pull request is:
- [ ] A documentation / typographical / small typing error fix
- [x] A short code fix
    - _Note: This is a general cleanup refactor rather than a fix for a specific reported issue._

- [ ] A new feature implementation

**Have a nice day!**

Closes: #12534
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12534
Pull-request-sha: 2c7ae17b73

Change-Id: I1ec3b48f42aea7e45bc20f81add03051eb30bb98
2025-04-21 15:08:22 +00:00
Mike Bayer 3217acc113 disable mysql/connector-python, again
Just as we got this driver "working", a new regression is introduced
in version 9.3.0 which prevents basic binary string persistence [1].
I would say we need to leave this driver off for another few years
until something changes with its upstream maintenance.

[1] https://bugs.mysql.com/bug.php?id=118025

Change-Id: If876f63ebb9a6f7dfa0b316df044afa469a154f2
2025-04-21 10:22:02 -04:00
Ryu Juheon 299284cec6 chore: add type hint for reconstructor (#12527)
* chore: add type hint for reconstructor

* chore: fix attr-defined

* chore: use defined typevar

* chore: ignore type error
2025-04-17 21:48:54 +02:00
Matt John f2a9ecde29 chore: Fix typo of psycopg2 in comment (#12526)
This is the first example in the documentation of a particular connector, which mgith result in copy+pastes, resulting in an error
2025-04-15 21:05:36 +02:00
Federico Caselli d6489a7600 Merge "Type postgresql.aggregate_order_by()" into main 2025-04-10 22:32:29 +00:00
Michael Bayer 5925687491 Merge "simplify internal storage of DML ordered values" into main 2025-04-10 15:07:52 +00:00
Mike Bayer 359f2ef702 simplify internal storage of DML ordered values
towards some refactorings I will need to do for #12496, this
factors out the "_ordered_values" list of tuples that was used to
track UPDATE VALUES in a specific order.   The rationale for this
separate collection was due to Python dictionaries not maintaining
insert order.   Now that this is standard behavior in Python 3
we can use the same `statement._values` for param-ordered and
table-column-ordered UPDATE rendering.

Change-Id: Id6024ab06e5e3ba427174e7ba3630ff83d81f603
2025-04-10 10:28:56 -04:00
Denis Laxalde 09c1d3ccac Type postgresql.aggregate_order_by()
Overloading of `__init__()` is needed, probably for the same reason as it is in `ReturnTypeFromArgs`.

Related to #6810.

Closes: #12463
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12463
Pull-request-sha: 701d979e20

Change-Id: I7e1bb4d2c48dfb3461725c7079aaa72c66f1dc03
2025-04-09 03:04:20 -04:00
Inada Naoki d5a913c8ae orm.exc.NoResultFound => exc.NoResultFound (#12509)
* s/orm.exc.NoResultFound/exc.NoResultFound/

* use _exc
2025-04-08 22:43:25 +02:00
Inada Naoki 370f13fe88 optimize @util.decorator
### Description

util.decorator uses code generation + eval to create signature matching wrapper.
It consumes some CPU because we can not use pyc cache.

Additionally, each wrapped function has own globals for function annotations.

By stripping function annotations from eval-ed code, compile time and memory usage are saved.

```python
from sqlalchemy.util import decorator
from sqlalchemy import *
import timeit
import tracemalloc
import sqlalchemy.orm._orm_constructors

@decorator
def with_print(fn, *args, **kwargs):
    res = fn(*args, **kwargs)
    print(f"{fn.__name__}(*{args}, **{kwargs}) => {res}")
    return res

# test
PI = 3.14

def f():
    @with_print
    def add(x: int|float, *, y: int|float=PI) -> int|float:
        return x + y
    return add

add = f()
add(1)
print(add.__annotations__)

# benchmark
print(timeit.timeit(f, number=1000)*1000, "us")

# memory
tracemalloc.start(1)
[f() for _ in range(1000)]
mem, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f"{mem=}, {peak=}")
```

Result:
```
$ .venv/bin/python -VV
Python 3.14.0a6 (main, Mar 17 2025, 21:27:10) [Clang 20.1.0 ]

$ .venv/bin/python sample.py
add(*(1,), **{'y': 3.14}) => 4.140000000000001
{'x': int | float, 'y': int | float, 'return': int | float}
35.93937499681488 us
mem=9252896, peak=9300808

$ git switch -
Switched to branch 'opt-decorator'

$ .venv/bin/python sample.py
add(*(1,), **{'y': 3.14}) => 4.140000000000001
{'x': int | float, 'y': int | float, 'return': int | float}
23.32574996398762 us
mem=1439032, peak=1476423
```

### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

This pull request is:

- [ ] A documentation / typographical / small typing error fix
	- Good to go, no issue or tests are needed
- [x] A short code fix
	- please include the issue number, and create an issue if none exists, which
	  must include a complete example of the issue.  one line code fixes without an
	  issue and demonstration will not be accepted.
	- Please include: `Fixes: #<issue number>` in the commit message
	- please include tests.   one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
	- please include the issue number, and create an issue if none exists, which must
	  include a complete example of how the feature would look.
	- Please include: `Fixes: #<issue number>` in the commit message
	- please include tests.

Closes: #12502
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12502
Pull-request-sha: 34409cbbfd

Change-Id: I88b88eb6eb018608bc2881459f58564881d06641
2025-04-07 19:55:48 -04:00
Federico Caselli d0bc9b206d Merge "Support postgresql_include in UniqueConstraint and PrimaryKeyConstraint" into main 2025-04-04 19:04:01 +00:00
Federico Caselli 084eac6916 Merge "improve overloads applied to generic functions" into main 2025-04-03 19:22:37 +00:00
Michael Bayer ecbd1228c2 Merge "ensure ON UPDATE test is case insensitive" into main 2025-04-02 16:32:35 +00:00
Alexander Ruehe 6f8f4a7d62 ensure ON UPDATE test is case insensitive
Fixed regression caused by the DEFAULT rendering changes in 2.0.40
🎫`12425` where using lowercase `on update` in a MySQL server default
would incorrectly apply parenthesis, leading to errors when MySQL
interpreted the rendered DDL.  Pull request courtesy Alexander Ruehe.

Fixes: #12488
Closes: #12489
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12489
Pull-request-sha: b9008f747d

Change-Id: If5281c52415e4ddb6c2f8aee191d2335f6673b35
2025-04-02 11:23:00 -04:00
Federico Caselli 0861969379 minor cleanup of postgresql index reflection query
Change-Id: I669ea8e99c6b69cb70263b0cacd80d3ed0fab39c
2025-04-01 23:49:36 +02:00
Denis Laxalde 3b7725dd12 Support postgresql_include in UniqueConstraint and PrimaryKeyConstraint
This is supported both for schema definition and reflection.

Fixes #10665.

Closes: #12485
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12485
Pull-request-sha: 1aabea7b55ece9fc0c6e069b777d4404ac01f964

Change-Id: I81d23966f84390dd1b03f0d13284ce6d883ee24e
2025-04-01 23:20:32 +02:00
Federico Caselli 77d8609d7b Merge "Add type annotations to postgresql.pg_catalog" into main 2025-04-01 19:10:01 +00:00
Michael Bayer 451225588f Merge "Removed executable coercion" into main 2025-03-31 12:34:16 +00:00
Michael Bayer 77a6750efe Merge "Type array_agg()" into main 2025-03-27 16:34:35 +00:00
Michael Bayer 7fdada9c90 Merge "implement AsyncSessionTransaction._regenerate_proxy_for_target" into main 2025-03-27 16:29:58 +00:00
Mike Bayer 0202673a34 implement AsyncSessionTransaction._regenerate_proxy_for_target
Fixed issue where :meth:`.AsyncSession.get_transaction` and
:meth:`.AsyncSession.get_nested_transaction` would fail with
``NotImplementedError`` if the "proxy transaction" used by
:class:`.AsyncSession` were garbage collected and needed regeneration.

Fixes: #12471
Change-Id: Ia8055524618df706d7958786a500cdd25d9d8eaf
2025-03-27 11:11:04 -04:00
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 94c0976ad3 Merge "compatibility with typing_extensions 4.13 and type statement" into main 2025-03-27 13:33:30 +00:00
Daraan 690e754b65 compatibility with typing_extensions 4.13 and type statement
Fixed regression caused by ``typing_extension==4.13.0`` that introduced
a different implementation for ``TypeAliasType`` while SQLAlchemy assumed
that it would be equivalent to the ``typing`` version.

Added test regarding generic TypeAliasType

Fixes: #12473
Closes: #12472
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12472
Pull-request-sha: 8861a5acfb

Change-Id: I053019a222546a625ed6d588314ae9f5b34c2f8a
2025-03-27 00:36:23 +01:00
Federico Caselli a9b3719913 document sqlite truncate_microseconds in DATETIME and TIME
Change-Id: I93412d951b466343f2cf9b6d513ad46d17f5d8ee
2025-03-26 21:43:10 +01:00
Federico Caselli 5cc6a65c61 improve overloads applied to generic functions
try again to remove the overloads to the generic functionn
generator (like coalesce, array_agg, etc).
As of mypy 1.15 it still does now work, but a simpler version
is added in this change

Change-Id: I8b97ae00298ec6f6bf8580090e5defff71e1ceb0
2025-03-25 23:39:05 +01:00
Stefanie Molin aae34df0b5 Add missing imports to example (#12453) 2025-03-25 20:05:44 +01:00
Denis Laxalde 864f79d7c4 Add type annotations to postgresql.pg_catalog
Related to #6810.

Closes: #12462
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12462
Pull-request-sha: 5a131cc9a9

Change-Id: Ie4494d61f815edefef6a896499db4292fd94a22a
2025-03-25 04:51:30 -04:00
Denis Laxalde 543acbd8d1 Type array_agg()
The return type of `array_agg()` is declared as a `Sequence[T]` where `T` is bound to the type of input argument.

This is implemented by making `array_agg()` inheriting from `ReturnTypeFromArgs` which provides appropriate overloads of `__init__()` to support this.

This usage of ReturnTypeFromArgs is a bit different from previous ones as the return type of the function is not exactly the same as that of its arguments, but a "collection" (a generic, namely a Sequence here) of the argument types.  Accordingly, we adjust the code of `tools/generate_sql_functions.py` to retrieve the "collection" type from 'fn_class' annotation and generate expected return type.

Also add a couple of hand-written typing tests for PostgreSQL.

Related to #6810

Closes: #12461
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12461
Pull-request-sha: ba27cbb863

Change-Id: I3fd538cc7092a0492c26970f0b825bf70ddb66cd
2025-03-24 22:22:35 +01: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