Skip to content

Commit

Permalink
Support PostgreSQL do nothing conflict action (#31048)
Browse files Browse the repository at this point in the history
  • Loading branch information
TherChenYang authored Apr 29, 2024
1 parent a6962d2 commit b910c87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,10 @@ public ASTNode visitInsert(final InsertContext ctx) {

@Override
public ASTNode visitOptOnConflict(final OptOnConflictContext ctx) {
SetClauseListContext setClauseListContext = ctx.setClauseList();
Collection<ColumnAssignmentSegment> assignments = ((SetAssignmentSegment) visit(setClauseListContext)).getAssignments();
Collection<ColumnAssignmentSegment> assignments = new LinkedList<>();
if (null != ctx.setClauseList()) {
assignments = ((SetAssignmentSegment) visit(ctx.setClauseList())).getAssignments();
}
return new OnDuplicateKeyColumnsSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), assignments);
}

Expand Down
16 changes: 16 additions & 0 deletions test/it/parser/src/main/resources/case/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4140,4 +4140,20 @@
</value>
</values>
</insert>

<insert sql-case-id="insert_into_with_conflict_action_do_nothing" parameters="1">
<table name="sj_event" start-index="12" stop-index="19" />
<columns start-index="20" stop-index="29">
<column name="event_id" start-index="21" stop-index="28" />
</columns>
<values>
<value>
<assignment-value>
<parameter-marker-expression parameter-index="0" start-index="39" stop-index="39" />
<literal-expression value="1" start-index="39" stop-index="39" />
</assignment-value>
</value>
</values>
<on-duplicate-key-columns start-index="42" stop-index="73"/>
</insert>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,5 @@
<sql-case id="insert_with_output_input" value="INSERT Production.ScrapReason OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate INTO @MyTableVar VALUES (N'Operator error', GETDATE())" db-types="SQLServer"/>
<sql-case id="insert_into_with_multi_nchar" value="INSERT INTO Production.UnitMeasure VALUES (N'FT2', N'Square Feet ', '20080923'), (N'Y', N'Yards', '20080923'), (N'Y3', N'Cubic Yards', '20080923')" db-types="SQLServer"/>
<sql-case id="insert_into_with_select_with_merge" value="INSERT INTO Production.ZeroInventory (DeletedProductID, RemovedOnDate) SELECT ProductID, GETDATE() FROM (MERGE Production.ProductInventory AS pi USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod JOIN Sales.SalesOrderHeader AS soh ON sod.SalesOrderID = soh.SalesOrderID AND soh.OrderDate = '20070401' GROUP BY ProductID) AS src (ProductID, OrderQty) ON (pi.ProductID = src.ProductID) WHEN MATCHED AND pi.Quantity - src.OrderQty &lt;= 0 THEN DELETE WHEN MATCHED THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQty OUTPUT $action, deleted.ProductID) AS Changes (Action, ProductID) WHERE Action = 'DELETE'" db-types="SQLServer"/>
<sql-case id="insert_into_with_conflict_action_do_nothing" value="INSERT INTO sj_event(event_id) VALUES (?) ON CONFLICT(event_id) DO NOTHING" db-types="PostgreSQL"/>
</sql-cases>

0 comments on commit b910c87

Please sign in to comment.