Skip to content

Commit 94fb806

Browse files
committed
[fix](fe) Align Paimon MERGE input layout on branch-4.2
### What problem does this PR solve? Issue Number: N/A Related PR: #70103, #69343 Problem Summary: The backported Paimon merge planner adds its target-presence marker to the left join input, but PaimonMergeCommand still places the source on the left. Unmatched source rows therefore select MATCHED and write NULL target keys instead of inserting. Reuse MergeUtils to keep the target on the left and preserve the source with RIGHT OUTER JOIN when INSERT is present. Add command-level tests for both join layouts. Keep logical splits when the compatibility case asserts Rust execution and use the 4.2 MERGE diagnostics in nullable-key error expectations. ### Release note Fix Paimon MERGE INTO incorrectly classifying unmatched source rows as matched on branch-4.2, including NULL key writes to non-nullable primary keys. ### Check List (For Author) - Test: FE/JNI build and Checkstyle; 490 FE unit tests passed, 2 environment-gated tests skipped; 47 regression framework unit tests passed; all 6 affected Paimon regression suites passed against existing golden outputs using the PR's successful CI ASAN BE and the repository's Spark/Paimon fixtures. - Behavior changed: Yes, unmatched Paimon MERGE source rows select INSERT. - Does this need documentation: No, restores the intended MERGE semantics.
1 parent 2d91fb8 commit 94fb806

4 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/PaimonMergeCommand.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@
2222
import org.apache.doris.nereids.analyzer.UnboundRelation;
2323
import org.apache.doris.nereids.analyzer.UnboundSlot;
2424
import org.apache.doris.nereids.parser.LogicalPlanBuilderAssistant;
25-
import org.apache.doris.nereids.rules.exploration.join.JoinReorderContext;
2625
import org.apache.doris.nereids.trees.expressions.EqualTo;
2726
import org.apache.doris.nereids.trees.expressions.Expression;
2827
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
2928
import org.apache.doris.nereids.trees.plans.Explainable;
30-
import org.apache.doris.nereids.trees.plans.JoinType;
3129
import org.apache.doris.nereids.trees.plans.Plan;
3230
import org.apache.doris.nereids.trees.plans.PlanType;
3331
import org.apache.doris.nereids.trees.plans.commands.info.PaimonRowChangeSpec;
3432
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
3533
import org.apache.doris.nereids.trees.plans.commands.merge.MergeMatchedClause;
3634
import org.apache.doris.nereids.trees.plans.commands.merge.MergeNotMatchedClause;
37-
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
35+
import org.apache.doris.nereids.trees.plans.commands.merge.MergeUtils;
3836
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
3937
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
4038
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
@@ -100,10 +98,7 @@ private LogicalPlan generateBasePlan() {
10098
if (targetAlias.isPresent()) {
10199
target = new LogicalSubQueryAlias<>(targetAlias.get(), target);
102100
}
103-
JoinType joinType = notMatchedClauses.isEmpty()
104-
? JoinType.INNER_JOIN : JoinType.LEFT_OUTER_JOIN;
105-
return new LogicalJoin<>(joinType, ImmutableList.of(), ImmutableList.of(onClause),
106-
source, target, JoinReorderContext.EMPTY);
101+
return MergeUtils.buildMergeJoin(target, source, onClause, !notMatchedClauses.isEmpty());
107102
}
108103

109104
@Override
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
21+
import org.apache.doris.nereids.trees.plans.JoinType;
22+
import org.apache.doris.nereids.trees.plans.RelationId;
23+
import org.apache.doris.nereids.trees.plans.commands.merge.MergeMatchedClause;
24+
import org.apache.doris.nereids.trees.plans.commands.merge.MergeNotMatchedClause;
25+
import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation;
26+
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
27+
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
28+
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
29+
import org.apache.doris.qe.ConnectContext;
30+
31+
import com.google.common.collect.ImmutableList;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
35+
import java.util.Optional;
36+
37+
class PaimonMergeCommandTest {
38+
@Test
39+
void matchedOnlyMergeKeepsTargetOnLeft() {
40+
assertMergeJoin(false, JoinType.INNER_JOIN);
41+
}
42+
43+
@Test
44+
void mergeWithInsertPreservesSourceOnRight() {
45+
assertMergeJoin(true, JoinType.RIGHT_OUTER_JOIN);
46+
}
47+
48+
private void assertMergeJoin(boolean hasInsert, JoinType expectedType) {
49+
LogicalPlan source = new LogicalEmptyRelation(new RelationId(1), ImmutableList.of());
50+
PaimonMergeCommand command = new PaimonMergeCommand(ImmutableList.of("target"),
51+
Optional.of("t"), Optional.empty(), source, BooleanLiteral.TRUE,
52+
ImmutableList.of(new MergeMatchedClause(Optional.empty(), ImmutableList.of(), true)),
53+
hasInsert ? ImmutableList.of(new MergeNotMatchedClause(Optional.empty(),
54+
ImmutableList.of(), ImmutableList.of())) : ImmutableList.of());
55+
LogicalJoin<?, ?> join = (LogicalJoin<?, ?>) command.getExplainPlan(new ConnectContext()).child(0);
56+
57+
Assertions.assertEquals(expectedType, join.getJoinType());
58+
Assertions.assertSame(source, join.right());
59+
Assertions.assertInstanceOf(LogicalSubQueryAlias.class, join.left());
60+
Assertions.assertEquals("t", ((LogicalSubQueryAlias<?>) join.left()).getAlias());
61+
Assertions.assertEquals(ImmutableList.of(BooleanLiteral.TRUE), join.getOtherJoinConjuncts());
62+
}
63+
}

‎regression-test/suites/external_table_p0/paimon/test_paimon_2_1_compatibility.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ suite("test_paimon_2_1_compatibility", "p0,external,paimon") {
5858
'bucket'='1', 'write-only'='true', 'deletion-vectors.enabled'='false')"""
5959
sql "INSERT INTO nullable_keys_single VALUES (NULL,10), (1,100), (NULL,20), (1,200)"
6060
order_qt_nullable_single_jni "SELECT id,v FROM nullable_keys_single ORDER BY id NULLS FIRST"
61-
sql "set force_jni_scanner=false"
61+
// Keep logical splits: native Parquet bypasses the JNI/Rust reader selection.
6262
sql "set enable_paimon_rust_reader=true"
6363
order_qt_nullable_keys_rust "SELECT id,v FROM nullable_keys_single ORDER BY id NULLS FIRST"
6464
def queryId = sql("select last_query_id()")[0][0].toString()

‎regression-test/suites/external_table_p0/paimon/test_paimon_nullable_key_merge.groovy‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ suite("test_paimon_nullable_key_merge", "p0,external,paimon") {
8686
USING (SELECT CAST(NULL AS INT) AS id, 101 AS v
8787
UNION ALL SELECT CAST(NULL AS INT), 102) s ON t.id <=> s.id
8888
WHEN MATCHED THEN UPDATE SET v=s.v"""
89-
exception "Connector MERGE matched one target row with multiple source rows"
89+
exception "Paimon MERGE matched one target row with multiple source rows"
9090
}
9191
order_qt_null_key_duplicate_unchanged "SELECT * FROM nullable_target ORDER BY id NULLS FIRST"
9292
test {
9393
sql """MERGE INTO nullable_target t
9494
USING (SELECT CAST(NULL AS INT) AS id, 101 AS v
9595
UNION ALL SELECT CAST(NULL AS INT), 102) s ON t.id = s.id
9696
WHEN NOT MATCHED THEN INSERT (id,v) VALUES (s.id,s.v)"""
97-
exception "Connector MERGE attempted to insert multiple rows with the same primary key"
97+
exception "Paimon MERGE attempted to insert multiple rows with the same primary key"
9898
}
9999
order_qt_duplicate_null_insert_unchanged "SELECT * FROM nullable_target ORDER BY id NULLS FIRST"
100100
sql """MERGE INTO nullable_target t

0 commit comments

Comments
 (0)