Skip to content

Commit

Permalink
updated callsites to pass in custom grain names
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDee committed Nov 5, 2024
1 parent 83a8111 commit 56a8fae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
7 changes: 4 additions & 3 deletions dbt_semantic_interfaces/parsing/text_input/ti_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ def __post_init__(self) -> None: # noqa: D105
else:
assert_values_exhausted(item_type)

structured_item_name = StructuredDunderedName.parse_name(self.item_name)

# Check that metrics do not have an entity prefix or entity path.
if item_type is QueryItemType.METRIC:
if len(self.entity_path) > 0:
raise InvalidQuerySyntax("The entity path should not be specified for a metric.")
if len(structured_item_name.entity_links) > 0:
if (
len(StructuredDunderedName.parse_name(name=self.item_name, custom_granularity_names=()).entity_links)
> 0
):
raise InvalidQuerySyntax("The name of the metric should not have entity links.")
# Check that dimensions / time dimensions have a valid date part.
elif item_type is QueryItemType.DIMENSION or item_type is QueryItemType.TIME_DIMENSION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ParseWhereFilterException,
TimeDimensionCallParameterSet,
)
from dbt_semantic_interfaces.naming.dundered import DunderedNameFormatter
from dbt_semantic_interfaces.naming.dundered import StructuredDunderedName
from dbt_semantic_interfaces.naming.keywords import is_metric_time_name
from dbt_semantic_interfaces.references import (
DimensionReference,
Expand Down Expand Up @@ -49,6 +49,7 @@ def create_time_dimension(
time_granularity_name: Optional[str] = None,
entity_path: Sequence[str] = (),
date_part_name: Optional[str] = None,
custom_granularity_names: Sequence[str] = (),
) -> TimeDimensionCallParameterSet:
"""Gets called by Jinja when rendering {{ TimeDimension(...) }}.
Expand All @@ -65,14 +66,14 @@ def create_time_dimension(
for parsing where filters. When we solve the problems with our current where filter spec this will
persist as a backwards compatibility model, but nothing more.
"""
group_by_item_name = DunderedNameFormatter.parse_name(time_dimension_name)
group_by_item_name = StructuredDunderedName.parse_name(
name=time_dimension_name, custom_granularity_names=custom_granularity_names
)
if len(group_by_item_name.entity_links) != 1 and not is_metric_time_name(group_by_item_name.element_name):
raise ParseWhereFilterException(
ParameterSetFactory._exception_message_for_incorrect_format(time_dimension_name)
)
grain_parsed_from_name = (
group_by_item_name.time_granularity.value if group_by_item_name.time_granularity else None
)
grain_parsed_from_name = group_by_item_name.time_granularity
inputs_are_mismatched = (
grain_parsed_from_name is not None
and time_granularity_name is not None
Expand Down Expand Up @@ -101,7 +102,7 @@ def create_time_dimension(
@staticmethod
def create_dimension(dimension_name: str, entity_path: Sequence[str] = ()) -> DimensionCallParameterSet:
"""Gets called by Jinja when rendering {{ Dimension(...) }}."""
group_by_item_name = DunderedNameFormatter.parse_name(dimension_name)
group_by_item_name = StructuredDunderedName.parse_name(name=dimension_name, custom_granularity_names=())

if len(group_by_item_name.entity_links) != 1 and not is_metric_time_name(group_by_item_name.element_name):
raise ParseWhereFilterException(ParameterSetFactory._exception_message_for_incorrect_format(dimension_name))
Expand All @@ -116,7 +117,7 @@ def create_dimension(dimension_name: str, entity_path: Sequence[str] = ()) -> Di
@staticmethod
def create_entity(entity_name: str, entity_path: Sequence[str] = ()) -> EntityCallParameterSet:
"""Gets called by Jinja when rendering {{ Entity(...) }}."""
structured_dundered_name = DunderedNameFormatter.parse_name(entity_name)
structured_dundered_name = StructuredDunderedName.parse_name(name=entity_name, custom_granularity_names=())
if structured_dundered_name.time_granularity is not None:
raise ParseWhereFilterException(
f"Name is in an incorrect format: {repr(entity_name)}. " f"It should not contain a time grain suffix."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def parse_item_descriptions(where_sql_template: str) -> Sequence[ObjectBuilderIt
raise ParseWhereFilterException(f"Error while parsing Jinja template:\n{where_sql_template}") from e

@staticmethod
def parse_call_parameter_sets(where_sql_template: str) -> FilterCallParameterSets:
def parse_call_parameter_sets(
where_sql_template: str, custom_granularity_names: Sequence[str]
) -> FilterCallParameterSets:
"""Return the result of extracting the semantic objects referenced in the where SQL template string."""
descriptions = WhereFilterParser.parse_item_descriptions(where_sql_template)

Expand All @@ -63,6 +65,7 @@ def parse_call_parameter_sets(where_sql_template: str) -> FilterCallParameterSet
time_granularity_name=description.time_granularity_name,
entity_path=description.entity_path,
date_part_name=description.date_part_name,
custom_granularity_names=custom_granularity_names,
)
)
else:
Expand All @@ -79,6 +82,7 @@ def parse_call_parameter_sets(where_sql_template: str) -> FilterCallParameterSet
time_granularity_name=description.time_granularity_name,
entity_path=description.entity_path,
date_part_name=description.date_part_name,
custom_granularity_names=custom_granularity_names,
)
)
elif item_type is QueryItemType.ENTITY:
Expand Down

0 comments on commit 56a8fae

Please sign in to comment.