Skip to content

Commit

Permalink
Update json schemas to allow for list or string input for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tlento committed Oct 10, 2023
1 parent bd01bf0 commit 8a3c6cf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20231009-210737.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Allow metric filters and saved query where properties to accept lists of filter
expressions
time: 2023-10-09T21:07:37.978465-07:00
custom:
Author: tlento
Issue: "147"
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@
],
"type": "object"
},
"filter_schema": {
"$id": "filter_schema",
"oneOf": [
{
"type": "string"
},
{
"items": {
"type": "string"
},
"type": "array"
}
]
},
"is-time-dimension": {
"properties": {
"type": {
Expand Down Expand Up @@ -234,7 +248,7 @@
"type": "integer"
},
"filter": {
"type": "string"
"$ref": "#/definitions/filter_schema"
},
"join_to_timespine": {
"type": "boolean"
Expand All @@ -255,7 +269,7 @@
"type": "string"
},
"filter": {
"type": "string"
"$ref": "#/definitions/filter_schema"
},
"name": {
"type": "string"
Expand All @@ -277,7 +291,7 @@
"type": "string"
},
"filter": {
"type": "string"
"$ref": "#/definitions/filter_schema"
},
"label": {
"type": "string"
Expand Down Expand Up @@ -435,10 +449,7 @@
"type": "string"
},
"where": {
"items": {
"type": "string"
},
"type": "array"
"$ref": "#/definitions/filter_schema"
}
},
"required": [
Expand Down
23 changes: 16 additions & 7 deletions dbt_semantic_interfaces/parsing/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@

time_dimension_type_values = ["TIME", "time"]

filter_schema = {
"$id": "filter_schema",
"oneOf": [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"},
}
]
}

metric_input_measure_schema = {
"$id": "metric_input_measure_schema",
"oneOf": [
Expand All @@ -47,7 +58,7 @@
"type": "object",
"properties": {
"name": {"type": "string"},
"filter": {"type": "string"},
"filter": {"$ref": "filter_schema"},
"alias": {"type": "string"},
"join_to_timespine": {"type": "boolean"},
"fill_nulls_with": {"type": "integer"},
Expand All @@ -62,7 +73,7 @@
"type": "object",
"properties": {
"name": {"type": "string"},
"filter": {"type": "string"},
"filter": {"$ref": "filter_schema"},
"alias": {"type": "string"},
"offset_window": {"type": "string"},
"offset_to_grain": {"type": "string"},
Expand Down Expand Up @@ -218,7 +229,7 @@
},
"type": {"enum": metric_types_enum_values},
"type_params": {"$ref": "metric_type_params"},
"filter": {"type": "string"},
"filter": {"$ref": "filter_schema"},
"description": {"type": "string"},
"label": {"type": "string"},
},
Expand Down Expand Up @@ -292,10 +303,7 @@
"type": "array",
"items": {"type": "string"},
},
"where": {
"type": "array",
"items": {"type": "string"},
},
"where": {"$ref": "filter_schema"},
"label": {"type": "string"},
},
"required": ["name", "metrics"],
Expand Down Expand Up @@ -333,6 +341,7 @@
project_configuration_schema["$id"]: project_configuration_schema,
saved_query_schema["$id"]: saved_query_schema,
# Sub-object schemas
filter_schema["$id"]: filter_schema,
metric_input_measure_schema["$id"]: metric_input_measure_schema,
metric_type_params_schema["$id"]: metric_type_params_schema,
entity_schema["$id"]: entity_schema,
Expand Down
31 changes: 31 additions & 0 deletions tests/parsing/test_metric_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ def test_constraint_metric_parsing() -> None:
)


def test_constraint_list_metric_parsing() -> None:
"""Test for parsing a metric specification with a list of constraints included."""
yaml_contents = textwrap.dedent(
"""\
metric:
name: constraint_test
type: simple
type_params:
measure:
name: input_measure
filter:
- "{{ dimension('some_dimension') }} IN ('value1', 'value2')"
- "1 > 0"
"""
)
file = YamlConfigFile(filepath="inline_for_test", contents=yaml_contents)

build_result = parse_yaml_files_to_semantic_manifest(files=[file, EXAMPLE_PROJECT_CONFIGURATION_YAML_CONFIG_FILE])

assert len(build_result.semantic_manifest.metrics) == 1
metric = build_result.semantic_manifest.metrics[0]
assert metric.name == "constraint_test"
assert metric.type is MetricType.SIMPLE
assert metric.filter == PydanticWhereFilterIntersection(
where_filters=[
PydanticWhereFilter(where_sql_template="{{ dimension('some_dimension') }} IN ('value1', 'value2')"),
PydanticWhereFilter(where_sql_template="1 > 0"),
]
)


def test_derived_metric_input_parsing() -> None:
"""Test for parsing derived metrics with metric_input properties."""
yaml_contents = textwrap.dedent(
Expand Down

0 comments on commit 8a3c6cf

Please sign in to comment.