-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Generalize constraint warnings #7250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
ce9f341
CT-1922: Rough in functionality for parsing model level constraints
peterallenwebb f3bd5f3
CT-1922: (Almost) complete support for model level constraints
peterallenwebb 5699852
CT-1922: Fix typo affecting correct model constraint parsing.
peterallenwebb d9197c7
CT-1922: Rework base class for model tests for greater simplicity
peterallenwebb 02c94ed
CT-1922: Rough in functionality for parsing model level constraints
peterallenwebb 7b85bf6
CT-1922: Revise unit tests for new model-level constraints property
peterallenwebb bf614ba
CT-1922: (Almost) complete support for model level constraints
peterallenwebb 2198c4c
first pass
emmyoop edda34e
implement in core
emmyoop a920bfe
add proto
emmyoop 620023f
WIP
emmyoop 27d074e
resolve errors in columns_spec_ddl
emmyoop a94ca62
changelog
emmyoop 24c5a56
update comment
emmyoop 9daada8
move logic over to python
emmyoop 253dc9f
rename and use enum
emmyoop cf5a6ee
update default constraint_support dict
emmyoop dd49aa2
generate new proto definition after conflicts
emmyoop ee00d36
reorganize code and break warnings into each constraint
emmyoop 6f18100
fix postgres constraint support
emmyoop 47d6bdc
remove breakpoint
emmyoop 51e71b5
convert constraint support to constant
emmyoop d9603db
update postgres
emmyoop 42d90d2
add to export
emmyoop 2d2f277
add to export
emmyoop ad0b4a4
regen proto types file
emmyoop 35d3307
standardize names
emmyoop 146ebb3
put back mypy error
emmyoop 3058822
more naming + add back comma
emmyoop 3e3b8a3
add constraint support to model level constraints
emmyoop 4bed983
update event message and method signature
emmyoop 15101f1
rename method
emmyoop 4e2c174
CT-1922: Rough in functionality for parsing model level constraints
peterallenwebb 432ef9a
CT-1922: Revise unit tests for new model-level constraints property
peterallenwebb f1d4c18
CT-1922: (Almost) complete support for model level constraints
peterallenwebb 864554d
CT-1922: Fix typo affecting correct model constraint parsing.
peterallenwebb ae94f48
CT-1922: Improve whitespace handling
peterallenwebb de4e163
CT-1922: Render raw constraints to constraint list directly
peterallenwebb 9c79a0d
make method return consistent
emmyoop 276d0c3
regenerate proto defn
emmyoop 044140b
update evvent test
emmyoop bae80b0
add some code cleanup
emmyoop 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| kind: Under the Hood | ||
| body: Generalize constraint compatibility warnings | ||
| time: 2023-03-31T13:21:19.507995-05:00 | ||
| custom: | ||
| Author: emmyoop | ||
| Issue: "7067" |
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 |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| # these are all just exports, #noqa them so flake8 will be happy | ||
|
|
||
| # TODO: Should we still include this in the `adapters` namespace? | ||
| from dbt.contracts.connection import Credentials # noqa | ||
| from dbt.adapters.base.meta import available # noqa | ||
| from dbt.adapters.base.connections import BaseConnectionManager # noqa | ||
| from dbt.adapters.base.relation import ( # noqa | ||
| from dbt.contracts.connection import Credentials # noqa: F401 | ||
| from dbt.adapters.base.meta import available # noqa: F401 | ||
| from dbt.adapters.base.connections import BaseConnectionManager # noqa: F401 | ||
| from dbt.adapters.base.relation import ( # noqa: F401 | ||
| BaseRelation, | ||
| RelationType, | ||
| SchemaSearchMap, | ||
| ) | ||
| from dbt.adapters.base.column import Column # noqa | ||
| from dbt.adapters.base.impl import AdapterConfig, BaseAdapter, PythonJobHelper # noqa | ||
| from dbt.adapters.base.plugin import AdapterPlugin # noqa | ||
| from dbt.adapters.base.column import Column # noqa: F401 | ||
| from dbt.adapters.base.impl import ( # noqa: F401 | ||
| AdapterConfig, | ||
| BaseAdapter, | ||
| PythonJobHelper, | ||
| ConstraintSupport, | ||
| ) | ||
| from dbt.adapters.base.plugin import AdapterPlugin # noqa: F401 |
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| from concurrent.futures import as_completed, Future | ||
| from contextlib import contextmanager | ||
| from datetime import datetime | ||
| from enum import Enum | ||
| import time | ||
| from itertools import chain | ||
| from typing import ( | ||
|
|
@@ -16,6 +17,7 @@ | |
| Set, | ||
| Tuple, | ||
| Type, | ||
| Union, | ||
| ) | ||
|
|
||
| from dbt.contracts.graph.nodes import ColumnLevelConstraint, ConstraintType, ModelLevelConstraint | ||
|
|
@@ -53,6 +55,8 @@ | |
| CodeExecution, | ||
| CodeExecutionStatus, | ||
| CatalogGenerationError, | ||
| ConstraintNotSupported, | ||
| ConstraintNotEnforced, | ||
| ) | ||
| from dbt.utils import filter_null_values, executor, cast_to_str, AttrDict | ||
|
|
||
|
|
@@ -73,6 +77,12 @@ | |
| FRESHNESS_MACRO_NAME = "collect_freshness" | ||
|
|
||
|
|
||
| class ConstraintSupport(str, Enum): | ||
| ENFORCED = "enforced" | ||
| NOT_ENFORCED = "not_enforced" | ||
| NOT_SUPPORTED = "not_supported" | ||
|
|
||
|
|
||
| def _expect_row_value(key: str, row: agate.Row): | ||
| if key not in row.keys(): | ||
| raise DbtInternalError( | ||
|
|
@@ -204,6 +214,14 @@ class BaseAdapter(metaclass=AdapterMeta): | |
| # for use in materializations | ||
| AdapterSpecificConfigs: Type[AdapterConfig] = AdapterConfig | ||
|
|
||
| CONSTRAINT_SUPPORT = { | ||
| ConstraintType.check: ConstraintSupport.NOT_SUPPORTED, | ||
| ConstraintType.not_null: ConstraintSupport.ENFORCED, | ||
| ConstraintType.unique: ConstraintSupport.NOT_ENFORCED, | ||
| ConstraintType.primary_key: ConstraintSupport.NOT_ENFORCED, | ||
| ConstraintType.foreign_key: ConstraintSupport.ENFORCED, | ||
| } | ||
|
|
||
| def __init__(self, config): | ||
| self.config = config | ||
| self.cache = RelationsCache() | ||
|
|
@@ -1273,14 +1291,8 @@ def _parse_column_constraint(cls, raw_constraint: Dict[str, Any]) -> ColumnLevel | |
| except Exception: | ||
| raise DbtValidationError(f"Could not parse constraint: {raw_constraint}") | ||
|
|
||
| @available | ||
| @classmethod | ||
| def render_raw_column_constraint(cls, raw_constraint: Dict[str, Any]) -> str: | ||
| constraint = cls._parse_column_constraint(raw_constraint) | ||
| return cls.render_column_constraint(constraint) | ||
|
|
||
| @classmethod | ||
| def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> str: | ||
| def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> Optional[str]: | ||
| """Render the given constraint as DDL text. Should be overriden by adapters which need custom constraint | ||
| rendering.""" | ||
| if constraint.type == ConstraintType.check and constraint.expression: | ||
|
|
@@ -1296,7 +1308,46 @@ def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> str: | |
| elif constraint.type == ConstraintType.custom and constraint.expression: | ||
| return constraint.expression | ||
| else: | ||
| return "" | ||
| return None | ||
|
|
||
| @available | ||
| @classmethod | ||
| def render_raw_columns_constraints(cls, raw_columns: Dict[str, Dict[str, Any]]) -> List: | ||
|
Contributor
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. I'm so selfishly delighted to see this now as a Python method and the types for this call stack standardized 🥂 |
||
| rendered_column_constraints = [] | ||
|
|
||
| for v in raw_columns.values(): | ||
| rendered_column_constraint = [f"{v['name']} {v['data_type']}"] | ||
| for con in v.get("constraints", None): | ||
| constraint = cls._parse_column_constraint(con) | ||
| c = cls.process_parsed_constraint(constraint, cls.render_column_constraint) | ||
| if c is not None: | ||
| rendered_column_constraint.append(c) | ||
| rendered_column_constraints.append(" ".join(rendered_column_constraint)) | ||
|
|
||
| return rendered_column_constraints | ||
|
|
||
| @classmethod | ||
| def process_parsed_constraint( | ||
| cls, parsed_constraint: Union[ColumnLevelConstraint, ModelLevelConstraint], render_func | ||
emmyoop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> Optional[str]: | ||
| if ( | ||
| parsed_constraint.warn_unsupported | ||
| and cls.CONSTRAINT_SUPPORT[parsed_constraint.type] == ConstraintSupport.NOT_SUPPORTED | ||
| ): | ||
| warn_or_error( | ||
| ConstraintNotSupported(constraint=parsed_constraint.type.value, adapter=cls.type()) | ||
| ) | ||
| if ( | ||
| parsed_constraint.warn_unenforced | ||
| and cls.CONSTRAINT_SUPPORT[parsed_constraint.type] == ConstraintSupport.NOT_ENFORCED | ||
| ): | ||
| warn_or_error( | ||
| ConstraintNotEnforced(constraint=parsed_constraint.type.value, adapter=cls.type()) | ||
| ) | ||
| if cls.CONSTRAINT_SUPPORT[parsed_constraint.type] != ConstraintSupport.NOT_SUPPORTED: | ||
| return render_func(parsed_constraint) | ||
|
|
||
| return None | ||
|
|
||
| @classmethod | ||
| def _parse_model_constraint(cls, raw_constraint: Dict[str, Any]) -> ModelLevelConstraint: | ||
|
|
@@ -1315,7 +1366,7 @@ def render_raw_model_constraints(cls, raw_constraints: List[Dict[str, Any]]) -> | |
| @classmethod | ||
| def render_raw_model_constraint(cls, raw_constraint: Dict[str, Any]) -> Optional[str]: | ||
| constraint = cls._parse_model_constraint(raw_constraint) | ||
| return cls.render_model_constraint(constraint) | ||
| return cls.process_parsed_constraint(constraint, cls.render_model_constraint) | ||
|
|
||
| @classmethod | ||
| def render_model_constraint(cls, constraint: ModelLevelConstraint) -> Optional[str]: | ||
|
|
||
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
Large diffs are not rendered by default.
Oops, something went wrong.
mikealfare marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.