dbt-databricks icon indicating copy to clipboard operation
dbt-databricks copied to clipboard

Failure to source correct data types with use_materialization_v2

Open ShaneMazur opened this issue 1 year ago • 6 comments

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

  1. Create a model with incorrect data types defined in the model.yml
  2. Run the model with use_materialization_v2: true
  3. Watch it fail during INSERT as it realizes it created a table target_relation with data types that don't match the types in the intermediate_relation

Expected behavior

  • Simple: Use the data types returned in the DESCRIBE TABLE that was run on the intermediate_relation
  • Nice to have: Provide options to allow data types to be sourced from a DESCRIBE TABLE or from model.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

ShaneMazur avatar Jul 08 '25 20:07 ShaneMazur

Why are you specifying incorrect data types in your yaml? What is the purpose of including data types if they are not correct?

benc-db avatar Jul 08 '25 20:07 benc-db

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

  1. 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

  2. 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.

  3. As mentioned, incorrect data types aren't always intended - its a little more complicated in practice. Some examples of this:

    • struct fields are messy. The data types struct<name: string, dob: timestamp> is not equivilent to struct<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 type struct<dob: timestamp, name: string> but i've documented it as struct<name: string, dob: timestamp> The model MAY succeed IF Databricks can cast the type properly but even if it does, the target_relation will have a different data type than the intermediate_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 tinyint to bigint) then that change flows through to my model. With V2, my model would be forced to have only tinyint which may break when downcasting from bigint.

@benc-db Hope this helps - let me know if you want to set up some time to chat more

ShaneMazur avatar Jul 08 '25 21:07 ShaneMazur

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?

benc-db avatar Jul 08 '25 21:07 benc-db

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

ShaneMazur avatar Jul 08 '25 21:07 ShaneMazur

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 avatar Jul 08 '25 22:07 benc-db

@benc-db any update on this?

A pretty common order of operations is:

  1. Build models from our internal requirements
  2. Generate the YAML files using dbt-labs/dbt-codegen from the datastore
  3. 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

ShaneMazur avatar Aug 18 '25 16:08 ShaneMazur

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.

sd-db avatar Apr 21 '26 07:04 sd-db