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

[fix] use cast macro in default__get_empty_schema_sql #165

Merged
merged 6 commits into from
Apr 17, 2024

Conversation

MichelleArk
Copy link
Contributor

@MichelleArk MichelleArk commented Apr 12, 2024

resolves #166
docs dbt-labs/docs.getdbt.com/#

Problem

adapter-specific cast macro not being used in default__get_empty_schema_sql which leads to adapter-specific cases being mishandled and erroring.

Solution

Use the newly available adapter-specific cast macro! By default, this is just a refactor since the default implementation just does a cast <field> as <type>. However, adapters that override the cast macro (e.g. snowflake) would benefit from broader type support in constraint enforcement if the base implementation of get_empty_schema_sql made use of the adapter-specific override.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development, and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc.) or this PR has already received feedback and approval from Product or DX

🎩

-- geography.sql
SELECT 
TO_GEOMETRY('POINT(1820.12 890.56)') as geometry_field, 
TO_GEOGRAPHY('POINT(-122.35 37.55)') as geography_field

✅ correct & passing contract:

❯ dbt run --select geography
02:05:28  Running dbt...
02:05:28  target not specified in profile 'snowflake', using 'default'
02:05:29  Found 14 models, 6 seeds, 20 data tests, 6 sources, 13 metrics, 709 macros, 6 semantic models, 3 unit tests
02:05:29  
02:05:31  Concurrency: 4 threads (target='default')
02:05:31  
02:05:31  1 of 1 START sql view model dbt_marky.geography ................................ [RUN]
02:05:31  1 of 1 OK created sql view model dbt_marky.geography ........................... [SUCCESS 1 in 0.72s]
02:05:31  
02:05:31  Finished running 1 view model in 0 hours 0 minutes and 1.84 seconds (1.84s).
02:05:31  
02:05:31  Completed successfully
02:05:31  
02:05:31  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1

✅ incorrect and failing contract error message looks good!

❯ dbt run --select geography
02:06:46  Running dbt...
02:06:46  target not specified in profile 'snowflake', using 'default'
02:06:47  Found 14 models, 6 seeds, 20 data tests, 6 sources, 13 metrics, 709 macros, 6 semantic models, 3 unit tests
02:06:47  
02:06:48  Concurrency: 4 threads (target='default')
02:06:48  
02:06:48  1 of 1 START sql view model dbt_marky.geography ................................ [RUN]
02:06:49  1 of 1 ERROR creating sql view model dbt_marky.geography ....................... [ERROR in 0.70s]
02:06:49  
02:06:49  Finished running 1 view model in 0 hours 0 minutes and 1.80 seconds (1.80s).
02:06:49  
02:06:49  Completed with 1 error and 0 warnings:
02:06:49  
02:06:49    Compilation Error in model geography (models/geography.sql)
  This model has an enforced contract that failed.
  Please ensure the name, data_type, and number of columns in your contract match the columns in your model's definition.
  
  | column_name     | definition_type | contract_type | mismatch_reason    |
  | --------------- | --------------- | ------------- | ------------------ |
  | GEOGRAPHY_FIELD | GEOGRAPHY       | TEXT          | data type mismatch |
  | GEOMETRY_FIELD  | GEOMETRY        | TEXT          | data type mismatch |

Copy link

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@MichelleArk MichelleArk marked this pull request as ready for review April 12, 2024 01:57
@dbeatty10
Copy link
Contributor

@MichelleArk looks like the combination of this PR and dbt-labs/dbt-snowflake#969 resolves dbt-labs/dbt-snowflake#894 too 🤩

@MichelleArk MichelleArk changed the title use cast macro in default__get_empty_schema_sql [fix] use cast macro in default__get_empty_schema_sql Apr 17, 2024
@MichelleArk MichelleArk self-assigned this Apr 17, 2024
@MichelleArk MichelleArk merged commit 4774638 into main Apr 17, 2024
13 checks passed
@MichelleArk MichelleArk deleted the cast-macro-in-get-empty-schema-sql branch April 17, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Constraints] Constraint enforcement fails for types that cannot be cast to directly using cast function
3 participants