From ce983c07857edbac45b36e82f0373e4b414b3180 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Fri, 4 Jul 2025 19:23:14 +0800 Subject: [PATCH 1/5] fix insert interval --- .../insert_into_table/insert_values.out | 5 ++++- .../insert_into_table/insert_values.groovy | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/regression-test/data/nereids_p0/insert_into_table/insert_values.out b/regression-test/data/nereids_p0/insert_into_table/insert_values.out index 36d51ffcb2a6fc..184079b2af3927 100644 --- a/regression-test/data/nereids_p0/insert_into_table/insert_values.out +++ b/regression-test/data/nereids_p0/insert_into_table/insert_values.out @@ -47,8 +47,11 @@ 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 + diff --git a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy index 84461ca2ba8476..6f3b1887d832b1 100644 --- a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy +++ b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy @@ -143,4 +143,21 @@ 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" } \ No newline at end of file From 9122319eb2f8ffd3fdf80428710fdd1ae3e8641a Mon Sep 17 00:00:00 2001 From: 924060929 Date: Fri, 4 Jul 2025 19:38:20 +0800 Subject: [PATCH 2/5] fix insert into values --- .../nereids/rules/analysis/BindSink.java | 3 ++ .../plans/commands/insert/InsertUtils.java | 33 +++++++++---------- .../insert_into_table/insert_values.out | 3 ++ .../insert_into_table/insert_values.groovy | 17 ++++++++++ 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java index 5eb3e9883cb36f..e74accc8dee9e8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java @@ -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; @@ -298,6 +299,8 @@ private LogicalProject getOutputProjectByCoercion(List tableSchema, L int targetLength = ((CharacterType) targetType).getLen(); if (sourceLength == targetLength) { castExpr = TypeCoercionUtils.castIfNotSameType(castExpr, targetType); + } else if (sourceLength > targetLength && targetLength >= 0) { + castExpr = new Substring(castExpr, Literal.of(1), Literal.of(targetLength)); } else if (targetType.isStringType()) { castExpr = new Cast(castExpr, StringType.INSTANCE); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java index c3a441bd68c433..d635d99b54b0ea 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java @@ -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())) { @@ -409,14 +409,10 @@ 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 { @@ -432,14 +428,10 @@ 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); } } } @@ -519,26 +511,33 @@ public Expression visitUnboundStar(UnboundStar unboundStar, ExpressionRewriteCon private static void addColumnValue( Optional analyzer, ImmutableList.Builder optimizedRowConstructor, - NamedExpression value) { + NamedExpression value, DataType targetType, ExpressionRewriteContext rewriteContext) { if (analyzer.isPresent() && !(value instanceof Alias && value.child(0) instanceof Literal)) { ExpressionAnalyzer expressionAnalyzer = analyzer.get(); value = (NamedExpression) expressionAnalyzer.analyze( value, new ExpressionRewriteContext(expressionAnalyzer.getCascadesContext()) ); } + + if (targetType != null) { + value = castValue(value, targetType); + 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)); } } diff --git a/regression-test/data/nereids_p0/insert_into_table/insert_values.out b/regression-test/data/nereids_p0/insert_into_table/insert_values.out index 184079b2af3927..62d824e5e6f8cc 100644 --- a/regression-test/data/nereids_p0/insert_into_table/insert_values.out +++ b/regression-test/data/nereids_p0/insert_into_table/insert_values.out @@ -55,3 +55,6 @@ true 10 10000 10000000 92233720368547758 19223372036854775807 3.14159 hello worl -- !select_test_insert_cast_interval -- 1 2020-02-02 +-- !select_test_insert_more_string -- +3 akljalkjbalkjsldkrjewokjf aa + diff --git a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy index 6f3b1887d832b1..d6da58ea4fddad 100644 --- a/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy +++ b/regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy @@ -160,4 +160,21 @@ suite('nereids_insert_into_values') { """ 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" } \ No newline at end of file From e06f84c57764686d401139cc197bb0efcd246d03 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Fri, 4 Jul 2025 19:49:35 +0800 Subject: [PATCH 3/5] fix --- .../trees/plans/commands/insert/InsertUtils.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java index d635d99b54b0ea..ce8e0edd824276 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java @@ -409,10 +409,14 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table, } if (values.get(i) instanceof DefaultValueSlot) { NamedExpression defaultExpression = generateDefaultExpression(sameNameColumn); - addColumnValue(analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext); + addColumnValue( + analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext + ); } else { DataType targetType = DataType.fromCatalogType(sameNameColumn.getType()); - addColumnValue(analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext); + addColumnValue( + analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext + ); } } } else { @@ -428,10 +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, null, rewriteContext); + addColumnValue( + analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext + ); } else { DataType targetType = DataType.fromCatalogType(columns.get(i).getType()); - addColumnValue(analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext); + addColumnValue( + analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext + ); } } } From 5ac054463be4e927da785391afd5c06e018359d1 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Wed, 9 Jul 2025 16:57:31 +0800 Subject: [PATCH 4/5] fix --- .../nereids/trees/plans/commands/insert/InsertUtils.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java index ce8e0edd824276..83251dd0ba1b9a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java @@ -520,15 +520,14 @@ private static void addColumnValue( Optional analyzer, ImmutableList.Builder optimizedRowConstructor, 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()) ); - } - - if (targetType != null) { - value = castValue(value, targetType); value = rewriteContext == null ? value : (NamedExpression) FoldConstantRuleOnFE.evaluate(value, rewriteContext); From 1aff963e80c56f22822668e82441cd89e6ee3a02 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Wed, 9 Jul 2025 19:17:20 +0800 Subject: [PATCH 5/5] fix --- .../apache/doris/nereids/load/NereidsLoadUtils.java | 2 +- .../apache/doris/nereids/rules/analysis/BindSink.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadUtils.java index b5d1db22327dc3..24e33c2995a3ca 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadUtils.java @@ -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() ), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java index e74accc8dee9e8..581ce6fe64f28e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java @@ -107,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 buildRules() { @@ -299,7 +308,7 @@ private LogicalProject getOutputProjectByCoercion(List tableSchema, L int targetLength = ((CharacterType) targetType).getLen(); if (sourceLength == targetLength) { castExpr = TypeCoercionUtils.castIfNotSameType(castExpr, targetType); - } else if (sourceLength > targetLength && targetLength >= 0) { + } 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);