Skip to content

Commit

Permalink
Refactor saved query export parsing to allow user facing schema attr
Browse files Browse the repository at this point in the history
The protocol uses `schema_name` because on the DSI side `schema` causes
an error when in use with pydantic classes. However we want the user
to specify `schema`. This allows for that.
  • Loading branch information
QMalcolm committed Oct 31, 2023
1 parent bd36f8f commit bb5fa88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
21 changes: 19 additions & 2 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from dbt import deprecations
from dbt.node_types import NodeType
from dbt.contracts.graph.saved_queries import Export
from dbt.contracts.graph.semantic_models import (
Defaults,
DimensionValidityParams,
Expand All @@ -20,6 +19,7 @@
from dbt.exceptions import CompilationError, ParsingError, DbtInternalError

from dbt.dataclass_schema import dbtClassMixin, StrEnum, ExtensibleDbtClassMixin, ValidationError
from dbt_semantic_interfaces.type_enums.export_destination_type import ExportDestinationType

from dataclasses import dataclass, field
from datetime import timedelta
Expand Down Expand Up @@ -729,13 +729,30 @@ class UnparsedQueryParams(dbtClassMixin):
where: Optional[Union[str, List[str]]] = None


@dataclass
class UnparsedExportConfig(dbtClassMixin):
"""Nested configuration attributes for exports."""

export_as: ExportDestinationType
schema: Optional[str] = None
alias: Optional[str] = None


@dataclass
class UnparsedExport(dbtClassMixin):
"""Configuration for writing query results to a table."""

name: str
config: UnparsedExportConfig


@dataclass
class UnparsedSavedQuery(dbtClassMixin):
name: str
query_params: UnparsedQueryParams
description: Optional[str] = None
label: Optional[str] = None
exports: List[Export] = field(default_factory=list)
exports: List[UnparsedExport] = field(default_factory=list)
config: Dict[str, Any] = field(default_factory=dict)


Expand Down
16 changes: 14 additions & 2 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
UnparsedDimension,
UnparsedDimensionTypeParams,
UnparsedEntity,
UnparsedExport,
UnparsedExportConfig,
UnparsedExposure,
UnparsedGroup,
UnparsedMeasure,
Expand All @@ -28,7 +30,7 @@
SemanticModel,
SavedQuery,
)
from dbt.contracts.graph.saved_queries import QueryParams
from dbt.contracts.graph.saved_queries import Export, ExportConfig, QueryParams
from dbt.contracts.graph.semantic_layer_common import WhereFilter, WhereFilterIntersection
from dbt.contracts.graph.semantic_models import (
Dimension,
Expand Down Expand Up @@ -667,6 +669,16 @@ def _generate_saved_query_config(

return config

def _get_export_config(self, unparsed: UnparsedExportConfig) -> ExportConfig:
return ExportConfig(
export_as=unparsed.export_as,
schema_name=unparsed.schema,
alias=unparsed.alias,
)

def _get_export(self, unparsed: UnparsedExport) -> Export:
return Export(name=unparsed.name, config=self._get_export_config(unparsed.config))

def _get_query_params(self, unparsed: UnparsedQueryParams) -> QueryParams:
return QueryParams(
group_by=unparsed.group_by,
Expand Down Expand Up @@ -709,7 +721,7 @@ def parse_saved_query(self, unparsed: UnparsedSavedQuery) -> None:
resource_type=NodeType.SavedQuery,
unique_id=unique_id,
query_params=self._get_query_params(unparsed.query_params),
exports=unparsed.exports,
exports=[self._get_export(export) for export in unparsed.exports],
config=config,
unrendered_config=unrendered_config,
group=config.group,
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/saved_queries/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
config:
alias: my_export_alias
export_as: table
schema_name: my_export_schema_name
schema: my_export_schema_name
"""

0 comments on commit bb5fa88

Please sign in to comment.