-
Notifications
You must be signed in to change notification settings - Fork 211
Feat(experimental): DBT project conversion #4495
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
Open
erindru
wants to merge
1
commit into
main
Choose a base branch
from
erin/dbt-convert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2015,6 +2015,7 @@ def load_sql_based_model( | |
variables: t.Optional[t.Dict[str, t.Any]] = None, | ||
infer_names: t.Optional[bool] = False, | ||
blueprint_variables: t.Optional[t.Dict[str, t.Any]] = None, | ||
migrated_dbt_project_name: t.Optional[str] = None, | ||
**kwargs: t.Any, | ||
) -> Model: | ||
"""Load a model from a parsed SQLMesh model SQL file. | ||
|
@@ -2186,6 +2187,7 @@ def load_sql_based_model( | |
name, | ||
query_or_seed_insert, | ||
time_column_format=time_column_format, | ||
migrated_dbt_project_name=migrated_dbt_project_name, | ||
**common_kwargs, | ||
) | ||
seed_properties = { | ||
|
@@ -2393,6 +2395,7 @@ def _create_model( | |
signal_definitions: t.Optional[SignalRegistry] = None, | ||
variables: t.Optional[t.Dict[str, t.Any]] = None, | ||
blueprint_variables: t.Optional[t.Dict[str, t.Any]] = None, | ||
migrated_dbt_project_name: t.Optional[str] = None, | ||
**kwargs: t.Any, | ||
) -> Model: | ||
validate_extra_and_required_fields( | ||
|
@@ -2448,13 +2451,42 @@ def _create_model( | |
|
||
if jinja_macros: | ||
jinja_macros = ( | ||
jinja_macros if jinja_macros.trimmed else jinja_macros.trim(jinja_macro_references) | ||
jinja_macros | ||
if jinja_macros.trimmed | ||
else jinja_macros.trim(jinja_macro_references, package=migrated_dbt_project_name) | ||
) | ||
else: | ||
jinja_macros = JinjaMacroRegistry() | ||
|
||
for jinja_macro in jinja_macros.root_macros.values(): | ||
used_variables.update(extract_macro_references_and_variables(jinja_macro.definition)[1]) | ||
# extract {{ var() }} references used in all jinja macro dependencies to check for any variables specific | ||
# to a migrated DBT package and resolve them accordingly | ||
# vars are added into __sqlmesh_vars__ in the Python env so that the native SQLMesh var() function can resolve them | ||
if migrated_dbt_project_name: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be encapsulated into its own function? |
||
# note: JinjaMacroRegistry is trimmed here so "all_macros" should be just be all the macros used by this model | ||
for _, _, jinja_macro in jinja_macros.all_macros: | ||
_, extracted_variable_names = extract_macro_references_and_variables( | ||
jinja_macro.definition | ||
) | ||
used_variables.update(extracted_variable_names) | ||
|
||
variables = variables or {} | ||
if (dbt_package_variables := variables.get(c.MIGRATED_DBT_PACKAGES)) and isinstance( | ||
dbt_package_variables, dict | ||
): | ||
# flatten the nested dict structure from the migrated dbt package variables in the SQLmesh config into __dbt_packages.<package>.<variable> | ||
# to match what extract_macro_references_and_variables() returns. This allows the usage checks in create_python_env() to work | ||
def _flatten(prefix: str, root: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: | ||
acc = {} | ||
for k, v in root.items(): | ||
key_with_prefix = f"{prefix}.{k}" | ||
if isinstance(v, dict): | ||
acc.update(_flatten(key_with_prefix, v)) | ||
else: | ||
acc[key_with_prefix] = v | ||
return acc | ||
|
||
flattened = _flatten(c.MIGRATED_DBT_PACKAGES, dbt_package_variables) | ||
variables.update(flattened) | ||
|
||
model = klass( | ||
name=name, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,6 +491,18 @@ def _merge_filter_validator( | |
|
||
return v.transform(d.replace_merge_table_aliases) | ||
|
||
@field_validator("batch_concurrency", mode="before") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? There's already a validator for this field |
||
def _batch_concurrency_validator( | ||
cls, v: t.Optional[exp.Expression], info: ValidationInfo | ||
) -> int: | ||
if isinstance(v, exp.Literal): | ||
return int( | ||
v.to_py() | ||
) # allow batch_concurrency = 1 to be specified in a Model definition without throwing a Pydantic error | ||
if isinstance(v, int): | ||
return v | ||
return 1 | ||
|
||
@property | ||
def data_hash_values(self) -> t.List[t.Optional[str]]: | ||
return [ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
import jinja2.nodes as j | ||
from sqlglot import exp | ||
import typing as t | ||
import sqlmesh.core.constants as c | ||
from pathlib import Path | ||
|
||
|
||
# jinja transform is a function that takes (current node, previous node, parent node) and returns a new Node or None | ||
# returning None means the current node is removed from the tree | ||
# returning a different Node means the current node is replaced with the new Node | ||
JinjaTransform = t.Callable[[j.Node, t.Optional[j.Node], t.Optional[j.Node]], t.Optional[j.Node]] | ||
SQLGlotTransform = t.Callable[[exp.Expression], t.Optional[exp.Expression]] | ||
|
||
|
||
def _sqlmesh_predefined_macro_variables() -> t.Set[str]: | ||
def _gen() -> t.Iterable[str]: | ||
for suffix in ("dt", "date", "ds", "ts", "tstz", "hour", "epoch", "millis"): | ||
for prefix in ("start", "end", "execution"): | ||
yield f"{prefix}_{suffix}" | ||
|
||
for item in ("runtime_stage", "gateway", "this_model", "this_env", "model_kind_name"): | ||
yield item | ||
|
||
return set(_gen()) | ||
|
||
|
||
SQLMESH_PREDEFINED_MACRO_VARIABLES = _sqlmesh_predefined_macro_variables() | ||
|
||
|
||
def infer_dbt_package_from_path(path: Path) -> t.Optional[str]: | ||
""" | ||
Given a path like "sqlmesh-project/macros/__dbt_packages__/foo/bar.sql" | ||
|
||
Infer that 'foo' is the DBT package | ||
""" | ||
if c.MIGRATED_DBT_PACKAGES in path.parts: | ||
idx = path.parts.index(c.MIGRATED_DBT_PACKAGES) | ||
return path.parts[idx + 1] | ||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead extend the
init
command like we do for dlt generation?