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

feat: support for custom time granularities #54

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion dbtsl/api/adbc/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
QueryParameters,
validate_query_parameters,
)
from dbtsl.models.time import TimeGranularity


class ADBCProtocol:
Expand All @@ -32,7 +33,7 @@ def _serialize_val(cls, val: Any) -> str:
if isinstance(val, OrderByGroupBy):
d = f'Dimension("{val.name}")'
if val.grain:
grain_str = val.grain.name.lower()
grain_str = val.grain.name.lower() if isinstance(val.grain, TimeGranularity) else val.grain.lower()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@courtneyholcomb This supposes custom grains are not case-sensitive. Is this true?

Choose a reason for hiding this comment

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

They will be lowercase! This is enforced with a validation error in DSI

d += f'.grain("{grain_str}")'
if val.descending:
d += ".descending(True)"
Expand Down
4 changes: 2 additions & 2 deletions dbtsl/api/shared/query_params.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import List, Optional, TypedDict, Union

from dbtsl.models.time import TimeGranularity
from dbtsl.models.time import Grain


@dataclass(frozen=True)
Expand All @@ -20,7 +20,7 @@ class OrderByGroupBy:
"""

name: str
grain: Optional[TimeGranularity]
grain: Optional[Grain]
descending: bool = False


Expand Down
3 changes: 2 additions & 1 deletion dbtsl/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
SavedQueryQueryParams,
SavedQueryWhereParam,
)
from .time import DatePart, TimeGranularity
from .time import DatePart, Grain, TimeGranularity

# Only importing this so it registers aliases
_ = QueryResult
Expand All @@ -37,6 +37,7 @@
"Export",
"ExportConfig",
"ExportDestinationType",
"Grain",
"Measure",
"Metric",
"MetricType",
Expand Down
4 changes: 2 additions & 2 deletions dbtsl/models/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional

from dbtsl.models.base import BaseModel, GraphQLFragmentMixin
from dbtsl.models.time import TimeGranularity
from dbtsl.models.time import Grain


class DimensionType(str, Enum):
Expand All @@ -24,4 +24,4 @@ class Dimension(BaseModel, GraphQLFragmentMixin):
label: Optional[str]
is_partition: bool
expr: Optional[str]
queryable_granularities: List[TimeGranularity]
queryable_granularities: List[Grain]
4 changes: 2 additions & 2 deletions dbtsl/models/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dbtsl.models.dimension import Dimension
from dbtsl.models.entity import Entity
from dbtsl.models.measure import Measure
from dbtsl.models.time import TimeGranularity
from dbtsl.models.time import Grain


class MetricType(str, Enum):
Expand All @@ -29,6 +29,6 @@ class Metric(BaseModel, GraphQLFragmentMixin):
dimensions: List[Dimension]
measures: List[Measure]
entities: List[Entity]
queryable_granularities: List[TimeGranularity]
queryable_granularities: List[Grain]
label: str
requires_metric_time: bool
4 changes: 2 additions & 2 deletions dbtsl/models/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional

from dbtsl.models.base import BaseModel, GraphQLFragmentMixin
from dbtsl.models.time import DatePart, TimeGranularity
from dbtsl.models.time import DatePart, Grain


class ExportDestinationType(str, Enum):
Expand Down Expand Up @@ -42,7 +42,7 @@ class SavedQueryGroupByParam(BaseModel, GraphQLFragmentMixin):
"""The groupBy param of a saved query."""

name: str
grain: Optional[TimeGranularity]
grain: Optional[Grain]
date_part: Optional[DatePart]


Expand Down
5 changes: 5 additions & 0 deletions dbtsl/models/time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Union


class TimeGranularity(str, Enum):
Expand All @@ -17,6 +18,10 @@ class TimeGranularity(str, Enum):
YEAR = "YEAR"


Grain = Union[TimeGranularity, str]
"""Either a standard TimeGranularity or a custom grain."""


class DatePart(str, Enum):
"""Date part."""

Expand Down
4 changes: 4 additions & 0 deletions tests/api/adbc/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_serialize_val_OrderByGroupBy() -> None:
ADBCProtocol._serialize_val(OrderByGroupBy(name="m", grain=TimeGranularity.WEEK, descending=True))
== 'Dimension("m").grain("week").descending(True)'
)
assert (
ADBCProtocol._serialize_val(OrderByGroupBy(name="m", grain="custom_grain"))
== 'Dimension("m").grain("custom_grain")'
)


def test_serialize_query_params_metrics() -> None:
Expand Down