Skip to content

Commit 6a3b08b

Browse files
author
Siby Abin Thomas
committed
Fix incremental table recreation when defaultProject not set (issue #2053)
1 parent e168207 commit 6a3b08b

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

cli/api/commands/build.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ export class Builder {
4949

5050
const tableMetadataByTarget = new Map<string, dataform.ITableMetadata>();
5151

52+
// Normalize warehouse state targets to match compiled targets for consistent lookups.
53+
// This is necessary because targets from the warehouse may have a database field set
54+
// (from BigQuery credentials) while compiled targets may not have a database field
55+
// if defaultDatabase is not specified in workflow_settings.yaml.
5256
this.warehouseState.tables.forEach(tableState => {
53-
tableMetadataByTarget.set(targetStringifier.stringify(tableState.target), tableState);
57+
const normalizedTarget = this.normalizeWarehouseTarget(tableState.target);
58+
tableMetadataByTarget.set(targetStringifier.stringify(normalizedTarget), tableState);
5459
});
5560

5661
const actions: dataform.IExecutionAction[] = [].concat(
@@ -121,4 +126,22 @@ export class Builder {
121126
actionDescriptor: action.actionDescriptor
122127
});
123128
}
129+
130+
private normalizeWarehouseTarget(warehouseTarget: dataform.ITarget): dataform.ITarget {
131+
// If the warehouse target has a database field and the compiled graph doesn't have a
132+
// defaultDatabase, we should remove the database field from the warehouse target
133+
// to match the format of the compiled targets. This ensures consistent lookups
134+
// when defaultDatabase is not specified in workflow_settings.yaml.
135+
if (
136+
warehouseTarget.database &&
137+
!this.prunedGraph.projectConfig.defaultDatabase
138+
) {
139+
return dataform.Target.create({
140+
schema: warehouseTarget.schema,
141+
name: warehouseTarget.name
142+
});
143+
}
144+
// Otherwise return the warehouse target as-is
145+
return warehouseTarget;
146+
}
124147
}

core/actions/incremental_table_test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,39 @@ defaultIcebergConfig:
882882
});
883883
});
884884
});
885+
886+
test("incremental table without defaultProject in workflow_settings", () => {
887+
const projectDir = tmpDirFixture.createNewTmpDir();
888+
// Create workflow_settings without defaultProject (only defaultDataset and defaultLocation)
889+
fs.writeFileSync(
890+
path.join(projectDir, "workflow_settings.yaml"),
891+
`defaultDataset: dataform
892+
defaultLocation: europe-west2
893+
`
894+
);
895+
fs.mkdirSync(path.join(projectDir, "definitions"));
896+
fs.writeFileSync(
897+
path.join(projectDir, "definitions/incremental_table_without_default_project.sqlx"),
898+
`config {
899+
type: "incremental",
900+
name: "incremental_table_without_default_project"
901+
}
902+
903+
select \${incremental()} as is_incremental`
904+
);
905+
906+
const result = runMainInVm(coreExecutionRequestFromPath(projectDir));
907+
908+
expect(result.compile.compiledGraph.graphErrors.compilationErrors).deep.equals([]);
909+
const compiledTable = result.compile.compiledGraph.tables[0];
910+
// Verify the table compiles without database field since defaultProject is not set.
911+
// The normalizeWarehouseTarget ensures that targets without database
912+
// are correctly matched with warehouse state during the build phase, allowing incremental
913+
// appends to work even when defaultProject is not specified in workflow_settings.yaml.
914+
expect(compiledTable.type).equals("incremental");
915+
expect(compiledTable.enumType).equals(dataform.TableType.INCREMENTAL);
916+
expect(compiledTable.target.schema).equals("dataform");
917+
expect(compiledTable.target.name).equals("incremental_table_without_default_project");
918+
expect(compiledTable.target.database).equals("");
919+
});
885920
});

0 commit comments

Comments
 (0)