-
Notifications
You must be signed in to change notification settings - Fork 99
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
Implemented date_part in where filter #852
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Implemented date_part in where filter. | ||
time: 2023-11-07T18:08:43.67846-06:00 | ||
custom: | ||
Author: DevonFulcher | ||
Issue: None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import pytest | ||
from dbt_semantic_interfaces.implementations.filters.where_filter import PydanticWhereFilter | ||
from dbt_semantic_interfaces.references import EntityReference | ||
from dbt_semantic_interfaces.type_enums.date_part import DatePart | ||
from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity | ||
|
||
from metricflow.query.query_exceptions import InvalidQueryException | ||
|
@@ -98,6 +99,94 @@ def test_time_dimension_in_filter( # noqa: D | |
) | ||
|
||
|
||
def test_date_part_in_filter( # noqa: D | ||
column_association_resolver: ColumnAssociationResolver, | ||
) -> None: | ||
where_filter = PydanticWhereFilter(where_sql_template="{{ Dimension('metric_time').date_part('year') }} = '2020'") | ||
|
||
where_filter_spec = WhereSpecFactory( | ||
column_association_resolver=column_association_resolver, | ||
).create_from_where_filter(where_filter) | ||
|
||
assert where_filter_spec.where_sql == "metric_time__extract_year = '2020'" | ||
assert where_filter_spec.linkable_spec_set == LinkableSpecSet( | ||
dimension_specs=(), | ||
time_dimension_specs=( | ||
TimeDimensionSpec( | ||
element_name="metric_time", | ||
entity_links=(), | ||
time_granularity=TimeGranularity.DAY, | ||
date_part=DatePart.YEAR, | ||
), | ||
), | ||
entity_specs=(), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"where_sql", | ||
( | ||
("{{ TimeDimension('metric_time', 'WEEK', date_part_name='year') }} = '2020'"), | ||
("{{ Dimension('metric_time').date_part('year').grain('WEEK') }} = '2020'"), | ||
("{{ Dimension('metric_time').grain('WEEK').date_part('year') }} = '2020'"), | ||
Comment on lines
+129
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope these aren't case-sensitive. I may add some additional tests on after we merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had fixed the case sensitivity here dbt-labs/dbt-semantic-interfaces#207. So, date_part was case-sensitive as of this PR, but that change to DSI fixes it. |
||
), | ||
) | ||
def test_date_part_and_grain_in_filter( # noqa: D | ||
column_association_resolver: ColumnAssociationResolver, where_sql: str | ||
) -> None: | ||
where_filter = PydanticWhereFilter(where_sql_template=where_sql) | ||
|
||
where_filter_spec = WhereSpecFactory( | ||
column_association_resolver=column_association_resolver, | ||
).create_from_where_filter(where_filter) | ||
|
||
assert where_filter_spec.where_sql == "metric_time__extract_year = '2020'" | ||
assert where_filter_spec.linkable_spec_set == LinkableSpecSet( | ||
dimension_specs=(), | ||
time_dimension_specs=( | ||
TimeDimensionSpec( | ||
element_name="metric_time", | ||
entity_links=(), | ||
time_granularity=TimeGranularity.WEEK, | ||
date_part=DatePart.YEAR, | ||
), | ||
), | ||
entity_specs=(), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"where_sql", | ||
( | ||
("{{ TimeDimension('metric_time', 'WEEK', date_part_name='day') }} = '2020'"), | ||
("{{ Dimension('metric_time').date_part('day').grain('WEEK') }} = '2020'"), | ||
("{{ Dimension('metric_time').grain('WEEK').date_part('day') }} = '2020'"), | ||
), | ||
) | ||
def test_date_part_less_than_grain_in_filter( # noqa: D | ||
column_association_resolver: ColumnAssociationResolver, where_sql: str | ||
) -> None: | ||
where_filter = PydanticWhereFilter(where_sql_template=where_sql) | ||
|
||
where_filter_spec = WhereSpecFactory( | ||
column_association_resolver=column_association_resolver, | ||
).create_from_where_filter(where_filter) | ||
|
||
assert where_filter_spec.where_sql == "metric_time__extract_day = '2020'" | ||
assert where_filter_spec.linkable_spec_set == LinkableSpecSet( | ||
dimension_specs=(), | ||
time_dimension_specs=( | ||
TimeDimensionSpec( | ||
element_name="metric_time", | ||
entity_links=(), | ||
time_granularity=TimeGranularity.WEEK, | ||
date_part=DatePart.DAY, | ||
), | ||
), | ||
entity_specs=(), | ||
) | ||
|
||
|
||
def test_entity_in_filter( # noqa: D | ||
column_association_resolver: ColumnAssociationResolver, | ||
) -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so this returns a TimeDimension with DEFAULT_TIME_GRANULARITY if neither grain nor date_part are set. Interesting. I think that makes the most sense with where we're heading with time dimension expressions.