diff --git a/metrics_layer/core/sql/query_filter.py b/metrics_layer/core/sql/query_filter.py index e95cc5b..c8f70bc 100644 --- a/metrics_layer/core/sql/query_filter.py +++ b/metrics_layer/core/sql/query_filter.py @@ -19,6 +19,8 @@ def datatype_cast(field, value): + if field.datatype.upper() == "DATE": + return LiteralValue(f"CAST(CAST('{value}' AS TIMESTAMP) AS DATE)") return LiteralValue(f"CAST('{value}' AS {field.datatype.upper()})") diff --git a/pyproject.toml b/pyproject.toml index 4f98325..ca6d324 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "metrics_layer" -version = "0.12.34" +version = "0.12.35" description = "The open source metrics layer." authors = ["Paul Blankley "] keywords = ["Metrics Layer", "Business Intelligence", "Analytics"] diff --git a/tests/test_arbitrary_merged_results.py b/tests/test_arbitrary_merged_results.py index 167f552..61b0bf4 100644 --- a/tests/test_arbitrary_merged_results.py +++ b/tests/test_arbitrary_merged_results.py @@ -375,7 +375,7 @@ def test_query_merged_queries_dim_group(connection, query_type): product_group = "order_lines_product_name" lines_order_by = "" orders_order_by = "" - time = "CAST('2018-01-02 00:00:00' AS DATE)" + time = "CAST(CAST('2018-01-02 00:00:00' AS TIMESTAMP) AS DATE)" condition = ( "CAST(merged_query_0.orders_order_date AS TIMESTAMP)=CAST(merged_query_1.order_lines_order_date" " AS TIMESTAMP)" diff --git a/tests/test_join_query.py b/tests/test_join_query.py index 9ba10fc..d116c99 100644 --- a/tests/test_join_query.py +++ b/tests/test_join_query.py @@ -87,6 +87,10 @@ def test_query_bigquery_week_filter_type_conversion(connection, field): ) cast_as = "DATE" if "order_lines.order_week" == field else "TIMESTAMP" + if cast_as == "DATE": + casted = f"CAST(CAST('2021-08-04 00:00:00' AS TIMESTAMP) AS {cast_as})" + else: + casted = f"CAST('2021-08-04 00:00:00' AS {cast_as})" sql_field = "order_lines.order_date" if "order_lines.order_week" == field else "orders.order_date" join = "" if "orders" in field: @@ -94,8 +98,8 @@ def test_query_bigquery_week_filter_type_conversion(connection, field): correct = ( "SELECT order_lines.sales_channel as order_lines_channel,SUM(order_lines.revenue) as" f" order_lines_total_item_revenue FROM analytics.order_line_items order_lines {join}WHERE" - f" CAST(DATE_TRUNC(CAST({sql_field} AS DATE), WEEK) AS {cast_as})>CAST('2021-08-04 00:00:00' AS" - f" {cast_as}) GROUP BY order_lines_channel;" + f" CAST(DATE_TRUNC(CAST({sql_field} AS DATE), WEEK) AS {cast_as})>{casted} GROUP BY" + " order_lines_channel;" ) assert query == correct diff --git a/tests/test_simple_query.py b/tests/test_simple_query.py index b9a628e..8e83783 100644 --- a/tests/test_simple_query.py +++ b/tests/test_simple_query.py @@ -1489,9 +1489,9 @@ def test_simple_query_with_where_dim_group(connections, field, expression, value ): condition = "CAST(DATE_TRUNC(CAST(simple.previous_order_date AS DATE), DAY) AS DATETIME)>CAST('2021-08-04 00:00:00' AS DATETIME)" # noqa elif query_type == Definitions.trino and isinstance(value, datetime) and field == "first_order_date": - condition = "DATE_TRUNC('DAY', CAST(simple.first_order_date AS TIMESTAMP))>CAST('2021-08-04 00:00:00' AS DATE)" # noqa + condition = "DATE_TRUNC('DAY', CAST(simple.first_order_date AS TIMESTAMP))>CAST(CAST('2021-08-04 00:00:00' AS TIMESTAMP) AS DATE)" # noqa elif query_type == Definitions.bigquery and isinstance(value, datetime) and field == "first_order_date": - condition = "CAST(DATE_TRUNC(CAST(simple.first_order_date AS DATE), DAY) AS DATE)>CAST('2021-08-04 00:00:00' AS DATE)" # noqa + condition = "CAST(DATE_TRUNC(CAST(simple.first_order_date AS DATE), DAY) AS DATE)>CAST(CAST('2021-08-04 00:00:00' AS TIMESTAMP) AS DATE)" # noqa elif sf_or_rs and expression == "matches" and value == "last year": last_year = pendulum.now("UTC").year - 1 if query_type == Definitions.trino: