From 163c85c8ed327150a6e5c79f1a4b52a8848d4408 Mon Sep 17 00:00:00 2001 From: Toby Mao Date: Tue, 26 Mar 2024 17:09:18 -0700 Subject: [PATCH] fix!: convert dt with isoformat sep space for better compat, trino doesnt accept T --- sqlglot/expressions.py | 4 +++- tests/dataframe/unit/test_column.py | 4 ++-- tests/dataframe/unit/test_functions.py | 4 ++-- tests/test_expressions.py | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py index 2f1ea744e..5ca0d5d20 100644 --- a/sqlglot/expressions.py +++ b/sqlglot/expressions.py @@ -6980,7 +6980,9 @@ def convert(value: t.Any, copy: bool = False) -> Expression: return HexString(this=value.hex()) if isinstance(value, datetime.datetime): datetime_literal = Literal.string( - (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat() + (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat( + sep=" " + ) ) return TimeStrToTime(this=datetime_literal) if isinstance(value, datetime.date): diff --git a/tests/dataframe/unit/test_column.py b/tests/dataframe/unit/test_column.py index 7a12808c1..833005bcf 100644 --- a/tests/dataframe/unit/test_column.py +++ b/tests/dataframe/unit/test_column.py @@ -150,8 +150,8 @@ def test_between(self): F.col("cola").between(datetime.date(2022, 1, 1), datetime.date(2022, 3, 1)).sql(), ) self.assertEqual( - "cola BETWEEN CAST('2022-01-01T01:01:01+00:00' AS TIMESTAMP) " - "AND CAST('2022-03-01T01:01:01+00:00' AS TIMESTAMP)", + "cola BETWEEN CAST('2022-01-01 01:01:01+00:00' AS TIMESTAMP) " + "AND CAST('2022-03-01 01:01:01+00:00' AS TIMESTAMP)", F.col("cola") .between(datetime.datetime(2022, 1, 1, 1, 1, 1), datetime.datetime(2022, 3, 1, 1, 1, 1)) .sql(), diff --git a/tests/dataframe/unit/test_functions.py b/tests/dataframe/unit/test_functions.py index 12cbd1136..b5067ece9 100644 --- a/tests/dataframe/unit/test_functions.py +++ b/tests/dataframe/unit/test_functions.py @@ -29,7 +29,7 @@ def test_lit(self): test_date = SF.lit(datetime.date(2022, 1, 1)) self.assertEqual("CAST('2022-01-01' AS DATE)", test_date.sql()) test_datetime = SF.lit(datetime.datetime(2022, 1, 1, 1, 1, 1)) - self.assertEqual("CAST('2022-01-01T01:01:01+00:00' AS TIMESTAMP)", test_datetime.sql()) + self.assertEqual("CAST('2022-01-01 01:01:01+00:00' AS TIMESTAMP)", test_datetime.sql()) test_dict = SF.lit({"cola": 1, "colb": "test"}) self.assertEqual("STRUCT(1 AS cola, 'test' AS colb)", test_dict.sql()) @@ -51,7 +51,7 @@ def test_col(self): test_date = SF.col(datetime.date(2022, 1, 1)) self.assertEqual("CAST('2022-01-01' AS DATE)", test_date.sql()) test_datetime = SF.col(datetime.datetime(2022, 1, 1, 1, 1, 1)) - self.assertEqual("CAST('2022-01-01T01:01:01+00:00' AS TIMESTAMP)", test_datetime.sql()) + self.assertEqual("CAST('2022-01-01 01:01:01+00:00' AS TIMESTAMP)", test_datetime.sql()) test_dict = SF.col({"cola": 1, "colb": "test"}) self.assertEqual("STRUCT(1 AS cola, 'test' AS colb)", test_dict.sql()) diff --git a/tests/test_expressions.py b/tests/test_expressions.py index 9365fc4e6..0766fac0a 100644 --- a/tests/test_expressions.py +++ b/tests/test_expressions.py @@ -794,11 +794,11 @@ def test_convert(self): ({"x": None}, "MAP(ARRAY('x'), ARRAY(NULL))"), ( datetime.datetime(2022, 10, 1, 1, 1, 1, 1), - "TIME_STR_TO_TIME('2022-10-01T01:01:01.000001+00:00')", + "TIME_STR_TO_TIME('2022-10-01 01:01:01.000001+00:00')", ), ( datetime.datetime(2022, 10, 1, 1, 1, 1, tzinfo=datetime.timezone.utc), - "TIME_STR_TO_TIME('2022-10-01T01:01:01+00:00')", + "TIME_STR_TO_TIME('2022-10-01 01:01:01+00:00')", ), (datetime.date(2022, 10, 1), "DATE_STR_TO_DATE('2022-10-01')"), (math.nan, "NULL"),