Skip to content

Commit

Permalink
[fix](Nereids) set operation output nullable maybe wrong (#39109) (#3…
Browse files Browse the repository at this point in the history
…9444)

pick from master #39109

when first regulator child output nullable is not right, we may get
wrong nullable output, and lead be crash
  • Loading branch information
morrySnow authored Aug 16, 2024
1 parent d6a0469 commit f247513
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.doris.nereids.trees.expressions.OrderExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
import org.apache.doris.nereids.trees.expressions.functions.Function;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
import org.apache.doris.nereids.trees.plans.Plan;
Expand Down Expand Up @@ -57,7 +56,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* because some rule could change output's nullable.
Expand Down Expand Up @@ -145,8 +143,10 @@ public Plan visitLogicalSetOperation(LogicalSetOperation setOperation, Map<ExprI
ImmutableList.Builder<List<SlotReference>> newChildrenOutputs = ImmutableList.builder();
List<Boolean> inputNullable = null;
if (!setOperation.children().isEmpty()) {
inputNullable = setOperation.getRegularChildOutput(0).stream()
.map(ExpressionTrait::nullable).collect(Collectors.toList());
inputNullable = Lists.newArrayListWithCapacity(setOperation.getOutputs().size());
for (int i = 0; i < setOperation.getOutputs().size(); i++) {
inputNullable.add(false);
}
for (int i = 0; i < setOperation.arity(); i++) {
List<Slot> childOutput = setOperation.child(i).getOutput();
List<SlotReference> setChildOutput = setOperation.getRegularChildOutput(i);
Expand Down Expand Up @@ -262,13 +262,6 @@ private <T extends Expression> Set<T> updateExpressions(Set<T> inputs, Map<ExprI
return inputs.stream().map(i -> updateExpression(i, replaceMap)).collect(ImmutableSet.toImmutableSet());
}

private Map<ExprId, Slot> collectChildrenOutputMap(LogicalPlan plan) {
return plan.children().stream()
.map(Plan::getOutputSet)
.flatMap(Set::stream)
.collect(Collectors.toMap(NamedExpression::getExprId, s -> s));
}

private static class SlotReferenceReplacer extends DefaultExpressionRewriter<Map<ExprId, Slot>> {
public static SlotReferenceReplacer INSTANCE = new SlotReferenceReplacer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,84 @@
suite("test_set_operation_adjust_nullable") {

sql """
DROP TABLE IF EXISTS t1
DROP TABLE IF EXISTS set_operation_t1
"""
sql """
DROP TABLE IF EXISTS t2
DROP TABLE IF EXISTS set_operation_t2
"""

sql """
CREATE TABLE t1(c1 varchar) DISTRIBUTED BY hash(c1) PROPERTIES ("replication_num" = "1");
CREATE TABLE set_operation_t1(c1 varchar) DISTRIBUTED BY hash(c1) PROPERTIES ("replication_num" = "1");
"""

sql """
CREATE TABLE t2(c2 date) DISTRIBUTED BY hash(c2) PROPERTIES ("replication_num" = "1");
CREATE TABLE set_operation_t2(c2 date) DISTRIBUTED BY hash(c2) PROPERTIES ("replication_num" = "1");
"""

sql """
insert into t1 values('+06-00');
insert into set_operation_t1 values('+06-00');
"""

sql """
insert into t2 values('1990-11-11');
insert into set_operation_t2 values('1990-11-11');
"""

sql """
SELECT c1, c1 FROM t1 EXCEPT SELECT c2, c2 FROM t2;
"""

// do not use regulator child output nullable as init nullable info

sql """
DROP TABLE IF EXISTS set_operation_t1
"""
sql """
DROP TABLE IF EXISTS set_operation_t2
"""

sql """
create table set_operation_t1 (
pk int,
c1 char(25) not null ,
c2 varchar(100) null ,
)
distributed by hash(pk) buckets 10
properties("replication_num" = "1");
"""

sql """insert into set_operation_t1 values (1, '1', '1');"""

sql """
create table set_operation_t2 (
c3 varchar(100) not null ,
pk int
)
distributed by hash(pk) buckets 10
properties("replication_num" = "1");
"""

sql """insert into set_operation_t2 values ('1', 1);"""

sql """
select
c2,
c1
from
set_operation_t1
order by
1,
2 asc
limit
0
union distinct
select
c3,
c3
from
set_operation_t2
except
select
'LDvlqYTfrq',
'rVdUjeSaJW';
"""
}

0 comments on commit f247513

Please sign in to comment.