]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Correctly apply self_group in type_coerce element.
authorFederico Caselli <cfederico87@gmail.com>
Fri, 22 May 2020 21:56:50 +0000 (23:56 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Fri, 22 May 2020 21:59:51 +0000 (23:59 +0200)
The type coerce element did not correctly apply grouping rules when using
in an expression

Fixes: #5344
Change-Id: Id67b0e60ac54f8992f931aaed62731672f60c96c
(cherry picked from commit d163088de1d68919b6811a25745d3becbbf5b069)

doc/build/changelog/unreleased_13/5344.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/sql/test_selectable.py

diff --git a/doc/build/changelog/unreleased_13/5344.rst b/doc/build/changelog/unreleased_13/5344.rst
new file mode 100644 (file)
index 0000000..d2e598c
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 5344
+
+    Correctly apply self_group in type_coerce element.
+
+    The type coerce element did not correctly apply grouping rules when using
+    in an expression
\ No newline at end of file
index cd41a3f05ebf60a9710ed2f632a60ba8327d4902..f050ce9a21da9803cc597a25fa793a8837d1bc93 100644 (file)
@@ -2645,7 +2645,7 @@ class TypeCoerce(ColumnElement):
 
             SELECT date_string AS anon_1 FROM log
 
-        When result rows are fetched, the ``StringDateTime`` type
+        When result rows are fetched, the ``StringDateTime`` type processor
         will be applied to result rows on behalf of the ``date_string`` column.
         The rationale for the "anon_1" label is so that the type-coerced
         column remains separate in the list of result columns vs. other
@@ -2714,6 +2714,13 @@ class TypeCoerce(ColumnElement):
         else:
             return self.clause
 
+    def self_group(self, against=None):
+        grouped = self.clause.self_group(against=against)
+        if grouped is not self.clause:
+            return TypeCoerce(grouped, self.type)
+        else:
+            return self
+
 
 class Extract(ColumnElement):
     """Represent a SQL EXTRACT clause, ``extract(field FROM expr)``."""
index 71532601d12be1953c10386f2a06bce1cc8ac18d..fd8b997c42c49ea43eb4bcdaa6b402db0a7d064a 100644 (file)
@@ -351,6 +351,13 @@ class SelectableTest(
         assert isinstance(stmt.c.foo.type, MyType)
         assert isinstance(stmt2.c.foo.type, MyType)
 
+    def test_type_coerce_selfgroup(self):
+        no_group = column("a") / type_coerce(column("x"), Integer)
+        group = column("b") / type_coerce(column("y") * column("w"), Integer)
+
+        self.assert_compile(no_group, "a / x")
+        self.assert_compile(group, "b / (y * w)")
+
     def test_select_on_table(self):
         sel = select([table1, table2], use_labels=True)