Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Time Granularity for Time Dimensions in the Where Filter #176

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231010-111707.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: 'Optional Time Granularity for Time Dimensions in the Where Filter'
time: 2023-10-10T11:17:07.95603-07:00
custom:
Author: plypaul
Issue: "175"
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=(),
)
Loading