From: indivar Date: Fri, 20 Oct 2023 15:25:38 +0000 (-0400) Subject: use oracle.INTERVAL for generic interval X-Git-Tag: rel_2_0_23~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=932a70a91ffc8c1c1e948de6fa1a3a882bebf8b7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git use oracle.INTERVAL for generic interval Fixed issue in :class:`.Interval` datatype where the Oracle implementation was not being used for DDL generation, leading to the ``day_precision`` and ``second_precision`` parameters to be ignored, despite being supported by this dialect. Pull request courtesy Indivar. Fixes: #10509 Closes: #10513 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10513 Pull-request-sha: a3b901978504b1bc726f722fc27ca4827de05274 Change-Id: I074afeeac182567f9104c5175b81c735d87e1352 --- diff --git a/doc/build/changelog/unreleased_20/10509.rst b/doc/build/changelog/unreleased_20/10509.rst new file mode 100644 index 0000000000..28500435a2 --- /dev/null +++ b/doc/build/changelog/unreleased_20/10509.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, oracle + :tickets: 10509 + + Fixed issue in :class:`.Interval` datatype where the Oracle implementation + was not being used for DDL generation, leading to the ``day_precision`` and + ``second_precision`` parameters to be ignored, despite being supported by + this dialect. Pull request courtesy Indivar. diff --git a/lib/sqlalchemy/dialects/oracle/types.py b/lib/sqlalchemy/dialects/oracle/types.py index 62028c7673..860186195c 100644 --- a/lib/sqlalchemy/dialects/oracle/types.py +++ b/lib/sqlalchemy/dialects/oracle/types.py @@ -203,6 +203,15 @@ class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval): second_precision=interval.second_precision, ) + @classmethod + def adapt_emulated_to_native( + cls, interval: sqltypes.Interval, **kw # type: ignore[override] + ): + return INTERVAL( + day_precision=interval.day_precision, + second_precision=interval.second_precision, + ) + @property def _type_affinity(self): return sqltypes.Interval diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 343575f196..8f7a818aae 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -2052,8 +2052,8 @@ class Interval(Emulated, _AbstractInterval, TypeDecorator[dt.timedelta]): """A type for ``datetime.timedelta()`` objects. The Interval type deals with ``datetime.timedelta`` objects. In - PostgreSQL, the native ``INTERVAL`` type is used; for others, the - value is stored as a date which is relative to the "epoch" + PostgreSQL and Oracle, the native ``INTERVAL`` type is used; for others, + the value is stored as a date which is relative to the "epoch" (Jan. 1, 1970). Note that the ``Interval`` type does not currently provide date arithmetic diff --git a/test/dialect/oracle/test_types.py b/test/dialect/oracle/test_types.py index a970adc4ba..7bf05aafaf 100644 --- a/test/dialect/oracle/test_types.py +++ b/test/dialect/oracle/test_types.py @@ -183,6 +183,10 @@ class DialectTypesTest(fixtures.TestBase, AssertsCompiledSQL): oracle.INTERVAL(day_precision=2, second_precision=5), "INTERVAL DAY(2) TO SECOND(5)", ), + ( + sqltypes.Interval(day_precision=9, second_precision=3), + "INTERVAL DAY(9) TO SECOND(3)", + ), ) def test_interval(self, type_, expected): self.assert_compile(type_, expected)