-
Notifications
You must be signed in to change notification settings - Fork 124
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
Block taking jinja2.runtime.Undefined into DatabricksAdapter #98
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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,51 @@ | ||
import functools | ||
import inspect | ||
from typing import Any, Callable, Type, TypeVar | ||
|
||
from dbt.adapters.base import BaseAdapter | ||
from jinja2.runtime import Undefined | ||
|
||
|
||
A = TypeVar("A", bound=BaseAdapter) | ||
|
||
|
||
def remove_undefined(v: Any) -> Any: | ||
return None if isinstance(v, Undefined) else v | ||
|
||
|
||
def undefined_proof(cls: Type[A]) -> Type[A]: | ||
for name in cls._available_: | ||
func = getattr(cls, name) | ||
if not callable(func): | ||
continue | ||
try: | ||
static_attr = inspect.getattr_static(cls, name) | ||
isstatic = isinstance(static_attr, staticmethod) | ||
isclass = isinstance(static_attr, classmethod) | ||
except AttributeError: | ||
isstatic = False | ||
isclass = False | ||
wrapped_function = _wrap_function(func.__func__ if isclass else func) | ||
setattr( | ||
cls, | ||
name, | ||
( | ||
staticmethod(wrapped_function) | ||
if isstatic | ||
else classmethod(wrapped_function) | ||
if isclass | ||
else wrapped_function | ||
), | ||
) | ||
|
||
return cls | ||
|
||
|
||
def _wrap_function(func: Callable) -> Callable: | ||
@functools.wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
new_args = [remove_undefined(arg) for arg in args] | ||
new_kwargs = {key: remove_undefined(value) for key, value in kwargs.items()} | ||
return func(*new_args, **new_kwargs) | ||
|
||
return wrapper |
This file contains 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 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,67 @@ | ||
import unittest | ||
|
||
from jinja2.runtime import Undefined | ||
|
||
from dbt.adapters.databricks.relation import DatabricksRelation | ||
|
||
|
||
class TestDatabricksRelation(unittest.TestCase): | ||
def test_pre_deserialize(self): | ||
data = { | ||
"quote_policy": {"database": False, "schema": False, "identifier": False}, | ||
"path": { | ||
"database": "some_database", | ||
"schema": "some_schema", | ||
"identifier": "some_table", | ||
}, | ||
"type": None, | ||
} | ||
|
||
relation = DatabricksRelation.from_dict(data) | ||
self.assertEqual(relation.database, "some_database") | ||
self.assertEqual(relation.schema, "some_schema") | ||
self.assertEqual(relation.identifier, "some_table") | ||
|
||
data = { | ||
"quote_policy": {"database": False, "schema": False, "identifier": False}, | ||
"path": { | ||
"database": None, | ||
"schema": "some_schema", | ||
"identifier": "some_table", | ||
}, | ||
"type": None, | ||
} | ||
|
||
relation = DatabricksRelation.from_dict(data) | ||
self.assertIsNone(relation.database) | ||
self.assertEqual(relation.schema, "some_schema") | ||
self.assertEqual(relation.identifier, "some_table") | ||
|
||
data = { | ||
"quote_policy": {"database": False, "schema": False, "identifier": False}, | ||
"path": { | ||
"schema": "some_schema", | ||
"identifier": "some_table", | ||
}, | ||
"type": None, | ||
} | ||
|
||
relation = DatabricksRelation.from_dict(data) | ||
self.assertIsNone(relation.database) | ||
self.assertEqual(relation.schema, "some_schema") | ||
self.assertEqual(relation.identifier, "some_table") | ||
|
||
data = { | ||
"quote_policy": {"database": False, "schema": False, "identifier": False}, | ||
"path": { | ||
"database": Undefined(), | ||
"schema": "some_schema", | ||
"identifier": "some_table", | ||
}, | ||
"type": None, | ||
} | ||
|
||
relation = DatabricksRelation.from_dict(data) | ||
self.assertIsNone(relation.database) | ||
self.assertEqual(relation.schema, "some_schema") | ||
self.assertEqual(relation.identifier, "some_table") |
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.
I am curious when will "database" here be Undefined? Is there a specific configuration that can trigger this?
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.
While parsing profiles, models, etc. and building metadata that happen before setting up
DatabricksRelation
, it could containUndefined
.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.
Also macros for snapshot seems to cause the issue after we change
DatabricksRelation.include_policy.database=True
.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.
Do you think it might be helpful to add a test case here (if we set
DatabricksRelation.include_policy.database=True
)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.
I'm not sure what kind of test in your mind?
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.
For example, construct a test with the database field being jinja Undefined.
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.
Added a test. Thanks!