From: Mike Bayer Date: Wed, 24 Jun 2026 12:49:30 +0000 (-0400) Subject: override get_select_precolumns() in StrSQLCompiler X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1cf779b3f38404b07e4e2962e03154756debac06;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git override get_select_precolumns() in StrSQLCompiler Fixed issue where :meth:`_sql.Select.get_final_froms` would emit a deprecation warning when the statement made use of the PostgreSQL-specific expression argument to :meth:`_sql.Select.distinct`; the same spurious warning would be emitted when stringifying such a statement without explicitly using a PostgreSQL dialect. The fix ensures that this 1.4-era warning is suppressed under both 2.0 and 2.1. Note that under SQLAlchemy 2.1, passing an expression to :meth:`_sql.Select.distinct` is deprecated overall, and is replaced by a new PostgreSQL-specific construct (see :ticket:`12342`). Fixes: #13396 Change-Id: I587e24aa7016c56b1d5bfe1048c4b6eb809dfb30 (cherry picked from commit d011fcf9c2532abeae5a686db78b6c1ad1318bce) --- diff --git a/doc/build/changelog/unreleased_20/13396.rst b/doc/build/changelog/unreleased_20/13396.rst new file mode 100644 index 0000000000..542aa3dfb5 --- /dev/null +++ b/doc/build/changelog/unreleased_20/13396.rst @@ -0,0 +1,14 @@ +.. change:: + :tags: bug, sql + :tickets: 13396 + + Fixed issue where :meth:`_sql.Select.get_final_froms` would emit a + deprecation warning when the statement made use of the PostgreSQL-specific + expression argument to :meth:`_sql.Select.distinct`; the same spurious + warning would be emitted when stringifying such a statement without + explicitly using a PostgreSQL dialect. The fix ensures that this 1.4-era + warning is suppressed under both 2.0 and 2.1. + + Note that under SQLAlchemy 2.1, passing an expression to + :meth:`_sql.Select.distinct` is deprecated overall, and is replaced by a + new PostgreSQL-specific construct (see :ticket:`12342`). \ No newline at end of file diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 7f7243bbe4..df90b69c69 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -6655,6 +6655,9 @@ class StrSQLCompiler(SQLCompiler): """ + def get_select_precolumns(self, select: Select[Any], **kw: Any) -> str: + return "DISTINCT " if select._distinct else "" + def _fallback_column_name(self, column): return "" diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index d7ad8d09e6..af494f6a24 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -1984,12 +1984,29 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): "SELECT count(DISTINCT mytable.myid) AS count_1 FROM mytable", ) + @testing.variation("case", ["stringify", "final_froms"]) + def test_distinct_expr_no_dep_warning_str_compiler(self, case): + """test for #13396""" + + t = Table("foo", MetaData(), Column("bar")) + + stmt = select(t).distinct(t.c.bar) + + if case.stringify: + eq_(str(stmt), "SELECT DISTINCT foo.bar \nFROM foo") + elif case.final_froms: + eq_(stmt.get_final_froms(), [t]) + else: + case.fail() + def test_distinct_on(self): with testing.expect_deprecated( "DISTINCT ON is currently supported only by the PostgreSQL " "dialect" ): - select("*").distinct(table1.c.myid).compile() + self.assert_compile( + select("*").distinct(table1.c.myid), "SELECT DISTINCT *" + ) def test_where_empty(self): self.assert_compile(