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

Define Macro and Group resources in dbt/artifacts #9439

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/Under the Hood-20240125-095453.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Define Macro resource in dbt/artifacts
time: 2024-01-25T09:54:53.974332-05:00
custom:
Author: michelleark
Issue: "9382"
2 changes: 2 additions & 0 deletions core/dbt/artifacts/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

# alias to latest resource definitions
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
10 changes: 10 additions & 0 deletions core/dbt/artifacts/resources/v1/docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dataclasses import dataclass
from dbt_common.dataclass_schema import dbtClassMixin
from dbt_common.contracts.util import Replaceable
from typing import Optional


@dataclass
class Docs(dbtClassMixin, Replaceable):
show: bool = True
node_color: Optional[str] = None
40 changes: 40 additions & 0 deletions core/dbt/artifacts/resources/v1/macro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from dataclasses import dataclass, field
import time
from typing import Literal, List, Dict, Optional, Any

from dbt_common.contracts.util import Replaceable
from dbt_common.dataclass_schema import dbtClassMixin
from dbt.artifacts.resources.base import BaseArtifactNode
from dbt.artifacts.resources.types import NodeType, ModelLanguage
from dbt.artifacts.resources.v1.docs import Docs


@dataclass
class MacroArgument(dbtClassMixin):
name: str
type: Optional[str] = None
description: str = ""


@dataclass
class MacroDependsOn(dbtClassMixin, Replaceable):
macros: List[str] = field(default_factory=list)

# 'in' on lists is O(n) so this is O(n^2) for # of macros
def add_macro(self, value: str):
Copy link
Contributor Author

@MichelleArk MichelleArk Jan 25, 2024

Choose a reason for hiding this comment

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

Went back and forth on whether this method belongs in dbt/artifacts given it is mutating / not read-only. An alternative would be to define the method more directly on the internal Macro node class.

Decided to leave it as-is for now because DependsOn inherits from MacroDependsOn. We should revisit a design once we move DependsOn though.

Copy link
Contributor

Choose a reason for hiding this comment

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

For those looking in @MichelleArk and I have talked about this a bit in DMs. In my PR #9460 I plan on moving DependsOn in full over to artifacts. We plan in the future to revist the design of both MacroDependsOn and DependsOn to perhaps simplify and improve them.

if value not in self.macros:
self.macros.append(value)


@dataclass
class Macro(BaseArtifactNode):
macro_sql: str
resource_type: Literal[NodeType.Macro]
depends_on: MacroDependsOn = field(default_factory=MacroDependsOn)
description: str = ""
meta: Dict[str, Any] = field(default_factory=dict)
docs: Docs = field(default_factory=Docs)
patch_path: Optional[str] = None
arguments: List[MacroArgument] = field(default_factory=list)
created_at: float = field(default_factory=lambda: time.time())
supported_languages: Optional[List[ModelLanguage]] = None
39 changes: 10 additions & 29 deletions core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
)
from dbt.contracts.graph.unparsed import (
ConstantPropertyInput,
Docs,
ExposureType,
ExternalTable,
FreshnessThreshold,
HasYamlMetadata,
MacroArgument,
MaturityType,
Owner,
Quoting,
Expand Down Expand Up @@ -61,7 +59,6 @@
from dbt_common.events.contextvars import set_log_contextvars
from dbt.flags import get_flags
from dbt.node_types import (
ModelLanguage,
NodeType,
AccessType,
REFABLE_NODE_TYPES,
Expand Down Expand Up @@ -97,7 +94,14 @@
SavedQueryConfig,
)

from dbt.artifacts.resources import BaseArtifactNode, Documentation as DocumentationContract
from dbt.artifacts.resources import (
BaseArtifactNode,
Docs,
MacroDependsOn,
MacroArgument,
Documentation as DocumentationResource,
Macro as MacroResource,
)


# =====================================================================
Expand Down Expand Up @@ -240,18 +244,6 @@ def quoting_dict(self) -> Dict[str, bool]:
return {}


@dataclass
class MacroDependsOn(dbtClassMixin, Replaceable):
"""Used only in the Macro class"""

macros: List[str] = field(default_factory=list)

# 'in' on lists is O(n) so this is O(n^2) for # of macros
def add_macro(self, value: str):
if value not in self.macros:
self.macros.append(value)


@dataclass
class DeferRelation(HasRelationMetadata):
alias: str
Expand Down Expand Up @@ -1149,18 +1141,7 @@ class SnapshotNode(CompiledNode):


@dataclass
class Macro(BaseNode):
macro_sql: str
resource_type: Literal[NodeType.Macro]
depends_on: MacroDependsOn = field(default_factory=MacroDependsOn)
description: str = ""
meta: Dict[str, Any] = field(default_factory=dict)
docs: Docs = field(default_factory=Docs)
patch_path: Optional[str] = None
arguments: List[MacroArgument] = field(default_factory=list)
created_at: float = field(default_factory=lambda: time.time())
supported_languages: Optional[List[ModelLanguage]] = None

class Macro(MacroResource, BaseNode):
def same_contents(self, other: Optional["Macro"]) -> bool:
if other is None:
return False
Expand All @@ -1179,7 +1160,7 @@ def depends_on_macros(self):


@dataclass
class Documentation(DocumentationContract, BaseNode):
class Documentation(DocumentationResource, BaseNode):
@property
def search_name(self):
return self.name
Expand Down
14 changes: 1 addition & 13 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from dbt.exceptions import ParsingError

from dbt_semantic_interfaces.type_enums import ConversionCalculationType
from dbt.artifacts.resources import Docs, MacroArgument

from dataclasses import dataclass, field
from datetime import timedelta
Expand Down Expand Up @@ -83,12 +84,6 @@ class UnparsedRunHook(UnparsedNode):
index: Optional[int] = None


@dataclass
class Docs(dbtClassMixin, Replaceable):
show: bool = True
node_color: Optional[str] = None


@dataclass
class HasColumnProps(AdditionalPropertiesMixin, ExtensibleDbtClassMixin, Replaceable):
name: str
Expand Down Expand Up @@ -266,13 +261,6 @@ def get_tests_for_version(self, version: NodeVersion) -> List[TestDef]:
)


@dataclass
class MacroArgument(dbtClassMixin):
name: str
type: Optional[str] = None
description: str = ""


@dataclass
class UnparsedMacroUpdate(HasConfig, HasColumnProps, HasYamlMetadata):
arguments: List[MacroArgument] = field(default_factory=list)
Expand Down