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

Move SavedQuery data artifact to dbt/artifacts #9460

Merged
merged 11 commits into from
Jan 29, 2024
Merged
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240126-164038.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Move `SavedQuery` data definition to `dbt/artifacts`
time: 2024-01-26T16:40:38.790993-08:00
custom:
Author: QMalcolm
Issue: "9386"
17 changes: 16 additions & 1 deletion core/dbt/artifacts/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from dbt.artifacts.resources.base import BaseResource
from dbt.artifacts.resources.base import BaseResource, GraphResource

# alias to latest resource definitions
from dbt.artifacts.resources.v1.components import DependsOn, NodeVersion, RefArgs
from dbt.artifacts.resources.v1.documentation import Documentation
from dbt.artifacts.resources.v1.macro import Macro, MacroDependsOn, MacroArgument
from dbt.artifacts.resources.v1.docs import Docs
from dbt.artifacts.resources.v1.group import Group
from dbt.artifacts.resources.v1.owner import Owner
from dbt.artifacts.resources.v1.saved_query import (
Export,
ExportConfig,
QueryParams,
SavedQuery,
SavedQueryConfig,
SavedQueryMandatory,
)
from dbt.artifacts.resources.v1.semantic_layer_components import (
FileSlice,
SourceFileMetadata,
WhereFilter,
WhereFilterIntersection,
)
6 changes: 6 additions & 0 deletions core/dbt/artifacts/resources/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from dbt_common.dataclass_schema import dbtClassMixin
from dbt_common.contracts.util import Replaceable
from typing import List

from dbt.artifacts.resources.types import NodeType

Expand All @@ -13,3 +14,8 @@ class BaseResource(dbtClassMixin, Replaceable):
path: str
original_file_path: str
unique_id: str


@dataclass
class GraphResource(BaseResource):
fqn: List[str]
37 changes: 37 additions & 0 deletions core/dbt/artifacts/resources/v1/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from dataclasses import dataclass, field
from dbt.artifacts.resources.v1.macro import MacroDependsOn
from dbt_common.dataclass_schema import dbtClassMixin
from typing import Dict, List, Optional, Union


NodeVersion = Union[str, float]


@dataclass
class DependsOn(MacroDependsOn):
nodes: List[str] = field(default_factory=list)

def add_node(self, value: str):
if value not in self.nodes:
self.nodes.append(value)


@dataclass
class RefArgs(dbtClassMixin):
name: str
package: Optional[str] = None
version: Optional[NodeVersion] = None

@property
def positional_args(self) -> List[str]:
if self.package:
return [self.package, self.name]
else:
return [self.name]

@property
def keyword_args(self) -> Dict[str, Optional[NodeVersion]]:
if self.version:
return {"version": self.version}

Check warning on line 35 in core/dbt/artifacts/resources/v1/components.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/artifacts/resources/v1/components.py#L34-L35

Added lines #L34 - L35 were not covered by tests
else:
return {}

Check warning on line 37 in core/dbt/artifacts/resources/v1/components.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/artifacts/resources/v1/components.py#L37

Added line #L37 was not covered by tests
90 changes: 90 additions & 0 deletions core/dbt/artifacts/resources/v1/saved_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from __future__ import annotations
import time

from dataclasses import dataclass, field
from dbt.artifacts.resources.base import GraphResource
from dbt.artifacts.resources.v1.components import DependsOn, RefArgs
from dbt.artifacts.resources.v1.semantic_layer_components import (
SourceFileMetadata,
WhereFilterIntersection,
)
from dbt_common.contracts.config.base import BaseConfig, CompareBehavior, MergeBehavior
from dbt_common.dataclass_schema import dbtClassMixin
from dbt_semantic_interfaces.type_enums.export_destination_type import ExportDestinationType
from typing import Any, Dict, List, Optional


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

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


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

name: str
config: ExportConfig


@dataclass
class QueryParams(dbtClassMixin):
"""The query parameters for the saved query"""

metrics: List[str]
group_by: List[str]
where: Optional[WhereFilterIntersection]


@dataclass
class SavedQueryConfig(BaseConfig):
"""Where config options for SavedQueries are stored.

This class is much like many other node config classes. It's likely that
this class will expand in the direction of what's in the `NodeAndTestConfig`
class. It might make sense to clean the various *Config classes into one at
some point.
"""

enabled: bool = True
group: Optional[str] = field(
default=None,
metadata=CompareBehavior.Exclude.meta(),
)
meta: Dict[str, Any] = field(
default_factory=dict,
metadata=MergeBehavior.Update.meta(),
)
export_as: Optional[ExportDestinationType] = None
schema: Optional[str] = None


@dataclass
class SavedQueryMandatory(GraphResource):
query_params: QueryParams
exports: List[Export]


@dataclass
class SavedQuery(SavedQueryMandatory):
description: Optional[str] = None
label: Optional[str] = None
metadata: Optional[SourceFileMetadata] = None
config: SavedQueryConfig = field(default_factory=SavedQueryConfig)
unrendered_config: Dict[str, Any] = field(default_factory=dict)
group: Optional[str] = None
depends_on: DependsOn = field(default_factory=DependsOn)
created_at: float = field(default_factory=lambda: time.time())
refs: List[RefArgs] = field(default_factory=list)

@property
def metrics(self) -> List[str]:
return self.query_params.metrics

@property
def depends_on_nodes(self):
return self.depends_on.nodes
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ class WhereFilterIntersection(dbtClassMixin):
@property
def filter_expression_parameter_sets(self) -> Sequence[Tuple[str, FilterCallParameterSets]]:
raise NotImplementedError


@dataclass
class FileSlice(dbtClassMixin):
"""Provides file slice level context about what something was created from.

Implementation of the dbt-semantic-interfaces `FileSlice` protocol
"""

filename: str
content: str
start_line_number: int
end_line_number: int


@dataclass
class SourceFileMetadata(dbtClassMixin):
"""Provides file context about what something was created from.

Implementation of the dbt-semantic-interfaces `Metadata` protocol
"""

repo_file_path: str
file_slice: FileSlice
3 changes: 1 addition & 2 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing_extensions import Protocol

from dbt.adapters.base.column import Column
from dbt.artifacts.resources import NodeVersion, RefArgs
from dbt_common.clients.jinja import MacroProtocol
from dbt.adapters.factory import get_adapter, get_adapter_package_names, get_adapter_type_names
from dbt_common.clients import agate_helper
Expand All @@ -38,13 +39,11 @@
SourceDefinition,
Resource,
ManifestNode,
RefArgs,
AccessType,
SemanticModel,
UnitTestNode,
)
from dbt.contracts.graph.metrics import MetricReference, ResolvedMetricReference
from dbt.contracts.graph.unparsed import NodeVersion
from dbt_common.events.functions import get_metadata_vars
from dbt_common.exceptions import (
DbtInternalError,
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
UnitTestDefinition,
UnitTestFileFixture,
)
from dbt.contracts.graph.unparsed import SourcePatch, NodeVersion, UnparsedVersion
from dbt.contracts.graph.unparsed import SourcePatch, UnparsedVersion

# to preserve import paths
from dbt.artifacts.resources import NodeVersion
from dbt.artifacts.schemas.manifest import WritableManifest, ManifestMetadata, UniqueID
from dbt.contracts.files import (
SourceFile,
Expand Down
25 changes: 1 addition & 24 deletions core/dbt/contracts/graph/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, List, Optional, Dict, Union, Type
from typing_extensions import Annotated

from dbt.artifacts.resources import SavedQueryConfig
from dbt_common.contracts.config.base import BaseConfig, MergeBehavior, CompareBehavior
from dbt_common.contracts.config.materialization import OnConfigurationChangeOption
from dbt_common.contracts.config.metadata import Metadata, ShowBehavior
Expand All @@ -14,7 +15,6 @@
from dbt.contracts.util import Replaceable, list_str
from dbt import hooks
from dbt.node_types import NodeType, AccessType
from dbt_semantic_interfaces.type_enums.export_destination_type import ExportDestinationType
from mashumaro.jsonschema.annotations import Pattern


Expand Down Expand Up @@ -62,29 +62,6 @@ class SemanticModelConfig(BaseConfig):
)


@dataclass
class SavedQueryConfig(BaseConfig):
"""Where config options for SavedQueries are stored.

This class is much like many other node config classes. It's likely that
this class will expand in the direction of what's in the `NodeAndTestConfig`
class. It might make sense to clean the various *Config classes into one at
some point.
"""

enabled: bool = True
group: Optional[str] = field(
default=None,
metadata=CompareBehavior.Exclude.meta(),
)
meta: Dict[str, Any] = field(
default_factory=dict,
metadata=MergeBehavior.Update.meta(),
)
export_as: Optional[ExportDestinationType] = None
schema: Optional[str] = None


@dataclass
class MetricConfig(BaseConfig):
enabled: bool = True
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/node_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from typing import Optional, List

from dbt.contracts.graph.unparsed import NodeVersion
from dbt.artifacts.resources import NodeVersion
from dbt.node_types import NodeType, AccessType


Expand Down
Loading
Loading