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

Allow metrics in filters #274

Merged
merged 5 commits into from
Mar 19, 2024
Merged

Allow metrics in filters #274

merged 5 commits into from
Mar 19, 2024

Conversation

courtneyholcomb
Copy link
Contributor

@courtneyholcomb courtneyholcomb commented Mar 16, 2024

Resolves #273
Resolves SL-1849

Description

Allow users to reference metrics in where filters for metrics, measures, and saved queries. This uses syntax like:
{{ Metric('metric_name', group_by=['entity_name', 'dimension_name']) }} = 10
This unlocks new types of metrics that users have been asking for. Some examples can be found in the linked issue and in this design doc.

Checklist

@cla-bot cla-bot bot added the cla:yes label Mar 16, 2024
Copy link

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

Copy link

linear bot commented Mar 18, 2024

@courtneyholcomb courtneyholcomb marked this pull request as ready for review March 18, 2024 20:15
Copy link
Collaborator

@tlento tlento left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! If you haven't already I think we'll want to do some local testing against the jaffle project to make sure parsing the metric filter is working as expected with the dbt integration for 1.8. It should - as far as I know dbt's parser just passes the where filter expressions into these components - but I've never actually tried it.

Comment on lines +46 to +47
def descending(self, _is_descending: bool) -> QueryInterfaceMetric: # noqa: D
raise InvalidQuerySyntax("descending is invalid in the where parameter and filter spec")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see why this has to be here. We really need to carve out some time to clean up the underlying protocol specs so we don't have these runtime exceptions floating around all over the place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely agree!

@@ -108,6 +108,8 @@ def create_entity(entity_name: str, entity_path: Sequence[str] = ()) -> EntityCa
@staticmethod
def create_metric(metric_name: str, group_by: Sequence[str] = ()) -> MetricCallParameterSet:
"""Gets called by Jinja when rendering {{ Metric(...) }}."""
if not group_by:
raise ParseWhereFilterException("`group_by` parameter is required for Metric in where filter.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a little more descriptive as to why? Maybe something like:

"`group_by` parameter is required for filtering on Metrics, since a filter on a Metric does not make sense for queries where there are no sub-groups against which to apply the filter expression."

I don't know if normal people would understand that, but maybe it's better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

]

manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()])
manifest_validator.validate_semantic_manifest(manifest),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, so this should also run our internal check for missing group-bys and will throw a validation error if someone messes that up, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't quite do that, but now I added some more detail so that it will do that.

@courtneyholcomb courtneyholcomb merged commit e4f029b into main Mar 19, 2024
18 checks passed
@courtneyholcomb courtneyholcomb deleted the court/metric-filter branch March 19, 2024 22:44
courtneyholcomb added a commit that referenced this pull request Mar 19, 2024
Resolves #273
Resolves SL-1849

<!---
Include the number of the issue addressed by this PR above if
applicable.
  PRs for code changes without an associated issue *will not be merged*.
  See CONTRIBUTING.md for more information.
-->

### Description
Allow users to reference metrics in where filters for metrics, measures,
and saved queries. This uses syntax like:
`{{ Metric('metric_name', group_by=['entity_name', 'dimension_name']) }}
= 10`
This unlocks new types of metrics that users have been asking for. Some
examples can be found in the linked issue and in [this design
doc](https://www.notion.so/dbtlabs/Metrics-as-Dimensions-55718e9516a7462787ffd6e3e8c1237e?pvs=4).

### Checklist

- [x] I have read [the contributing
guide](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md)
and understand what's expected of me
- [x] I have signed the
[CLA](https://docs.getdbt.com/docs/contributor-license-agreements)
- [x] This PR includes tests, or tests are not required/relevant for
this PR
- [x] I have run `changie new` to [create a changelog
entry](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md#adding-a-changelog-entry)
courtneyholcomb added a commit that referenced this pull request Mar 21, 2024
### Description
Backporting #274
into `0.5.latest`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Support referencing a metric in where filter
2 participants