Skip to content

Commit

Permalink
fix casting for bq and trino
Browse files Browse the repository at this point in the history
  • Loading branch information
pblankley committed Aug 15, 2024
1 parent a588f37 commit 35a3f63
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions metrics_layer/core/sql/query_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()})")


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
keywords = ["Metrics Layer", "Business Intelligence", "Analytics"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_arbitrary_merged_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
8 changes: 6 additions & 2 deletions tests/test_join_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ 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:
join = "LEFT JOIN analytics.orders orders ON order_lines.order_unique_id=orders.id "
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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_simple_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 35a3f63

Please sign in to comment.