From: Mike Bayer Date: Mon, 16 Apr 2012 16:08:32 +0000 (-0400) Subject: - mysql [bug] Fixed bug whereby if cast() is used X-Git-Tag: rel_0_7_7~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdda4b0e018f8c1a869411b7ed31387ea90cb082;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - mysql [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] --- diff --git a/CHANGES b/CHANGES index 633695d8d3..83408f68d5 100644 --- 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 diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 780007a4d8..d28e934a3c 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -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_) diff --git a/test/dialect/test_mysql.py b/test/dialect/test_mysql.py index 5a34a79a0d..8a880645c2 100644 --- a/test/dialect/test_mysql.py +++ b/test/dialect/test_mysql.py @@ -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'))