-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update incremental table execution to avoid create or replace #1712
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -138,7 +138,7 @@ from (${query}) as insertions`; | |||||
|
||||||
if (table.enumType === dataform.TableType.INCREMENTAL) { | ||||||
if (!this.shouldWriteIncrementally(runConfig, tableMetadata)) { | ||||||
tasks.add(Task.statement(this.createOrReplace(table))); | ||||||
tasks.add(Task.statement(this.createTableStatement(table, runConfig))); | ||||||
} else { | ||||||
tasks.add( | ||||||
Task.statement( | ||||||
|
@@ -159,7 +159,7 @@ from (${query}) as insertions`; | |||||
); | ||||||
} | ||||||
} else { | ||||||
tasks.add(Task.statement(this.createOrReplace(table))); | ||||||
tasks.add(Task.statement(this.createTableStatement(table, runConfig))); | ||||||
} | ||||||
|
||||||
this.postOps(table, runConfig, tableMetadata).forEach(statement => tasks.add(statement)); | ||||||
|
@@ -182,7 +182,7 @@ from (${query}) as insertions`; | |||||
return `drop ${this.tableTypeAsSql(type)} if exists ${this.resolveTarget(target)}`; | ||||||
} | ||||||
|
||||||
private createOrReplace(table: dataform.ITable) { | ||||||
private createTableStatement(table: dataform.ITable, runConfig: dataform.IRunConfig) { | ||||||
const options = []; | ||||||
if (table.bigquery && table.bigquery.partitionBy && table.bigquery.partitionExpirationDays) { | ||||||
options.push(`partition_expiration_days=${table.bigquery.partitionExpirationDays}`); | ||||||
|
@@ -196,17 +196,34 @@ from (${query}) as insertions`; | |||||
} | ||||||
} | ||||||
|
||||||
return `create or replace ${table.materialized ? "materialized " : ""}${this.tableTypeAsSql( | ||||||
this.baseTableType(table.enumType) | ||||||
)} ${this.resolveTarget(table.target)} ${ | ||||||
table.bigquery && table.bigquery.partitionBy | ||||||
? `partition by ${table.bigquery.partitionBy} ` | ||||||
: "" | ||||||
}${ | ||||||
table.bigquery && table.bigquery.clusterBy && table.bigquery.clusterBy.length > 0 | ||||||
? `cluster by ${table.bigquery.clusterBy.join(", ")} ` | ||||||
: "" | ||||||
}${options.length > 0 ? `OPTIONS(${options.join(",")})` : ""}as ${table.query}`; | ||||||
let protectTable = false; | ||||||
if (table.enumType === dataform.TableType.INCREMENTAL) { | ||||||
protectTable = table.protected || !runConfig.fullRefresh; | ||||||
} | ||||||
|
||||||
let statement = "create "; | ||||||
if (!protectTable) { | ||||||
statement += "or replace "; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the bug - I think we still want to create or replace in the case that it is incremental AND it's not projected AND it's full refresh? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add a test for this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is projected? Have added the full refresh. This is tested, but not obviously, across dataform/tests/api/api.spec.ts Line 463 in c55ca6b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *protected! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right! Done. I had to update the protected logic, such as removing the error that we test for here dataform/tests/api/api.spec.ts Line 109 in c55ca6b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, sorry I think this has got really confusing, a few thoughts.
The original method was clear what it did, but now you've moved this logic into it, I'd say it's much harder to follow what's going on in |
||||||
} | ||||||
if (table.materialized) { | ||||||
statement += "materialized "; | ||||||
} | ||||||
statement += `${this.tableTypeAsSql(this.baseTableType(table.enumType))} `; | ||||||
if (protectTable) { | ||||||
statement += "if not exists "; | ||||||
} | ||||||
statement += `${this.resolveTarget(table.target)} `; | ||||||
if (table.bigquery && table.bigquery.partitionBy) { | ||||||
statement += `partition by ${table.bigquery.partitionBy} `; | ||||||
} | ||||||
if (table.bigquery && table.bigquery.clusterBy && table.bigquery.clusterBy.length > 0) { | ||||||
statement += `cluster by ${table.bigquery.clusterBy.join(", ")} `; | ||||||
} | ||||||
if (options.length > 0) { | ||||||
statement += `OPTIONS(${options.join(",")})`; | ||||||
} | ||||||
statement += `as ${table.query}`; | ||||||
return statement; | ||||||
} | ||||||
|
||||||
private createOrReplaceView(target: dataform.ITarget, query: string) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep this?