Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Dec 6, 2024
1 parent 9e9dfe1 commit 4fe7f08
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.util.RelationUtil;
import org.apache.doris.nereids.util.TypeCoercionUtils;
import org.apache.doris.nereids.util.Utils;
import org.apache.doris.proto.InternalService;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.InsertStreamTxnExecutor;
Expand All @@ -80,6 +81,7 @@
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -353,14 +355,21 @@ public static Plan normalizePlan(Plan plan, TableIf table, Optional<InsertComman
);
}

Boolean[] outputSlotNullables = new Boolean[logicalInlineTable.getConstantExprsList().get(0).size()];
Arrays.fill(outputSlotNullables, false);

for (List<NamedExpression> values : logicalInlineTable.getConstantExprsList()) {
ImmutableList.Builder<NamedExpression> optimizedRowConstructor = ImmutableList.builder();
if (values.isEmpty()) {
if (CollectionUtils.isNotEmpty(unboundLogicalSink.getColNames())) {
throw new AnalysisException("value list should not be empty if columns are specified");
}
for (Column column : columns) {
optimizedRowConstructor.add(generateDefaultExpression(column));
for (int i = 0; i < columns.size(); i++) {
Column column = columns.get(i);
NamedExpression defaultExpression = generateDefaultExpression(column);
addColumnValue(
optimizedRowConstructor, outputSlotNullables, i, defaultExpression
);
}
} else {
if (CollectionUtils.isNotEmpty(unboundLogicalSink.getColNames())) {
Expand All @@ -386,14 +395,19 @@ public static Plan normalizePlan(Plan plan, TableIf table, Optional<InsertComman
+ "' in table '" + table.getName() + "' is not allowed.");
}
if (values.get(i) instanceof DefaultValueSlot) {
optimizedRowConstructor.add(generateDefaultExpression(sameNameColumn));
NamedExpression defaultExpression = generateDefaultExpression(sameNameColumn);
addColumnValue(
optimizedRowConstructor, outputSlotNullables, i, defaultExpression
);
} else {
DataType targetType = DataType.fromCatalogType(sameNameColumn.getType());
Expression castValue = castValue(values.get(i), targetType);
castValue = rewriteContext == null
? castValue
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
optimizedRowConstructor.add((NamedExpression) castValue);
addColumnValue(
optimizedRowConstructor, outputSlotNullables, i, (NamedExpression) castValue
);
}
}
} else {
Expand All @@ -408,22 +422,36 @@ public static Plan normalizePlan(Plan plan, TableIf table, Optional<InsertComman
+ "' in table '" + table.getName() + "' is not allowed.");
}
if (values.get(i) instanceof DefaultValueSlot) {
optimizedRowConstructor.add(generateDefaultExpression(columns.get(i)));
NamedExpression defaultExpression = generateDefaultExpression(columns.get(i));
addColumnValue(
optimizedRowConstructor, outputSlotNullables, i, defaultExpression
);
} else {
DataType targetType = DataType.fromCatalogType(columns.get(i).getType());
Expression castValue = castValue(values.get(i), targetType);
castValue = rewriteContext == null
? castValue
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
optimizedRowConstructor.add((NamedExpression) castValue);
addColumnValue(
optimizedRowConstructor, outputSlotNullables, i, (NamedExpression) castValue
);
}
}
}
}
optimizedRowConstructors.add(optimizedRowConstructor.build());
}

return plan.withChildren(new LogicalInlineTable(optimizedRowConstructors.build()));
return plan.withChildren(new LogicalInlineTable(optimizedRowConstructors.build(), Optional.of(
Utils.fastToImmutableList(outputSlotNullables)
)));
}

private static void addColumnValue(
ImmutableList.Builder<NamedExpression> optimizedRowConstructor,
Boolean[] nullable, int index, NamedExpression value) {
optimizedRowConstructor.add(value);
nullable[index] |= value.nullable();
}

private static Expression castValue(Expression value, DataType targetType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package org.apache.doris.nereids.trees.plans.logical;

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.properties.LogicalProperties;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.BlockFuncDepsPropagation;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PlanType;
Expand All @@ -33,7 +35,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* represent value list such as values(1), (2), (3) will generate LogicalInlineTable((1), (2), (3)).
Expand All @@ -42,15 +43,27 @@ public class LogicalInlineTable extends LogicalLeaf implements BlockFuncDepsProp

private final List<List<NamedExpression>> constantExprsList;

private final Optional<List<Boolean>> outputNullables;

public LogicalInlineTable(List<List<NamedExpression>> constantExprsList) {
this(constantExprsList, Optional.empty(), Optional.empty());
this(constantExprsList, Optional.empty(), Optional.empty(), Optional.empty());
}

public LogicalInlineTable(List<List<NamedExpression>> constantExprsList, Optional<List<Boolean>> outputNullables) {
this(constantExprsList, outputNullables, Optional.empty(), Optional.empty());
}

public LogicalInlineTable(List<List<NamedExpression>> constantExprsList,
Optional<List<Boolean>> outputNullables,
Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties) {
super(PlanType.LOGICAL_INLINE_TABLE, groupExpression, logicalProperties);
this.constantExprsList = ImmutableList.copyOf(

if (constantExprsList.isEmpty()) {
throw new AnalysisException("constantExprsList should now be empty");
}
this.outputNullables = Objects.requireNonNull(outputNullables, "outputNullables should not be null");
this.constantExprsList = Utils.fastToImmutableList(
Objects.requireNonNull(constantExprsList, "constantExprsList should not be null"));
}

Expand All @@ -70,21 +83,49 @@ public List<? extends Expression> getExpressions() {

@Override
public Plan withGroupExpression(Optional<GroupExpression> groupExpression) {
return new LogicalInlineTable(constantExprsList, groupExpression, Optional.of(getLogicalProperties()));
return new LogicalInlineTable(
constantExprsList, outputNullables, groupExpression, Optional.of(getLogicalProperties())
);
}

@Override
public Plan withGroupExprLogicalPropChildren(Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties, List<Plan> children) {
return new LogicalInlineTable(constantExprsList, groupExpression, logicalProperties);
if (!children.isEmpty()) {
throw new AnalysisException("children should not be empty");
}
return new LogicalInlineTable(constantExprsList, outputNullables, groupExpression, logicalProperties);
}

@Override
public List<Slot> computeOutput() {
return constantExprsList.get(0)
.stream()
.map(NamedExpression::toSlot)
.collect(Collectors.toList());
if (outputNullables.isPresent()) {
List<Boolean> nullables = outputNullables.get();
int columnNum = constantExprsList.get(0).size();
List<NamedExpression> firstRow = constantExprsList.get(0);
ImmutableList.Builder<Slot> output = ImmutableList.builderWithExpectedSize(constantExprsList.size());
for (int i = 0; i < columnNum; i++) {
NamedExpression firstRowColumn = firstRow.get(i);
output.add(new SlotReference(firstRowColumn.getName(), firstRowColumn.getDataType(), nullables.get(i)));
}
return output.build();
} else {
int columnNum = constantExprsList.get(0).size();
List<NamedExpression> firstRow = constantExprsList.get(0);
ImmutableList.Builder<Slot> output = ImmutableList.builderWithExpectedSize(constantExprsList.size());
for (int i = 0; i < columnNum; i++) {
NamedExpression firstRowColumn = firstRow.get(i);
boolean nullable = false;
for (List<NamedExpression> row : constantExprsList) {
if (row.get(i).nullable()) {
nullable = true;
break;
}
}
output.add(new SlotReference(firstRowColumn.getName(), firstRowColumn.getDataType(), nullable));
}
return output.build();
}
}

@Override
Expand Down

0 comments on commit 4fe7f08

Please sign in to comment.