Skip to content

Commit

Permalink
feat: full query parameters (#30)
Browse files Browse the repository at this point in the history
This commit adds all supported params to our `query` methods in both
GraphQL and ADBC.

Before, we were just using the `metrics` filter.
  • Loading branch information
serramatutu authored Jul 9, 2024
1 parent 4485a78 commit 5a81f70
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Features-20240709-121955.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Features
body: Full support for all query parameters
time: 2024-07-09T12:19:55.649315+02:00
7 changes: 6 additions & 1 deletion dbtsl/api/adbc/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ def _serialize_query_params(params: QueryParameters) -> str:
def append_param_if_exists(p_str: str, p_name: str) -> str:
p_value = params.get(p_name)
if p_value is not None:
p_str += f"{p_name}={json.dumps(p_value)},"
if isinstance(p_value, bool):
dumped = str(p_value)
else:
dumped = json.dumps(p_value)
p_str += f"{p_name}={dumped},"
return p_str

serialized_params = append_param_if_exists(serialized_params, "metrics")
serialized_params = append_param_if_exists(serialized_params, "group_by")
serialized_params = append_param_if_exists(serialized_params, "limit")
serialized_params = append_param_if_exists(serialized_params, "order_by")
serialized_params = append_param_if_exists(serialized_params, "where")
serialized_params = append_param_if_exists(serialized_params, "read_cache")

serialized_params = serialized_params.strip(",")

Expand Down
15 changes: 13 additions & 2 deletions dbtsl/api/graphql/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,21 @@ def get_request_text(self) -> str:
query = """
mutation createQuery(
$environmentId: BigInt!,
$metrics: [MetricInput!]!
$metrics: [MetricInput!]!,
$groupBy: [GroupByInput!]!,
$where: [WhereInput!]!,
$orderBy: [OrderByInput!]!,
$limit: Int,
$readCache: Boolean,
) {
createQuery(
environmentId: $environmentId,
metrics: $metrics
metrics: $metrics,
groupBy: $groupBy,
where: $where,
orderBy: $orderBy,
limit: $limit,
readCache: $readCache,
) {
queryId
}
Expand All @@ -221,6 +231,7 @@ def get_request_variables(self, environment_id: int, **kwargs: QueryParameters)
"where": [{"sql": sql} for sql in kwargs.get("where", [])],
"orderBy": [{"name": o} for o in kwargs.get("order_by", [])],
"limit": kwargs.get("limit", None),
"readCache": kwargs.get("read_cache", True),
}

@override
Expand Down
1 change: 1 addition & 0 deletions dbtsl/api/shared/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ class QueryParameters(TypedDict):
limit: NotRequired[int]
order_by: NotRequired[List[str]]
where: NotRequired[List[str]]
read_cache: NotRequired[bool]
4 changes: 3 additions & 1 deletion tests/api/adbc/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def test_serialize_query_params_complete_query() -> None:
"limit": 1,
"order_by": ["dim_c"],
"where": ['{{ Dimension("metric_time").grain("month") }} >= \'2017-03-09\''],
"read_cache": False,
}
)

expected = (
'metrics=["a", "b"],group_by=["dim_c"],limit=1,order_by=["dim_c"],'
'where=["{{ Dimension(\\"metric_time\\").grain(\\"month\\") }} >= \'2017-03-09\'"]'
'where=["{{ Dimension(\\"metric_time\\").grain(\\"month\\") }} >= \'2017-03-09\'"],'
"read_cache=False"
)
assert params == expected

Expand Down

0 comments on commit 5a81f70

Please sign in to comment.