Data Types in model.yml are ignored when contract is enforced
Describe the bug
I am using dbt-databricks version 1.10.1 and have use_materialization_v2 enabled. I have noticed, that I can set the data type of a column by specifying it in the model.yaml when using table materialization. As far as I know, previously the data types in the model.yaml were ignored and had no impact on the actual data type in the table. I just noticed, that this behaviour only shows, when a contract is not enforced. so what I am seeing is, that I can set the data type in the yaml only when the contract is not enforced.
Steps To Reproduce
create a model with a yaml and set the data_types in the yaml. disable or enable the contract enforcement to see the different behavior.
Expected behavior
data types are taken from the model.yaml even when the contract is enforced
System information
dbt-databricks 1.10.1
@ericj-db and I did a deep dive on this today. Per the code, we don't do anything different as far as column type specification between contract enforced = true vs false. I think what's happening is the result of the following: 1.) Contract enforcement is supposed to be a 'pre-flight' check, meaning before we create objects in Databricks 2.) We use the default logic for determining if the column types match, which gets the column types by issuing a LIMIT 0 query. 3.) On Databricks, a limit 0 will not guarantee correct column type inference 4.) As such, for complex types, or differences of precision (INT vs BIGINT), contract enforcement will fail before we actually create the table in Databricks, and thus don't have an opportunity to respect the column types you specified as part of table creation.
We tried ways of modifying the column inference, but ultimately were not able to come up with a reliable mechanism. What we're left at is recommending against using contract enforcement with dbt-databricks because otherwise completely reasonable use cases will fail this validation.
@AmyChen1776 Viz
@benc-db thank you for looking into this. The only reason we currently need to enforce contracts is to be able to set constraints on our table. Is there a different mechanism how we can set constraints when we shouldn't enforce contracts?
In V2, I'm pretty sure we apply constraints to the table regardless of whether or not you have set contract_enforced
@benc-db thanks, I was not aware of this. I checked and the constraints are created without contract enforcement.
One of the fundamental issues here is that materialization v.2 uses the YAML file to generate the create replace table to define the datatypes. This causes serious issues for models in development that are not ready for deployment. The YAML can easily be out of date from the SQL. IMHO, the YAML is a derivative of what SQL does. In many cases, including ours, the YAML skeleton is generated from the resulting table. The materializer uses a describe command, but then differs from the YAML for the datatype anyway.
Why is this problematic? The SQL defines a struct of <timestamp, string> during a development edit. The YAML is still outdated and says <string, timestamp>. Not only will your model no longer run correctly, but the error condition is extremely cryptic. So, deferring to YAML for datatype when running a describe is goofy. You should always use the datatype from the description of the temp table because it is always definitive of what the data model truly is. If that differs from the YAML, the right solution is to provide a warning text that the YAML model is outdated compared to the SQL.
It is not a given that YAML contract enforcement is always desired, especially during a rapid development cycle when the SQL might evolve way faster than the accompanying YAML. Both defering to the YAML for datatype and contract enforcement in general is contrary to needing to rabidly evolve an SQL model during iterative development cycles. In addition, it precludes the use of AI to generate YAML because the AI is very likely to access the underlying information schema for the table to generate that YAML.
@scalepadfrankb
One of the fundamental issues here is that materialization v.2 uses the YAML file to generate the create replace table to define the datatypes. This causes serious issues for models in development that are not ready for deployment. The YAML can easily be out of date from the SQL.
If dbt is operating as intended, the yaml is prescriptive for what is materialized in your datastore. dbt is a declarative framework for transforming your dbt. If your table is out of date from your yaml, and you are not intending for dbt to correct that by changing the table to match the yaml, you are not using dbt as expected. What do you mean a development edit? You shouldn't be editing your table separate from running dbt. Furthermore, no one is forcing you to specify your column types in yaml. We defer to them because the assumption is that you know better than the Databricks compiler, otherwise you wouldn't have specified them.
The other issue at stake here is that if we don't specify the column types at create time, we cannot do anything else to the columns at create time. That is a limitation of Databricks. So if we let Databricks infer the column types using a CTAS (create table as ... select ...), we cannot apply constraints until after the data is in there, in which case they are not actually constraints in the sense that a check constraint does not prevent invalid data from arriving in the table. We also can't specify column descriptions at create time if we don't have the types. The problem with that is that column comment altering scales with the size of the table, so instead of it being basically free, large tables end up waiting minutes for their descriptions to get persisted. Again, this is a Databricks limitation we are working around.
As a practical matter, YAML is not the data contract because SQL defines the datatypes, and those evolve. The developer must maintain that in two places, which is never a good solution. SQL is always definitive because it is what executes. YAML is not an executable, so practically it is at best descriptive.
By enforcing this, you are making my executable dependent on mostly documentation, which is prone to being out of date and requires extra steps to maintain. This should be something I can turn off. Otherwise, we must stop including the data type in YML to avoid maintaining this in two places, thus forcing the issue anyway and losing the ability to document the data type in DBT docs.
By enforcing this, you are making my executable dependent on mostly documentation, which is prone to being out of date and requires extra steps to maintain. This should be something I can turn off. Otherwise, we must stop including the data type in YML to avoid maintaining this in two places, thus forcing the issue anyway and losing the ability to document the data type in DBT docs.
Is the issue that you can't document your data types? I recommend opening a new issue for that. We used to gather column type and description for generating documentation from the data store directly, but the metadata APIs were so slow at the time that it wasn't reasonable for most users. Hopefully that has improved.
My understanding is that yaml exists primarily to configure the materialization process; you can use it as documentation precisely because it represents what happens when you run dbt. So fundamentally, if you don't want those column types to be the column types that get materialized in Mat V2, yes, the solution is to remove them.
I don't know the objection to allowing the ability to turn it off. You already use the describe extended to find data types when they are not defined in YAML. Defining them in YAML is optional anyway. The describe command is issued even when the datatypes are defined in YAML. So why not add a switch to turn off datatype contract enforcement? If the objection is philosophical, your suggested solution doesn't satisfy those tenets because you're telling us to remove the datatypes from the contract. So, the solution is to remove the datatypes from the contract. Let me turn it off so I can still use the datatypes in my dbt docs, AI agents, and the dbt docgen tool, since executing the materialization is obviously not required. It's a guard that we don't want guarded.
I will think about this more; I understand the push back as a change in behavior that you have come to rely on. But I still don't really understand why you would want types that are not accurate to be used by 'dbt docs, AI agents, and the dbt docgen tool, since executing the materialization is obviously not required'. Won't all of those use cases be impaired by not having correct types?
Copying from a parallel thread: "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."
On Databricks, a limit 0 will not guarantee correct column type inference
@benc-db got any reference to docs for that? This is the first I'm hearing of it (though I'm relatively new to databricks) so just wanting to understand the implementation limits a bit better.
Linking #1092 here — closed as a duplicate of this issue. It's the inverse symptom of the same V2 yml-vs-intermediate type reconciliation path: #1066 reports yml data_type being ignored (with contract enforced), #1092 reports it being used even when it doesn't match the actual intermediate type (notably for nested struct ordering). @ShaneMazur laid out concrete repro scenarios over there — codegen ordering, schema_of_json_agg-typed fields, source-type pass-through — worth reading when we revisit dbt/adapters/databricks/column.py enrichment.
@sd-db is there any plan to support materializations V2 while not enforcing yaml defined data types? Any path forward here would help
@sd-db is there any plan to support materializations V2 while not enforcing yaml defined data types? Any path forward here would help
I had always thought this would have been possible with contract.enforced: false but seems that is not the case. I will have to investigate a bit here. Will prioritise this post 1.12 release as this has been in the backlog for a while
Bumping this issue to keep it fresh. Transparently, this is the core issue that our organization needs resolved in order to adopt materialization_v2. We rely on enforced contracts and firmly believe that data types should be defined at the model layer with the model's property file updated to match.
This is unfortunate for us as we would love to take advantage of some of the other changes here, like column descriptions, that are handled much better than the older materialization strategy.