Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static LogicalPlan createLoadPlan(NereidsFileGroupInfo fileGroupInfo, Par
ImmutableList.of(Rewriter.bottomUp(
new BindExpression(),
new LoadProjectRewrite(fileGroupInfo.getTargetTable()),
new BindSink(),
new BindSink(false),
new AddPostFilter(
context.fileGroup.getWhereExpr()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
Expand Down Expand Up @@ -106,6 +107,15 @@
* bind an unbound logicalTableSink represent the target table of an insert command
*/
public class BindSink implements AnalysisRuleFactory {
public boolean needTruncateStringWhenInsert;

public BindSink() {
this(true);
}

public BindSink(boolean needTruncateStringWhenInsert) {
this.needTruncateStringWhenInsert = needTruncateStringWhenInsert;
}

@Override
public List<Rule> buildRules() {
Expand Down Expand Up @@ -298,6 +308,8 @@ private LogicalProject<?> getOutputProjectByCoercion(List<Column> tableSchema, L
int targetLength = ((CharacterType) targetType).getLen();
if (sourceLength == targetLength) {
castExpr = TypeCoercionUtils.castIfNotSameType(castExpr, targetType);
} else if (needTruncateStringWhenInsert && sourceLength > targetLength && targetLength >= 0) {
castExpr = new Substring(castExpr, Literal.of(1), Literal.of(targetLength));
} else if (targetType.isStringType()) {
castExpr = new Cast(castExpr, StringType.INSTANCE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
for (int i = 0; i < columns.size(); i++) {
Column column = columns.get(i);
NamedExpression defaultExpression = generateDefaultExpression(column);
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext);
}
} else {
if (CollectionUtils.isNotEmpty(unboundLogicalSink.getColNames())) {
Expand All @@ -409,14 +409,14 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
}
if (values.get(i) instanceof DefaultValueSlot) {
NamedExpression defaultExpression = generateDefaultExpression(sameNameColumn);
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
addColumnValue(
analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext
);
} else {
DataType targetType = DataType.fromCatalogType(sameNameColumn.getType());
Expression castValue = castValue(values.get(i), targetType);
castValue = rewriteContext == null
? castValue
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
addColumnValue(analyzer, optimizedRowConstructor, (NamedExpression) castValue);
addColumnValue(
analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext
);
}
}
} else {
Expand All @@ -432,14 +432,14 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
}
if (values.get(i) instanceof DefaultValueSlot) {
NamedExpression defaultExpression = generateDefaultExpression(columns.get(i));
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
addColumnValue(
analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext
);
} else {
DataType targetType = DataType.fromCatalogType(columns.get(i).getType());
Expression castValue = castValue(values.get(i), targetType);
castValue = rewriteContext == null
? castValue
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
addColumnValue(analyzer, optimizedRowConstructor, (NamedExpression) castValue);
addColumnValue(
analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext
);
}
}
}
Expand Down Expand Up @@ -519,26 +519,32 @@ public Expression visitUnboundStar(UnboundStar unboundStar, ExpressionRewriteCon
private static void addColumnValue(
Optional<ExpressionAnalyzer> analyzer,
ImmutableList.Builder<NamedExpression> optimizedRowConstructor,
NamedExpression value) {
NamedExpression value, DataType targetType, ExpressionRewriteContext rewriteContext) {
if (targetType != null) {
value = castValue(value, targetType);
}
if (analyzer.isPresent() && !(value instanceof Alias && value.child(0) instanceof Literal)) {
ExpressionAnalyzer expressionAnalyzer = analyzer.get();
value = (NamedExpression) expressionAnalyzer.analyze(
value, new ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
value, new ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
);
value = rewriteContext == null
? value
: (NamedExpression) FoldConstantRuleOnFE.evaluate(value, rewriteContext);
}
optimizedRowConstructor.add(value);
}

private static Alias castValue(Expression value, DataType targetType) {
private static NamedExpression castValue(Expression value, DataType targetType) {
if (value instanceof Alias) {
Expression oldChild = value.child(0);
Expression newChild = TypeCoercionUtils.castUnbound(oldChild, targetType);
return (Alias) (oldChild == newChild ? value : value.withChildren(newChild));
} else if (value instanceof UnboundAlias) {
UnboundAlias unboundAlias = (UnboundAlias) value;
return new Alias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
return new UnboundAlias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
} else {
return new Alias(TypeCoercionUtils.castUnbound(value, targetType));
return new UnboundAlias(TypeCoercionUtils.castUnbound(value, targetType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@
3 30 3 3 3.0 2000-01-03 5 5 5 5.0 2000-01-05 5

-- !select_all_default --
true 10 10000 10000000 92233720368547758 19223372036854775807 3.142 hello world, today is 15/06/2023 2023-06-15 2023-06-15T16:10:15
true 10 10000 10000000 92233720368547758 19223372036854775807 3.14159 hello world, today is 15/06/2023 2023-06-15 2023-06-15T16:10:15

-- !mv --
-4 -4 -4 d

-- !select_test_insert_cast_interval --
1 2020-02-02

-- !select_test_insert_more_string --
3 akljalkjbalkjsldkrjewokjf aa

Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,38 @@ suite('nereids_insert_into_values') {
sql "insert into agg_have_dup_base_value values (-4, -4, -4, 'd')"
sql "sync"
qt_mv "select * from agg_have_dup_base_value"

multi_sql """
DROP TABLE IF EXISTS test_insert_cast_interval;
CREATE TABLE test_insert_cast_interval (
`id` int NULL,
`dt` date NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`, `dt`)
DISTRIBUTED BY HASH(`id`) BUCKETS 10
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);

INSERT INTO test_insert_cast_interval values(1, date_floor('2020-02-02', interval 1 second));
"""

qt_select_test_insert_cast_interval "select * from test_insert_cast_interval"

multi_sql """
drop table if exists test_insert_more_string;
CREATE TABLE test_insert_more_string (
`r_regionkey` int NULL,
`r_name` varchar(25) NULL,
`r_comment` varchar(152) NULL
)
DUPLICATE KEY(`r_regionkey`)
DISTRIBUTED BY HASH(`r_regionkey`)
BUCKETS 1 PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
insert into test_insert_more_string values (3, "akljalkjbalkjsldkrjewokjfalksdjflaksjfdlaskjfalsdkfjalsdfjkasfdl", "aa")
"""

qt_select_test_insert_more_string "select * from test_insert_more_string"
}
Loading