]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed the behavior of extract() to apply operator
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 15 Nov 2009 20:11:42 +0000 (20:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 15 Nov 2009 20:11:42 +0000 (20:11 +0000)
precedence rules to the "::" operator when applying
the "timestamp" cast - ensures proper parenthesization.
[ticket:1611]

CHANGES
lib/sqlalchemy/databases/postgres.py
test/dialect/test_postgres.py

diff --git a/CHANGES b/CHANGES
index 00bf8913477a62f83151182654e47ce1ca3ea077..34b44557942d39b954f0e4adbf225f42fd6ea74a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -65,6 +65,11 @@ CHANGES
     - Corrected the "has_sequence" query to take current schema,
       or explicit sequence-stated schema, into account.
       [ticket:1576]
+
+    - Fixed the behavior of extract() to apply operator
+      precedence rules to the "::" operator when applying
+      the "timestamp" cast - ensures proper parenthesization.  
+      [ticket:1611]
       
 - mssql
     - Changed the name of TrustedConnection to 
index f380bcb28ff68879972eaf4d3ee9d38179bcdda3..a46ed6723d4895761d3a8032e0481b97fe624326 100644 (file)
@@ -821,8 +821,8 @@ class PGCompiler(compiler.DefaultCompiler):
 
     def visit_extract(self, extract, **kwargs):
         field = self.extract_map.get(extract.field, extract.field)
-        return "EXTRACT(%s FROM %s::timestamp)" % (
-            field, self.process(extract.expr))
+        return "EXTRACT(%s FROM %s)" % (
+            field, self.process(extract.expr.op('::')(sql.literal_column('timestamp'))))
 
 
 class PGSchemaGenerator(compiler.SchemaGenerator):
index fb915665a79fce29f551f0b9f266d4f12daaa8fc..f8eefec5c990eb91e8cb1ca751efd9fb8cb2794c 100644 (file)
@@ -66,9 +66,14 @@ class CompileTest(TestBase, AssertsCompiledSQL):
         for field in 'year', 'month', 'day':
             self.assert_compile(
                 select([extract(field, t.c.col1)]),
-                "SELECT EXTRACT(%s FROM t.col1::timestamp) AS anon_1 "
+                "SELECT EXTRACT(%s FROM t.col1 :: timestamp) AS anon_1 "
                 "FROM t" % field)
 
+        for field in 'year', 'month', 'day':
+            self.assert_compile(
+                select([extract(field, func.timestamp() - datetime.timedelta(days =5))]),
+                "SELECT EXTRACT(%s FROM (timestamp() - %%(timestamp_1)s) :: timestamp) AS anon_1"
+                 % field)
 
 class ReturningTest(TestBase, AssertsExecutionResults):
     __only_on__ = 'postgres'
@@ -507,7 +512,18 @@ class MiscTest(TestBase, AssertsExecutionResults):
             self.assert_((subject.c['id$']==referer.c.ref).compare(subject.join(referer).onclause))
         finally:
             meta1.drop_all()
-
+    
+    def test_extract(self):
+        for field, exp in (
+                    ('year', 2009),
+                    ('month', 11),
+                    ('day', 10),
+            ):
+            r = testing.db.execute(
+                select([extract(field, datetime.datetime(2009, 11, 15, 12, 15, 35) - datetime.timedelta(days =5))])
+            ).scalar()
+            eq_(r, exp)
+            
     def test_checksfor_sequence(self):
         meta1 = MetaData(testing.db)
         t = Table('mytable', meta1,