Failure to source correct data types with use_materialization_v2
Describe the bug
Very similar to #1066
When using use_materialization_v2: true The DDL statement that creates the target_relation is using data types defined in the YAML despite it running a DESCRIBE TABLE on the intermediate_relation which returns accurate data types.
This becomes even more problematic when using complex data types like STRUCT because the order of fields within the struct matter and are difficult to document exactly in the model.yml
Steps To Reproduce
- Create a model with incorrect data types defined in the model.yml
- Run the model with
use_materialization_v2: true - Watch it fail during
INSERTas it realizes it created a tabletarget_relationwith data types that don't match the types in theintermediate_relation
Expected behavior
- Simple: Use the data types returned in the
DESCRIBE TABLEthat was run on theintermediate_relation - Nice to have: Provide options to allow data types to be sourced from a
DESCRIBE TABLEor frommodel.yml
Screenshots and log output
If applicable, add screenshots or log output to help explain your problem.
System information
The output of dbt --version:
Core:
- installed: 1.10.3
- latest: 1.10.3 - Up to date!
Plugins:
- databricks: 1.10.4 - Up to date!
- spark: 1.9.2 - Up to date!
The operating system you're using:
The output of python --version:
Python 3.13.4
Additional context
Why are you specifying incorrect data types in your yaml? What is the purpose of including data types if they are not correct?
There are a variety of scenarios where data types in the yaml files may not be synchronized with the types produced by the model SQL
-
During development the data types specified in yaml will not always match exactly what is produced by the model sql. Updating/generating yaml to reflect code changes is often a last step in the development process but with V2 materializations, developers would be required to keep their yaml accurate during all stages of development if they want to run their model
-
Using dbt-labs/dbt-codegen documented data types are a consequence of what is run in the model not the other way around. i.e. Models must run first - then the yaml is generated with correct data types that were produced by the model sql. It is circular to allow documented data types to affect the actual types in the model in this scenario.
-
As mentioned, incorrect data types aren't always intended - its a little more complicated in practice. Some examples of this:
structfields are messy. The data typesstruct<name: string, dob: timestamp>is not equivilent tostruct<dob: timestamp, name: string>. Moreover - Databricks does some weird inference sometimes to try to resolve this type of CAST; If my sql produces a field of typestruct<dob: timestamp, name: string>but i've documented it asstruct<name: string, dob: timestamp>The model MAY succeed IF Databricks can cast the type properly but even if it does, thetarget_relationwill have a different data type than theintermediate_relation/(implied by my model code). The resulting column would have a struct with fields out of order - leading to broken sorting/filtering operations on these fields (easier to show than explain)- Fields aren't always statically typed for some models. For example, at compile time we can get the type of a field using the schema_of_json_agg function. In this case, we may have a type in the yaml of some model that represents what the json object looked like during development but not the schema it currently has. The model sql can produce a type that is not known until the dbt is compiled
- It's totally possible that a fields type is "passed through" from source. i.e. I have a table loading into Databricks, I source that table in DBT and use it in a model. Without V2 materializations the type of the field in my model is inferred from whatever it was in source, with V2 materializations, the type of the field is cast based on my yaml file. Prior to V2 materializations if the source type changes (let's say from
tinyinttobigint) then that change flows through to my model. With V2, my model would be forced to have onlytinyintwhich may break when downcasting frombigint.
@benc-db Hope this helps - let me know if you want to set up some time to chat more
Similar discussion is happening in https://github.com/databricks/dbt-databricks/issues/1066. My question is really why are you specifying data_type at all if it does not match what you want in your data store?
It's useful for:
- Letting developers know what type of data they are working with
- Generating
dbt docs - Training any ML models on data in our YAMLs
Ultimately having yaml files with data types that are 98% correct is better than excluding the data types from all our yaml docs.
Seems like solving it would just be a matter of updating the logic for how to "enrich" columns here
Generating dbt docs
We used to generate column info for dbt docs from the data store. It was a little slow so we switched to reading it out of the yaml.
Its not that the change is difficult to revert, it's that we assume our users known better than the compiler in some cases. It's an escape valve for situations that are otherwise very difficult to solve, such as if the types are getting truncated when we attempt to read them. We are trying out a new way of getting metadata that may fix those issues, in which case maybe we can go back to the previous behavior. I'll think on it.
@benc-db any update on this?
A pretty common order of operations is:
- Build models from our internal requirements
- Generate the YAML files using dbt-labs/dbt-codegen from the datastore
- Generate dbt docs from the YAML
Seems that materializations V2 breaks this approach with it assuming YAML defined data types are more correct than data types shown in Databricks. If dbt-databricks is going this direction by default, can we at least have an option to use the datastore data types when creating the table schema
Thanks @ShaneMazur — converging discussion on #1066 since both issues land on the same V2 yml-vs-intermediate type reconciliation path (and you already flagged the similarity up top). The concrete examples you laid out (codegen ordering, struct field ordering, schema_of_json_agg, source pass-through) are captured in the thread for when we revisit the enrichment logic. Closing as duplicate of #1066.