-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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-2196] Make constraints ddl agnostic to column order #7064
Comments
What this should look like, roughly: create table dbt_jcohen.my_model (
-- returned by get_columns_spec_ddl
column_a integer not null,
column_b text,
...
)
as (
-- this part is new
select
column_a, column_b, ...
from (
-- user's model SQL could return these columns in a diff order
select
... as column_b,
... as column_a,
...
) model_subq -- subqueries must be aliased on Postgres
); Where Note that this is quite similar to, but slightly different from, |
In terms of integration testing, We won't be able to remove |
I can't create a test with schema columns different than the SQL because an error occurs: 19:04:40 Compilation Error in model my_model (models/my_model.sql) Am I supposed to remove that check? |
Ah right, because the model contract (name, data_type, order (for now)) enforcement occurs before the ddl generation. So we won't even get to the ddl generation as long as model contract enforcement depends on contract order. We do want to remove that check as part of #6975. I'd be comfortable with removing that test as part of this work assuming the PR to close #6975 is merged in coordination with the PR associated with this issue. |
Resolved by #7161 |
The foundational work on constraints introduced pre-flight contract validation on column names, expecting that the user-provided column names are identical both in name and order the column names obtained from the model SQL. This is currently by design, so that the database associates any configured constraints to the correct columns from the query result.
However, it will be cumbersome for users to ensure that the order of configured columns in a model yaml file always matches with the order the database provides.
We can mitigate this while still ensuring any constraints are applied to the correct columns by wrapping user-provided SQL in a subquery which SELECTs columns in the order they are provided in the schema yaml (which is the same order used to generate the constraints ddl).
Implementation Notes:
contract: True
, wrap the user-provided SQL in aSELECT ..
statement generated frommodel['columns']
(the user-provided columns).Could look something like:
view
materializations as they don't support constraints.Relevant discussions: #6271 (comment), https://github.com/dbt-labs/dbt-core/pull/6271/files#r1069332715
The text was updated successfully, but these errors were encountered: