@@ -751,9 +751,9 @@ public MergeWriterNode plan(Merge merge)
751
751
.process (merge .getTarget ());
752
752
753
753
// Assign a unique id to every target table row
754
- Symbol uniqueIdSymbol = symbolAllocator .newSymbol ("unique_id " , BIGINT );
754
+ Symbol targetUniqueIdSymbol = symbolAllocator .newSymbol ("target_unique_id " , BIGINT );
755
755
RelationPlan planWithUniqueId = new RelationPlan (
756
- new AssignUniqueId (idAllocator .getNextId (), targetTablePlan .getRoot (), uniqueIdSymbol ),
756
+ new AssignUniqueId (idAllocator .getNextId (), targetTablePlan .getRoot (), targetUniqueIdSymbol ),
757
757
mergeAnalysis .getTargetTableScope (),
758
758
targetTablePlan .getFieldMappings (),
759
759
outerContext );
@@ -774,8 +774,16 @@ public MergeWriterNode plan(Merge merge)
774
774
RelationPlan source = new RelationPlanner (analysis , symbolAllocator , idAllocator , lambdaDeclarationToSymbolMap , plannerContext , outerContext , session , recursiveSubqueries )
775
775
.process (merge .getSource ());
776
776
777
+ // Assign a unique id to every source table row
778
+ Symbol sourceUniqueIdSymbol = symbolAllocator .newSymbol ("source_unique_id" , BIGINT );
779
+ RelationPlan sourcePlanWithUniqueId = new RelationPlan (
780
+ new AssignUniqueId (idAllocator .getNextId (), source .getRoot (), sourceUniqueIdSymbol ),
781
+ source .getScope (),
782
+ source .getFieldMappings (),
783
+ outerContext );
784
+
777
785
RelationPlan joinPlan = new RelationPlanner (analysis , symbolAllocator , idAllocator , lambdaDeclarationToSymbolMap , plannerContext , outerContext , session , recursiveSubqueries )
778
- .planJoin (merge .getPredicate (), Join .Type .RIGHT , mergeAnalysis .getJoinScope (), planWithPresentColumn , source , analysis .getSubqueries (merge )); // TODO: ir
786
+ .planJoin (merge .getPredicate (), Join .Type .RIGHT , mergeAnalysis .getJoinScope (), planWithPresentColumn , sourcePlanWithUniqueId , analysis .getSubqueries (merge )); // TODO: ir
779
787
780
788
PlanBuilder subPlan = newPlanBuilder (joinPlan , analysis , lambdaDeclarationToSymbolMap , session , plannerContext );
781
789
@@ -864,10 +872,10 @@ public MergeWriterNode plan(Merge merge)
864
872
865
873
List <io .trino .sql .tree .Expression > constraints = analysis .getCheckConstraints (mergeAnalysis .getTargetTable ());
866
874
if (!constraints .isEmpty ()) {
867
- assignments .putIdentity (uniqueIdSymbol );
875
+ assignments .putIdentity (targetUniqueIdSymbol );
868
876
assignments .putIdentity (presentColumn );
869
877
assignments .putIdentity (rowIdSymbol );
870
- assignments .putIdentities (source .getFieldMappings ());
878
+ assignments .putIdentities (sourcePlanWithUniqueId .getFieldMappings ());
871
879
subPlan = subPlan .withNewRoot (new ProjectNode (
872
880
idAllocator .getNextId (),
873
881
subPlan .getRoot (),
@@ -887,6 +895,7 @@ public MergeWriterNode plan(Merge merge)
887
895
.build ()),
888
896
null ));
889
897
898
+ Symbol uniqueIdSymbol = symbolAllocator .newSymbol ("unique_id" , BIGINT );
890
899
Symbol mergeRowSymbol = symbolAllocator .newSymbol ("merge_row" , mergeAnalysis .getMergeRowType ());
891
900
Symbol caseNumberSymbol = symbolAllocator .newSymbol ("case_number" , INTEGER );
892
901
@@ -897,7 +906,9 @@ public MergeWriterNode plan(Merge merge)
897
906
Symbol symbol = planWithPresentColumn .getFieldMappings ().get (fieldIndex );
898
907
projectionAssignmentsBuilder .putIdentity (symbol );
899
908
}
900
- projectionAssignmentsBuilder .putIdentity (uniqueIdSymbol );
909
+
910
+ Expression uniqueIdExpression = new Coalesce (targetUniqueIdSymbol .toSymbolReference (), sourceUniqueIdSymbol .toSymbolReference ());
911
+ projectionAssignmentsBuilder .put (uniqueIdSymbol , uniqueIdExpression );
901
912
projectionAssignmentsBuilder .putIdentity (rowIdSymbol );
902
913
projectionAssignmentsBuilder .put (mergeRowSymbol , caseExpression );
903
914
0 commit comments