]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- mysql [bug] Fixed bug whereby if cast() is used
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Apr 2012 16:08:32 +0000 (12:08 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Apr 2012 16:08:32 +0000 (12:08 -0400)
on a SQL expression whose type is not supported
by cast() and therefore CAST isn't rendered by
the dialect, the order of evaluation could change
if the casted expression required that it be
grouped; grouping is now applied to those
expressions.  [ticket:2467]

CHANGES
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/test_mysql.py

diff --git a/CHANGES b/CHANGES
index 633695d8d3155ffcaaf3606ae3fea7b213123dc2..83408f68d5ffe156404f2e30d43a56c52a427913 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -91,6 +91,14 @@ CHANGES
     to retrieve views marked as "SYSTEM VIEW".
     courtesy Matthew Turland.
 
+  - [bug] Fixed bug whereby if cast() is used
+    on a SQL expression whose type is not supported
+    by cast() and therefore CAST isn't rendered by
+    the dialect, the order of evaluation could change
+    if the casted expression required that it be
+    grouped; grouping is now applied to those
+    expressions.  [ticket:2467]
+
 0.7.6
 =====
 - orm
index 780007a4d843839861356acf6e086510c1b24ca0..d28e934a3c61b25d4a742561356692d410fd40dd 100644 (file)
@@ -1273,11 +1273,11 @@ class MySQLCompiler(compiler.SQLCompiler):
     def visit_cast(self, cast, **kwargs):
         # No cast until 4, no decimals until 5.
         if not self.dialect._supports_cast:
-            return self.process(cast.clause)
+            return self.process(cast.clause.self_group())
 
         type_ = self.process(cast.typeclause)
         if type_ is None:
-            return self.process(cast.clause)
+            return self.process(cast.clause.self_group())
 
         return 'CAST(%s AS %s)' % (self.process(cast.clause), type_)
 
index 5a34a79a0d6278cef29e8e9585576d954bb10b3d..8a880645c2d208d353dca6e446d4fbb4bfd9ec57 100644 (file)
@@ -1379,6 +1379,21 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL):
                     dialect=dialect
             )
 
+    def test_cast_grouped_expression_non_castable(self):
+        self.assert_compile(
+            cast(sql.column('x') + sql.column('y'), Float),
+            "(x + y)"
+        )
+
+    def test_cast_grouped_expression_pre_4(self):
+        dialect = mysql.dialect()
+        dialect.server_version_info = (3, 2, 3)
+        self.assert_compile(
+            cast(sql.column('x') + sql.column('y'), Integer),
+            "(x + y)",
+            dialect=dialect
+        )
+
     def test_extract(self):
         t = sql.table('t', sql.column('col1'))