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

ct-2198: Unify constraints and check_constraints fields #7130

Merged
merged 22 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4d47b27
ct-2198: clean up some type names and uses
peterallenwebb Mar 6, 2023
678c4a5
CT-2198: Unify constraints and constraints_check properties on columns
peterallenwebb Mar 9, 2023
8b352de
Make mypy version consistently 0.981 (#7134)
gshank Mar 7, 2023
95020ec
CT 1808 diff based partial parsing (#6873)
gshank Mar 7, 2023
ad470e7
model contracts on models materialized as views (#7120)
emmyoop Mar 7, 2023
15c327a
Create method for env var deprecation (#7086)
stu-k Mar 8, 2023
d00eb96
update to allow adapters to change model name resolution in py models…
colin-rogers-dbt Mar 8, 2023
5945b60
add env DBT_PROJECT_DIR support #6078 (#6659)
leo-schick Mar 9, 2023
27eeb5c
Add new index.html and changelog yaml files from dbt-docs (#7141)
FishtownBuildBot Mar 10, 2023
53f1471
Make version configs optional (#7060)
dave-connors-3 Mar 10, 2023
996cfa8
[CT-1584] New top level commands: interactive compile (#7008)
aranke Mar 11, 2023
4e46095
Merge remote-tracking branch 'origin/main' into paw/ct-2198-unify-con…
peterallenwebb Mar 14, 2023
d9d9b2a
CT-2198: Add changelog entry
peterallenwebb Mar 15, 2023
f0aadb8
Merge remote-tracking branch 'origin/main' into paw/ct-2198-unify-con…
peterallenwebb Mar 15, 2023
1e2b9cc
CT-2198: Fix tests which broke after merge
peterallenwebb Mar 15, 2023
f830081
CT-2198: Add explicit validation of constraint types w/ unit test
peterallenwebb Mar 16, 2023
618f8c5
CT-2198: Move access property, per code review
peterallenwebb Mar 17, 2023
6f188e6
CT-2198: Remove a redundant macro
peterallenwebb Mar 17, 2023
70a19d3
CT-1298: Rework constraints to be adapter-generated in Python code
peterallenwebb Mar 21, 2023
674084a
Merge remote-tracking branch 'origin/main' into paw/ct-2198-unify-con…
peterallenwebb Mar 21, 2023
ec31ebd
CT-2198: Clarify function name per review
peterallenwebb Mar 22, 2023
a09f912
Merge remote-tracking branch 'origin/main' into paw/ct-2198-unify-con…
peterallenwebb Mar 22, 2023
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
54 changes: 45 additions & 9 deletions core/dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,40 @@
import time
from itertools import chain
from typing import (
Optional,
Tuple,
Any,
Callable,
Iterable,
Type,
Dict,
Any,
Iterable,
Iterator,
List,
Mapping,
Iterator,
Optional,
Set,
Tuple,
Type,
)

from dbt.contracts.graph.nodes import ColumnLevelConstraint, ConstraintType

import agate
import pytz

from dbt.exceptions import (
DbtInternalError,
DbtRuntimeError,
DbtValidationError,
MacroArgTypeError,
MacroResultError,
QuoteConfigTypeError,
NotImplementedError,
NullRelationCacheAttemptedError,
NullRelationDropAttemptedError,
QuoteConfigTypeError,
RelationReturnedMultipleResultsError,
RenameToNoneAttemptedError,
DbtRuntimeError,
SnapshotTargetIncompleteError,
SnapshotTargetNotSnapshotTableError,
UnexpectedNullError,
UnexpectedNonTimestampError,
UnexpectedNullError,
)

from dbt.adapters.protocol import AdapterConfig, ConnectionManagerProtocol
Expand Down Expand Up @@ -1262,6 +1265,39 @@ def get_incremental_strategy_macro(self, model_context, strategy: str):
# This returns a callable macro
return model_context[macro_name]

@classmethod
def _parse_constraint(cls, raw_constraint: Dict[str, Any]) -> ColumnLevelConstraint:
Copy link
Contributor

Choose a reason for hiding this comment

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

naming nit: _parse_column_constraint

try:
ColumnLevelConstraint.validate(raw_constraint)
return ColumnLevelConstraint.from_dict(raw_constraint)
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:
MichelleArk marked this conversation as resolved.
Show resolved Hide resolved
constraint = cls._parse_constraint(raw_constraint)
return cls.render_column_constraint(constraint)

@classmethod
def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> 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:
return f"check {constraint.expression}"
elif constraint.type == ConstraintType.not_null:
return "not null"
elif constraint.type == ConstraintType.unique:
return "unique"
elif constraint.type == ConstraintType.primary_key:
return "primary key"
elif constraint.type == ConstraintType.foreign_key:
return "foreign key"
elif constraint.type == ConstraintType.custom and constraint.expression:
return constraint.expression
else:
return ""


COLUMNS_EQUAL_SQL = """
with diff_count as (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
{%- set user_provided_columns = model['columns'] -%}
(
{% for i in user_provided_columns %}
{% set col = user_provided_columns[i] %}
{% set constraints = col['constraints'] %}
{{ col['name'] }} {{ col['data_type'] }} {% for x in constraints %} {{ "check" if x.type == "check" else "not null" if x.type == "not_null" else "unique" if x.type == "unique" else "primary key" if x.type == "primary_key" else "foreign key" if x.type == "foreign key" else ""
}} {{ x.expression or "" }} {% endfor %} {{ "," if not loop.last }}
{% endfor %}
)
{%- set col = user_provided_columns[i] -%}
{%- set constraints = col['constraints'] -%}
{{ col['name'] }} {{ col['data_type'] }}{% for c in constraints %} {{ adapter.render_raw_column_constraint(c) }}{% endfor %}{{ "," if not loop.last }}
{% endfor -%}
)
{% endmacro %}

{%- macro get_assert_columns_equivalent(sql) -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def test__constraints_correct_column_data_types(self, project, data_types):
# TODO: make more generic
_expected_sql = """
create table {0} (
id integer not null primary key check (id > 0) ,
color text ,
id integer not null primary key check (id > 0),
color text,
date_day date
) ;
insert into {0} (
Expand Down