Skip to content

Commit 43f9ceb

Browse files
authored
[fix](join) Preserve input nullability for ASOF match conditions (#69630)
### What problem does this PR solve? Issue Number: N/A Related PR: #69616 Problem Summary: Outer ASOF joins can declare a NOT NULL input expression as nullable while BE evaluates the match key before null padding. Root cause: the final `AdjustNullable` pass uses join output nullability for the match condition. Preserve child input nullability for ASOF match conditions, retaining existing behavior for ordinary joins. ### Release note Fix incorrect nullability in ASOF JOIN match conditions. ### Check List (For Author) - Test: Added FE unit coverage for all four ASOF join types and a regular LEFT OUTER JOIN control; tests not run locally. Targeted Checkstyle passed with zero violations. - Manual diagnosis: The full CTAS reproducer fails on master with nullable output normalization removed (`declared=Nullable(DateTimeV2(0)), actual=DATETIMEV2`). No post-fix build or runtime validation performed. - Behavior changed: Yes. ASOF match conditions retain child input nullability. - Does this need documentation: No
1 parent 94ad139 commit 43f9ceb

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/AdjustNullable.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ public Plan visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join, M
194194
// then later at rewrite phase, the join conditions may push down, and the push down condition with proper
195195
// nullable attribute.
196196
// 2) when at the end of rewrite phase, update other conjuncts with join output nullables.
197-
// Just change it to be consistent with BE.
197+
// Just change it to be consistent with BE. ASOF match conditions are evaluated on child input blocks before
198+
// outer-join null padding, so they keep the child nullable attributes in both phases.
198199
Optional<List<Expression>> otherConjuncts = Optional.empty();
199-
if (isAnalyzedPhase) {
200+
boolean useJoinOutputNullableForOtherConjuncts = !isAnalyzedPhase && !join.getJoinType().isAsofJoin();
201+
if (!useJoinOutputNullableForOtherConjuncts) {
200202
otherConjuncts = doUpdateExpressions(join.getOtherJoinConjuncts(), replaceMap, false);
201203
}
202204
for (Slot slot : join.getOutput()) {
@@ -205,7 +207,7 @@ public Plan visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join, M
205207
if (!hadUpdatedMarkConjuncts) {
206208
markConjuncts = doUpdateExpressions(join.getMarkJoinConjuncts(), replaceMap, false);
207209
}
208-
if (!isAnalyzedPhase) {
210+
if (useJoinOutputNullableForOtherConjuncts) {
209211
otherConjuncts = doUpdateExpressions(join.getOtherJoinConjuncts(), replaceMap, false);
210212
}
211213
if (!hashConjuncts.isPresent() && !markConjuncts.isPresent() && !otherConjuncts.isPresent()) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.rules.rewrite;
19+
20+
import org.apache.doris.nereids.trees.expressions.EqualTo;
21+
import org.apache.doris.nereids.trees.expressions.Expression;
22+
import org.apache.doris.nereids.trees.expressions.GreaterThan;
23+
import org.apache.doris.nereids.trees.expressions.SlotReference;
24+
import org.apache.doris.nereids.trees.plans.JoinType;
25+
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
26+
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
27+
import org.apache.doris.nereids.util.LogicalPlanBuilder;
28+
import org.apache.doris.nereids.util.PlanConstructor;
29+
30+
import com.google.common.collect.ImmutableList;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
33+
34+
import java.util.HashMap;
35+
36+
class AdjustNullableTest {
37+
38+
@Test
39+
void testAsofMatchConditionUsesChildNullability() {
40+
assertOtherConjunctNullability(JoinType.ASOF_LEFT_INNER_JOIN, false);
41+
assertOtherConjunctNullability(JoinType.ASOF_RIGHT_INNER_JOIN, false);
42+
assertOtherConjunctNullability(JoinType.ASOF_LEFT_OUTER_JOIN, false);
43+
assertOtherConjunctNullability(JoinType.ASOF_RIGHT_OUTER_JOIN, false);
44+
}
45+
46+
@Test
47+
void testRegularOuterJoinOtherConditionUsesJoinOutputNullability() {
48+
assertOtherConjunctNullability(JoinType.LEFT_OUTER_JOIN, true);
49+
}
50+
51+
private void assertOtherConjunctNullability(JoinType joinType, boolean rightNullable) {
52+
LogicalOlapScan left = new LogicalOlapScan(PlanConstructor.getNextRelationId(), PlanConstructor.student,
53+
ImmutableList.of(""));
54+
LogicalOlapScan right = new LogicalOlapScan(PlanConstructor.getNextRelationId(), PlanConstructor.student,
55+
ImmutableList.of(""));
56+
Expression hashCondition = new EqualTo(left.getOutput().get(0), right.getOutput().get(0));
57+
Expression matchCondition = new GreaterThan(left.getOutput().get(1), right.getOutput().get(1));
58+
LogicalJoin<?, ?> join = (LogicalJoin<?, ?>) new LogicalPlanBuilder(left)
59+
.join(right, joinType, ImmutableList.of(hashCondition), ImmutableList.of(matchCondition))
60+
.build();
61+
62+
LogicalJoin<?, ?> adjustedJoin = AdjustNullable.doVisitLogicalJoin(join, new HashMap<>(), false, false);
63+
GreaterThan adjustedCondition = (GreaterThan) adjustedJoin.getOtherJoinConjuncts().get(0);
64+
65+
Assertions.assertFalse(((SlotReference) adjustedCondition.left()).nullable());
66+
Assertions.assertEquals(rightNullable, ((SlotReference) adjustedCondition.right()).nullable());
67+
}
68+
}

0 commit comments

Comments
 (0)