Skip to content

Commit

Permalink
Support OVERRIDING clause in MERGE..INSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 8, 2024
1 parent e2e2cf5 commit 9c7b174
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/cst/Merge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Alias } from "./Alias";
import { BaseNode, Keyword } from "./Base";
import { Expr, Identifier, ListExpr, ParenExpr, EntityName } from "./Expr";
import { DefaultValues, ValuesClause } from "./Insert";
import { DefaultValues, OverridingClause, ValuesClause } from "./Insert";
import { SelectStmt, WithClause } from "./Select";
import { SetClause } from "./Update";

Expand Down Expand Up @@ -82,7 +82,12 @@ export interface MergeActionInsert extends BaseNode {
type: "merge_action_insert";
insertKw: Keyword<"INSERT">;
columns?: ParenExpr<ListExpr<Identifier>>;
clauses: (ValuesClause | DefaultValues | MergeActionInsertRowClause)[];
clauses: (
| OverridingClause
| ValuesClause
| DefaultValues
| MergeActionInsertRowClause
)[];
}

// BigQuery
Expand Down
6 changes: 5 additions & 1 deletion src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,12 +1733,16 @@ merge_action_update

merge_action_insert
= insertKw:(INSERT __) columns:(paren$list$column __)?
overriding:(overriding_clause __)?
values:(values_clause / default_values / merge_action_insert_row_clause) {
return loc({
type: "merge_action_insert",
insertKw: read(insertKw),
columns: read(columns),
clauses: [values],
clauses: [
read(overriding),
values
].filter(identity),
});
}

Expand Down
13 changes: 13 additions & 0 deletions test/dml/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ describe("merge into", () => {
it("supports INSERT DEFAULT VALUES", () => {
testWc("MERGE INTO foo USING bar ON x=y WHEN NOT MATCHED THEN INSERT DEFAULT VALUES");
});

it("supports INSERT OVERRIDING SYSTEM/USER VALUE", () => {
testWc(`
MERGE INTO foo USING bar ON x=y
WHEN NOT MATCHED THEN
INSERT OVERRIDING SYSTEM VALUE VALUES (1, 2)
`);
testWc(`
MERGE INTO foo USING bar ON x=y
WHEN NOT MATCHED THEN
INSERT OVERRIDING USER VALUE VALUES (1, 2)
`);
});
});

dialect("bigquery", () => {
Expand Down

0 comments on commit 9c7b174

Please sign in to comment.