feat(databricks): create tables with IF NOT EXISTS and reconcile columns for concurrent loads#4138
Open
francescomucio wants to merge 1 commit into
Conversation
…mns for concurrent loads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4135
Problem
When several dlt runs load into the same Databricks table concurrently and the table doesn't exist yet, every run reads storage, sees the table missing (
generate_alter = False), and emits a plainCREATE TABLE. The first commit wins; the rest fail with "table already exists". Hiding behind that crash is a second bug: a run carrying an extra column would see its create become a no-op and then fail when its load writes a column that was never added.Change (Databricks only)
CREATE TABLE IF NOT EXISTSfor all tables._make_create_tableis overridden to drop the "dlt system tables only" restriction (still gated by the existingsupports_create_table_if_not_existscapability). Covers both the base CREATE path and the custom cluster/partition/tblproperties path.ADD COLUMN IF NOT EXISTS, so_execute_schema_update_sqlre-reads the destination after the (possibly no-op) create and issuesALTER TABLE … ADD COLUMNfor any columns still missing, tolerating theFIELDS_ALREADY_EXISTSerror raised when a concurrent run adds the same column first.Net effect: identical-schema parallel loads stop racing, and divergent-schema loads converge.
Notes
update_stored_schema(i.e. when the schema hash actually changes), not on every load.applied_updatediff; this is informational only — the data write maps columns by name against the full stored schema, so the load is unaffected.Tests
Added to
tests/load/databricks/test_databricks_table_builder.py(no live connection needed):CREATE TABLE IF NOT EXISTSemitted on both create paths.FIELDS_ALREADY_EXISTSis tolerated; other errors re-raise.