From c6551cd8b1160fdde82a55d908bb1a090b45dfad Mon Sep 17 00:00:00 2001 From: laughingman7743 Date: Sat, 21 Mar 2020 13:21:55 +0900 Subject: [PATCH 1/3] Changed to use CAST function in date and datetime type conversion (fix #126) --- pyathena/formatter.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pyathena/formatter.py b/pyathena/formatter.py index 4848a4a7..8be63c88 100644 --- a/pyathena/formatter.py +++ b/pyathena/formatter.py @@ -44,17 +44,11 @@ def _format_default(formatter, escaper, val): def _format_date(formatter, escaper, val): - if escaper is _escape_presto: - return "date'{0}'".format(val.strftime("%Y-%m-%d")) - else: - return "'{0}'".format(val.strftime("%Y-%m-%d")) + return "CAST('{0}' AS DATE)".format(val.strftime("%Y-%m-%d")) def _format_datetime(formatter, escaper, val): - if escaper is _escape_presto: - return "timestamp'{0}'".format(val.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) - else: - return "'{0}'".format(val.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) + return "CAST('{0}' AS TIMESTAMP)".format(val.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) def _format_bool(formatter, escaper, val): From 91176a3135d419279a82f58310bd941a35a4ec41 Mon Sep 17 00:00:00 2001 From: laughingman7743 Date: Sat, 21 Mar 2020 14:44:01 +0900 Subject: [PATCH 2/3] Use date & datetime literals --- pyathena/formatter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyathena/formatter.py b/pyathena/formatter.py index 8be63c88..6f342ce2 100644 --- a/pyathena/formatter.py +++ b/pyathena/formatter.py @@ -44,11 +44,11 @@ def _format_default(formatter, escaper, val): def _format_date(formatter, escaper, val): - return "CAST('{0}' AS DATE)".format(val.strftime("%Y-%m-%d")) + return "DATE '{0}'".format(val.strftime("%Y-%m-%d")) def _format_datetime(formatter, escaper, val): - return "CAST('{0}' AS TIMESTAMP)".format(val.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) + return "TIMESTAMP '{0}'".format(val.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) def _format_bool(formatter, escaper, val): From bfb7ca0a5394edae95ae6f5d722458370a3158d1 Mon Sep 17 00:00:00 2001 From: laughingman7743 Date: Sat, 21 Mar 2020 14:48:34 +0900 Subject: [PATCH 3/3] Fix test case --- tests/test_formatter.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_formatter.py b/tests/test_formatter.py index 67156138..68ce3a4c 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -22,7 +22,7 @@ def format(self, operation, parameters=None): def test_add_partition(self): expected = """ ALTER TABLE test_table - ADD PARTITION (dt='2017-01-01', hour=1) + ADD PARTITION (dt=DATE '2017-01-01', hour=1) """.strip() actual = self.format(""" @@ -34,7 +34,7 @@ def test_add_partition(self): def test_drop_partition(self): expected = """ ALTER TABLE test_table - DROP PARTITION (dt='2017-01-01', hour=1) + DROP PARTITION (dt=DATE '2017-01-01', hour=1) """.strip() actual = self.format(""" @@ -61,8 +61,8 @@ def test_format_datetime(self): expected = """ SELECT * FROM test_table - WHERE col_timestamp >= timestamp'2017-01-01 12:00:00.000' - AND col_timestamp <= timestamp'2017-01-02 06:00:00.000' + WHERE col_timestamp >= TIMESTAMP '2017-01-01 12:00:00.000' + AND col_timestamp <= TIMESTAMP '2017-01-02 06:00:00.000' """.strip() actual = self.format(""" @@ -77,7 +77,7 @@ def test_format_date(self): expected = """ SELECT * FROM test_table - WHERE col_date between date'2017-01-01' and date'2017-01-02' + WHERE col_date between DATE '2017-01-01' and DATE '2017-01-02' """.strip() actual = self.format(""" @@ -190,7 +190,7 @@ def test_format_datetime_list(self): SELECT * FROM test_table WHERE col_timestamp IN - (timestamp'2017-01-01 12:00:00.000', timestamp'2017-01-02 06:00:00.000') + (TIMESTAMP '2017-01-01 12:00:00.000', TIMESTAMP '2017-01-02 06:00:00.000') """.strip() actual = self.format(""" @@ -205,7 +205,7 @@ def test_format_date_list(self): expected = """ SELECT * FROM test_table - WHERE col_date IN (date'2017-01-01', date'2017-01-02') + WHERE col_date IN (DATE '2017-01-01', DATE '2017-01-02') """.strip() actual = self.format("""