Skip to content

Commit

Permalink
added logic to cast the bigquery date filters to datetime (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlokvenec authored Dec 23, 2024
1 parent f95d0c3 commit d8bfd1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions metrics_layer/core/model/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ class Definitions:

does_not_exist = "__DOES_NOT_EXIST__"
canon_date_join_graph_root = "canon_date_core"

date_format_tz = "%Y-%m-%dT%H:%M:%SZ"
15 changes: 13 additions & 2 deletions metrics_layer/core/sql/query_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def validate(self, definition: Dict) -> None:
except Exception:
pass

if self.design.query_type in Definitions.needs_datetime_cast and isinstance(
definition["value"], datetime.datetime
if self.design.query_type in Definitions.needs_datetime_cast and self._can_convert_to_datetime(
definition["value"]
):
definition["value"] = datatype_cast(self.field, definition["value"])

Expand All @@ -166,6 +166,17 @@ def validate(self, definition: Dict) -> None:
if self.field.type == "yesno" and "True" in str(definition["value"]):
definition["expression"] = "boolean_true"

def _can_convert_to_datetime(self, value):
if isinstance(value, datetime.datetime):
return True
if isinstance(value, str):
try:
datetime.datetime.strptime(value, Definitions.date_format_tz)
return True
except ValueError:
return False
return False

def group_sql_query(
self,
functional_pk: str,
Expand Down

0 comments on commit d8bfd1e

Please sign in to comment.