Skip to content

Commit

Permalink
Allow optional time granularity for time dimensions in the where filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Oct 10, 2023
1 parent 0fec036 commit d44369e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dbt_semantic_interfaces/call_parameter_sets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple
from typing import Optional, Tuple

from dbt_semantic_interfaces.references import (
DimensionReference,
Expand All @@ -25,7 +25,7 @@ class TimeDimensionCallParameterSet:

entity_path: Tuple[EntityReference, ...]
time_dimension_reference: TimeDimensionReference
time_granularity: TimeGranularity
time_granularity: Optional[TimeGranularity] = None


@dataclass(frozen=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence
from typing import Optional, Sequence

from dbt_semantic_interfaces.call_parameter_sets import (
DimensionCallParameterSet,
Expand Down Expand Up @@ -31,7 +31,7 @@ def _exception_message_for_incorrect_format(element_name: str) -> str:

@staticmethod
def create_time_dimension(
time_dimension_name: str, time_granularity_name: str, entity_path: Sequence[str] = ()
time_dimension_name: str, time_granularity_name: Optional[str] = None, entity_path: Sequence[str] = ()
) -> TimeDimensionCallParameterSet:
"""Gets called by Jinja when rendering {{ TimeDimension(...) }}."""
group_by_item_name = DunderedNameFormatter.parse_name(time_dimension_name)
Expand All @@ -56,7 +56,7 @@ def create_time_dimension(
entity_path=(
tuple(EntityReference(element_name=arg) for arg in entity_path) + group_by_item_name.entity_links
),
time_granularity=TimeGranularity(time_granularity_name),
time_granularity=TimeGranularity(time_granularity_name) if time_granularity_name is not None else None,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self) -> None: # noqa
def create(
self,
time_dimension_name: str,
time_granularity_name: str,
time_granularity_name: Optional[str] = None,
entity_path: Sequence[str] = (),
descending: Optional[bool] = None,
date_part_name: Optional[str] = None,
Expand Down
20 changes: 19 additions & 1 deletion tests/implementations/where_filter/test_parse_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_extract_time_dimension_call_parameter_sets() -> None: # noqa: D

def test_extract_metric_time_dimension_call_parameter_sets() -> None: # noqa: D
parse_result = PydanticWhereFilter(
where_sql_template=("""{{ TimeDimension('metric_time', 'month') }} = '2020-01-01'""")
where_sql_template="""{{ TimeDimension('metric_time', 'month') }} = '2020-01-01'"""
).call_parameter_sets

assert parse_result == FilterCallParameterSets(
Expand Down Expand Up @@ -215,3 +215,21 @@ def test_where_filter_intersection_error_collection() -> None:
ParameterSetFactory._exception_message_for_incorrect_format("customer__has_delivery_address")
not in error_string
)


def test_time_dimension_without_granularity() -> None: # noqa: D
parse_result = PydanticWhereFilter(
where_sql_template="{{ TimeDimension('booking__created_at') }} > 2023-09-18"
).call_parameter_sets

assert parse_result == FilterCallParameterSets(
dimension_call_parameter_sets=(),
time_dimension_call_parameter_sets=(
TimeDimensionCallParameterSet(
entity_path=(EntityReference("booking"),),
time_dimension_reference=TimeDimensionReference(element_name="created_at"),
time_granularity=None,
),
),
entity_call_parameter_sets=(),
)

0 comments on commit d44369e

Please sign in to comment.