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

Add order_by and limit fields to saved queries #10532

Merged
merged 4 commits into from
Oct 17, 2024
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-20240806-144859.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add `order_by` and `limit` fields to saved queries.
time: 2024-08-06T14:48:59.035914-07:00
custom:
Author: plypaul
Issue: "10531"
2 changes: 2 additions & 0 deletions core/dbt/artifacts/resources/v1/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class QueryParams(dbtClassMixin):
metrics: List[str]
group_by: List[str]
where: Optional[WhereFilterIntersection]
order_by: List[str] = field(default_factory=list)
limit: Optional[int] = None


@dataclass
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ class UnparsedQueryParams(dbtClassMixin):
group_by: List[str] = field(default_factory=list)
# Note: `Union` must be the outermost part of the type annotation for serialization to work properly.
where: Union[str, List[str], None] = None
order_by: List[str] = field(default_factory=list)
limit: Optional[int] = None


@dataclass
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ def _get_query_params(self, unparsed: UnparsedQueryParams) -> QueryParams:
group_by=unparsed.group_by,
metrics=unparsed.metrics,
where=parse_where_filter(unparsed.where),
order_by=unparsed.order_by,
limit=unparsed.limit,
)

def parse_saved_query(self, unparsed: UnparsedSavedQuery) -> None:
Expand Down
2 changes: 1 addition & 1 deletion schemas/dbt/catalog/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dbt_version": {
"type": "string",
"default": "1.9.0a1"
"default": "1.9.0b2"
},
"generated_at": {
"type": "string"
Expand Down
132 changes: 131 additions & 1 deletion schemas/dbt/manifest/v12.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dbt_version": {
"type": "string",
"default": "1.9.0a1"
"default": "1.9.0b2"
},
"generated_at": {
"type": "string"
Expand Down Expand Up @@ -706,6 +706,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -1739,6 +1745,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -2387,6 +2399,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -3172,6 +3190,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -3976,6 +4000,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -5331,6 +5361,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -5979,6 +6015,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -6942,6 +6984,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -10543,6 +10591,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -11576,6 +11630,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -12224,6 +12284,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -13009,6 +13075,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -13813,6 +13885,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -15168,6 +15246,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -15816,6 +15900,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -16779,6 +16869,12 @@
"type": "string"
}
},
"unrendered_config_call_dict": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"relation_name": {
"anyOf": [
{
Expand Down Expand Up @@ -19542,6 +19638,23 @@
"type": "null"
}
]
},
"order_by": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if you saw this / used it but there is a make json_schema target you can use to generate this.

"type": "array",
"items": {
"type": "string"
}
},
"limit": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -21076,6 +21189,23 @@
"type": "null"
}
]
},
"order_by": {
"type": "array",
"items": {
"type": "string"
}
},
"limit": {
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"default": null
}
},
"additionalProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion schemas/dbt/run-results/v6.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dbt_version": {
"type": "string",
"default": "1.9.0a1"
"default": "1.9.0b2"
},
"generated_at": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion schemas/dbt/sources/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dbt_version": {
"type": "string",
"default": "1.9.0a1"
"default": "1.9.0b2"
},
"generated_at": {
"type": "string"
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/saved_queries/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
- "{{ TimeDimension('id__ds', 'DAY') }} <= now()"
- "{{ TimeDimension('id__ds', 'DAY') }} >= '2023-01-01'"
- "{{ Metric('txn_revenue', ['id']) }} > 1"
order_by:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd recommend adding some logic in tests/functional/saved_queries/test_saved_query_parsing.py to test that these values parse as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was added.

- "Metric('simple_metric')"
- "Dimension('id__ds')"
limit: 10
exports:
- name: my_export
config:
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/saved_queries/test_saved_query_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def test_semantic_model_parsing(self, project):
assert len(saved_query.query_params.group_by) == 1
assert len(saved_query.query_params.where.where_filters) == 3
assert len(saved_query.depends_on.nodes) == 1

assert len(saved_query.query_params.order_by) == 2
assert saved_query.query_params.limit is not None

assert saved_query.description == "My SavedQuery Description"
assert len(saved_query.exports) == 1
assert saved_query.exports[0].name == "my_export"
Expand Down
Loading