-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Ct 1919/get column schema from query macro core #6981
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
Closed
MichelleArk
wants to merge
119
commits into
main
from
CT-1919/get_column_schema_from_query_macro_core
+130
−26
Closed
Changes from 118 commits
Commits
Show all changes
119 commits
Select commit
Hold shift + click to select a range
e879cdd
start off the blueprints
sungchun12 bd1afc7
Merge branch 'main' of https://github.com/dbt-labs/dbt into feature/d…
sungchun12 4b2a881
Merge branch 'main' of https://github.com/dbt-labs/dbt into feature/d…
sungchun12 e8b52e0
test commit
sungchun12 01a07d3
working snowflake env
sungchun12 3b31a15
update manifest expectation
sungchun12 1a1f46a
add error handling
sungchun12 ebaa54c
clean up language
sungchun12 fd7a47a
constraints validator
sungchun12 7e3a9be
cleaner example
sungchun12 6df02da
better terminal output
sungchun12 bc3c5bc
add python error handling
sungchun12 4b901fd
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 e29b571
add to manifest schema
sungchun12 7d085dc
add to schema config
sungchun12 e6559d4
clean up comments
sungchun12 ca89141
backwards compatible nodeconfig
sungchun12 2c4a4cf
remove comments
sungchun12 1975c6b
clean up more comments
sungchun12 f088a03
add changelog
sungchun12 7421caa
clarify error message
sungchun12 a7395bb
constraints list type
sungchun12 5d06524
fix grammar
sungchun12 380bd96
add global macros
sungchun12 e6e490d
clearer compile error
sungchun12 9c498ef
remove comments
sungchun12 bed1fec
fix tests in this file
sungchun12 8c466b0
conditional compile errors
sungchun12 547ad9e
add conditional check in ddl
sungchun12 52bd35b
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 7582531
add macro to dispatch
sungchun12 7291094
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 b87f57d
fix regressions in parsed
sungchun12 5529334
fix regressions in manifest tests
sungchun12 00f12c2
fix manifest test regressions
sungchun12 76bf69c
fix test_list regressions
sungchun12 2e51246
concise data_type terminal error
sungchun12 5891eb3
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 5d2867f
remove placeholder function
sungchun12 801b2fd
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 5d59cc1
fix failed regressions finally
sungchun12 92d2ea7
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 4f747b0
Revert "Merge branch 'main' of https://github.com/dbt-labs/dbt into d…
sungchun12 eba0b6d
Revert "Revert "Merge branch 'main' of https://github.com/dbt-labs/db…
sungchun12 ae56da1
remove tmp.csv
sungchun12 de653e4
template test plans
sungchun12 cfc53b0
postgres columns spec macro
sungchun12 fc7230b
schema does not exist error handling
sungchun12 e1c72ac
update postgres adapter
sungchun12 b215b6c
remove comments
sungchun12 d550de4
first passing test
sungchun12 9d87463
fix postgres macro
sungchun12 ce85c96
add more passing tests
sungchun12 49a4120
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 8ffb654
Add generated CLI API docs
FishtownBuildBot f2f2707
add disabled config test
sungchun12 096f3fd
column configs match
sungchun12 eae4e76
Merge branch 'dbt-constraints' of https://github.com/dbt-labs/dbt int…
sungchun12 b8c3812
test python error handling
sungchun12 bb1a6c3
adjust macro with rollback
sungchun12 b6dbcf6
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 751cdc8
start postgres tests
sungchun12 6bbd797
remove begin commit
sungchun12 d364eeb
remove begin commit comments
sungchun12 4a58ece
passing expected compiled sql test
sungchun12 ac795dd
passing rollback test
sungchun12 d452cae
update changelog
sungchun12 baf18f0
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 76c0e4f
fix artifacts regression
sungchun12 10ab3cb
modularize validator
sungchun12 6253ed0
PR feedback
sungchun12 307809d
verify database error occurs
sungchun12 ab4f396
focus on generic outcomes
sungchun12 b99e9be
fix global macro
sungchun12 5935201
rename to constraints_check
sungchun12 f59c9dd
missed a check rename
sungchun12 7e28a31
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 3ddd666
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 fabe2ce
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 ffec7d7
validate at parse time
sungchun12 d338f33
raise error for modelparser only
sungchun12 e34c467
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 44b2f18
better spacing in terminal output
sungchun12 17b1f8e
fix test regressions
sungchun12 c652367
fix manifest test regressions
sungchun12 f9e020d
these are parsing errors now
sungchun12 926e555
merge main
sungchun12 c6bd674
fix tests
sungchun12 bcc35fc
test passes in json log format
sungchun12 880ed43
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 426789e
add column compile error handling
sungchun12 3d61eda
update global macros for column handling
sungchun12 f163b2c
remove TODO
sungchun12 bf45243
uppercase columns for consistency
sungchun12 dbef42b
more specific error handling
sungchun12 c4de8f3
migrate tests
sungchun12 ad07ced
clean up core tests
sungchun12 a501c29
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 59b0298
Update core/dbt/include/global_project/macros/materializations/models…
sungchun12 e46066b
Revert "Update core/dbt/include/global_project/macros/materialization…
sungchun12 e80b8cd
update for pre-commit hooks
sungchun12 257eacd
update for black formatter
sungchun12 391bd3b
update for black formatter on all files
sungchun12 0441417
Merge remote-tracking branch 'origin/main' into dbt-constraints
jtcohen6 e0bcb25
Refactor functional tests
jtcohen6 dcf7062
Fixup formatting
jtcohen6 903a2cb
Dave feedback
jtcohen6 c40ee92
another one - dave
jtcohen6 111683a
the hits keep coming
jtcohen6 f8b16bc
adjust whitespace
dave-connors-3 97f0c6b
Merge branch 'main' of https://github.com/dbt-labs/dbt into dbt-const…
sungchun12 2a33baf
Light touchup
jtcohen6 6806a7c
Merge remote-tracking branch 'origin/main' into dbt-constraints
jtcohen6 1256e7b
Add more flexibility for spark
jtcohen6 0304dbf
Nearly there for spark
jtcohen6 b5b1699
Merge main
jtcohen6 1b0c213
Experiment: column types from empty query
jtcohen6 64470ca
use get_empty_schema_sql to get column schema from schema yml
MichelleArk c3e6b7a
Merge branch 'main' into CT-1919/get_column_schema_from_query_macro_core
MichelleArk 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,8 @@ | ||
| kind: Features | ||
| body: Data type constraints are now native to SQL table materializations. Enforce | ||
| columns are specific data types and not null depending on database functionality. | ||
| time: 2022-11-18T14:11:20.868062-08:00 | ||
| custom: | ||
| Author: sungchun12 | ||
| Issue: "6079" | ||
| PR: "6271" |
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 |
|---|---|---|
|
|
@@ -128,6 +128,31 @@ def get_result_from_cursor(cls, cursor: Any) -> agate.Table: | |
|
|
||
| return dbt.clients.agate_helper.table_from_data_flat(data, column_names) | ||
|
|
||
| @classmethod | ||
| def data_type_code_to_name(cls, int) -> str: | ||
| """Get the string representation of the data type from the type_code.""" | ||
| # https://peps.python.org/pep-0249/#type-objects | ||
| raise dbt.exceptions.NotImplementedError( | ||
| "`data_type_code_to_name` is not implemented for this adapter!" | ||
| ) | ||
|
|
||
| @classmethod | ||
| def get_column_schema_from_cursor(cls, cursor: Any) -> List[Tuple[str, str]]: | ||
| # (column_name, data_type) | ||
| columns: List[Tuple[str, str]] = [] | ||
|
|
||
| if cursor.description is not None: | ||
| # https://peps.python.org/pep-0249/#description | ||
| columns = [ | ||
| # TODO: ignoring size, precision, scale for now | ||
| # (though it is part of DB-API standard, and our Column class does have these attributes) | ||
| # IMO user-defined contracts shouldn't have to match an exact size/precision/scale | ||
| (col[0], cls.data_type_code_to_name(col[1])) | ||
| for col in cursor.description | ||
| ] | ||
|
|
||
| return columns | ||
|
|
||
| def execute( | ||
| self, sql: str, auto_begin: bool = False, fetch: bool = False | ||
| ) -> Tuple[AdapterResponse, agate.Table]: | ||
|
|
@@ -140,6 +165,20 @@ def execute( | |
| table = dbt.clients.agate_helper.empty_table() | ||
| return response, table | ||
|
|
||
| # TODO: do we need to care about auto_begin here? | ||
| def get_column_schema_from_query(self, sql: str) -> List[Tuple[str, str]]: | ||
| sql = self._add_query_comment(sql) | ||
| _, cursor = self.add_query(sql) | ||
| return self.get_column_schema_from_cursor(cursor) | ||
|
|
||
| # For dbt-bigquery | ||
| # def get_column_schema_from_query(cls, sql: str) -> List[Tuple[str, str]]: | ||
| # sql = self._add_query_comment(sql) | ||
| # # auto_begin is ignored on bigquery, and only included for consistency | ||
| # query_job, iterator = self.raw_execute(sql) | ||
| # columns = [(field.name, field.field_type) for field in resp.iterator] | ||
|
Contributor
Author
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. s/resp.iterator/iterator.schema |
||
| # return columns | ||
|
|
||
| def add_begin_query(self): | ||
| return self.add_query("BEGIN", auto_begin=False) | ||
|
|
||
|
|
||
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
Binary file not shown.
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
54 changes: 54 additions & 0 deletions
54
core/dbt/include/global_project/macros/materializations/models/table/columns_spec_ddl.sql
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,54 @@ | ||
| {%- macro get_columns_spec_ddl() -%} | ||
| {{ adapter.dispatch('get_columns_spec_ddl', 'dbt')() }} | ||
| {%- endmacro -%} | ||
|
|
||
| {% macro default__get_columns_spec_ddl() -%} | ||
| {{ return(columns_spec_ddl()) }} | ||
| {%- endmacro %} | ||
|
|
||
| {% macro columns_spec_ddl() %} | ||
| {# loop through user_provided_columns to create DDL with data types and constraints #} | ||
| {%- set user_provided_columns = model['columns'] -%} | ||
| ( | ||
| {% for i in user_provided_columns %} | ||
| {% set col = user_provided_columns[i] %} | ||
| {% set constraints = col['constraints'] %} | ||
| {% set constraints_check = col['constraints_check'] %} | ||
| {{ col['name'] }} {{ col['data_type'] }} {% for x in constraints %} {{ x or "" }} {% endfor %} {% if constraints_check -%} check {{ constraints_check or "" }} {%- endif %} {{ "," if not loop.last }} | ||
| {% endfor %} | ||
| ) | ||
| {% endmacro %} | ||
|
|
||
| {%- macro get_assert_columns_equivalent(sql) -%} | ||
| {{ adapter.dispatch('get_assert_columns_equivalent', 'dbt')(sql) }} | ||
| {%- endmacro -%} | ||
|
|
||
| {% macro default__get_assert_columns_equivalent(sql) -%} | ||
| {{ return(assert_columns_equivalent(sql)) }} | ||
| {%- endmacro %} | ||
|
|
||
| {% macro assert_columns_equivalent(sql) %} | ||
| {%- set sql_file_provided_columns = get_column_schema_from_query(sql) -%} | ||
| {%- set schema_file_provided_columns = get_column_schema_from_query(get_empty_schema_sql(model['columns'])) -%} | ||
|
|
||
| {%- set sql_file_provided_columns_formatted = format_columns(sql_file_provided_columns) -%} | ||
| {%- set schema_file_provided_columns_formatted = format_columns(schema_file_provided_columns) -%} | ||
|
|
||
| {%- if sql_file_provided_columns_formatted != schema_file_provided_columns_formatted -%} | ||
| {%- do exceptions.raise_compiler_error('Please ensure the name, data_type, order, and number of columns in your `yml` file match the columns in your SQL file.\nSchema File Columns: ' ~ sql_file_provided_columns_formatted ~ '\n\nSQL File Columns: ' ~ schema_file_provided_columns_formatted ~ ' ' ) %} | ||
| {%- endif -%} | ||
|
|
||
| {% endmacro %} | ||
|
|
||
| {% macro format_columns(columns) %} | ||
| {{ adapter.dispatch('format_columns', 'dbt')(columns) }} | ||
| {%- endmacro %} | ||
|
|
||
| {% macro default__format_columns(columns) -%} | ||
| {% set formatted_columns = [] %} | ||
| {% for column in columns %} | ||
| {%- set formatted_column = column.column.lower() ~ " " ~ column.dtype -%} | ||
| {%- do formatted_columns.append(formatted_column) -%} | ||
| {% endfor %} | ||
| {{ return(formatted_columns|join(', ')) }} | ||
| {%- endmacro -%} |
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
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.
TODO: change to
-> List[Tuple[str, Any]]