Skip to content

Commit 2c01f69

Browse files
authored
[fix](nereids) fix insert into values throw 'Invalid call to sql on unbound object' and 'Insert has filtered data in strict mode' exception (apache#52802)
1. fix `Invalid call to sql on unbound object` when use `interval`, introduced by apache#40202 ```sql CREATE TABLE `test_insert_cast_interval` ( `id` int NULL, `dt` date NULL ) ENGINE=OLAP 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)); (1105, 'errCode = 2, detailMessage = Invalid call to sql on unbound object') ``` 2. fix `Insert has filtered data in strict mode`, introduced by apache#49116 ```sql CREATE TABLE `test_insert_more_string` ( `r_regionkey` int NULL, `r_name` varchar(25) NULL, `r_comment` varchar(152) NULL ) ENGINE=OLAP DISTRIBUTED BY HASH(`r_regionkey`) BUCKETS 1 PROPERTIES ( "replication_allocation" = "tag.location.default: 1"); insert into test_insert_more_string values (3, "akljalkjbalkjsldkrjewokjfalksdjflaksjfdlaskjfalsdkfjalsdfjkasfdl", "aa"); (1105, 'errCode = 2, detailMessage = Insert has filtered data in strict mode') ```
1 parent d35fb95 commit 2c01f69

5 files changed

Lines changed: 78 additions & 20 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static LogicalPlan createLoadPlan(NereidsFileGroupInfo fileGroupInfo, Par
202202
ImmutableList.of(Rewriter.bottomUp(
203203
new BindExpression(),
204204
new LoadProjectRewrite(fileGroupInfo.getTargetTable()),
205-
new BindSink(),
205+
new BindSink(false),
206206
new AddPostFilter(
207207
context.fileGroup.getWhereExpr()
208208
),

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.doris.nereids.trees.expressions.Slot;
6363
import org.apache.doris.nereids.trees.expressions.SlotReference;
6464
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
65+
import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring;
6566
import org.apache.doris.nereids.trees.expressions.literal.Literal;
6667
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
6768
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
@@ -106,6 +107,15 @@
106107
* bind an unbound logicalTableSink represent the target table of an insert command
107108
*/
108109
public class BindSink implements AnalysisRuleFactory {
110+
public boolean needTruncateStringWhenInsert;
111+
112+
public BindSink() {
113+
this(true);
114+
}
115+
116+
public BindSink(boolean needTruncateStringWhenInsert) {
117+
this.needTruncateStringWhenInsert = needTruncateStringWhenInsert;
118+
}
109119

110120
@Override
111121
public List<Rule> buildRules() {
@@ -298,6 +308,8 @@ private LogicalProject<?> getOutputProjectByCoercion(List<Column> tableSchema, L
298308
int targetLength = ((CharacterType) targetType).getLen();
299309
if (sourceLength == targetLength) {
300310
castExpr = TypeCoercionUtils.castIfNotSameType(castExpr, targetType);
311+
} else if (needTruncateStringWhenInsert && sourceLength > targetLength && targetLength >= 0) {
312+
castExpr = new Substring(castExpr, Literal.of(1), Literal.of(targetLength));
301313
} else if (targetType.isStringType()) {
302314
castExpr = new Cast(castExpr, StringType.INSTANCE);
303315
}

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
382382
for (int i = 0; i < columns.size(); i++) {
383383
Column column = columns.get(i);
384384
NamedExpression defaultExpression = generateDefaultExpression(column);
385-
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
385+
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext);
386386
}
387387
} else {
388388
if (CollectionUtils.isNotEmpty(unboundLogicalSink.getColNames())) {
@@ -409,14 +409,14 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
409409
}
410410
if (values.get(i) instanceof DefaultValueSlot) {
411411
NamedExpression defaultExpression = generateDefaultExpression(sameNameColumn);
412-
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
412+
addColumnValue(
413+
analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext
414+
);
413415
} else {
414416
DataType targetType = DataType.fromCatalogType(sameNameColumn.getType());
415-
Expression castValue = castValue(values.get(i), targetType);
416-
castValue = rewriteContext == null
417-
? castValue
418-
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
419-
addColumnValue(analyzer, optimizedRowConstructor, (NamedExpression) castValue);
417+
addColumnValue(
418+
analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext
419+
);
420420
}
421421
}
422422
} else {
@@ -432,14 +432,14 @@ private static Plan normalizePlanWithoutLock(LogicalPlan plan, TableIf table,
432432
}
433433
if (values.get(i) instanceof DefaultValueSlot) {
434434
NamedExpression defaultExpression = generateDefaultExpression(columns.get(i));
435-
addColumnValue(analyzer, optimizedRowConstructor, defaultExpression);
435+
addColumnValue(
436+
analyzer, optimizedRowConstructor, defaultExpression, null, rewriteContext
437+
);
436438
} else {
437439
DataType targetType = DataType.fromCatalogType(columns.get(i).getType());
438-
Expression castValue = castValue(values.get(i), targetType);
439-
castValue = rewriteContext == null
440-
? castValue
441-
: FoldConstantRuleOnFE.evaluate(castValue, rewriteContext);
442-
addColumnValue(analyzer, optimizedRowConstructor, (NamedExpression) castValue);
440+
addColumnValue(
441+
analyzer, optimizedRowConstructor, values.get(i), targetType, rewriteContext
442+
);
443443
}
444444
}
445445
}
@@ -519,26 +519,32 @@ public Expression visitUnboundStar(UnboundStar unboundStar, ExpressionRewriteCon
519519
private static void addColumnValue(
520520
Optional<ExpressionAnalyzer> analyzer,
521521
ImmutableList.Builder<NamedExpression> optimizedRowConstructor,
522-
NamedExpression value) {
522+
NamedExpression value, DataType targetType, ExpressionRewriteContext rewriteContext) {
523+
if (targetType != null) {
524+
value = castValue(value, targetType);
525+
}
523526
if (analyzer.isPresent() && !(value instanceof Alias && value.child(0) instanceof Literal)) {
524527
ExpressionAnalyzer expressionAnalyzer = analyzer.get();
525528
value = (NamedExpression) expressionAnalyzer.analyze(
526-
value, new ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
529+
value, new ExpressionRewriteContext(expressionAnalyzer.getCascadesContext())
527530
);
531+
value = rewriteContext == null
532+
? value
533+
: (NamedExpression) FoldConstantRuleOnFE.evaluate(value, rewriteContext);
528534
}
529535
optimizedRowConstructor.add(value);
530536
}
531537

532-
private static Alias castValue(Expression value, DataType targetType) {
538+
private static NamedExpression castValue(Expression value, DataType targetType) {
533539
if (value instanceof Alias) {
534540
Expression oldChild = value.child(0);
535541
Expression newChild = TypeCoercionUtils.castUnbound(oldChild, targetType);
536542
return (Alias) (oldChild == newChild ? value : value.withChildren(newChild));
537543
} else if (value instanceof UnboundAlias) {
538544
UnboundAlias unboundAlias = (UnboundAlias) value;
539-
return new Alias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
545+
return new UnboundAlias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
540546
} else {
541-
return new Alias(TypeCoercionUtils.castUnbound(value, targetType));
547+
return new UnboundAlias(TypeCoercionUtils.castUnbound(value, targetType));
542548
}
543549
}
544550

‎regression-test/data/nereids_p0/insert_into_table/insert_values.out‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@
4747
3 30 3 3 3.0 2000-01-03 5 5 5 5.0 2000-01-05 5
4848

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

5252
-- !mv --
5353
-4 -4 -4 d
5454

55+
-- !select_test_insert_cast_interval --
56+
1 2020-02-02
57+
58+
-- !select_test_insert_more_string --
59+
3 akljalkjbalkjsldkrjewokjf aa
60+

‎regression-test/suites/nereids_p0/insert_into_table/insert_values.groovy‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,38 @@ suite('nereids_insert_into_values') {
143143
sql "insert into agg_have_dup_base_value values (-4, -4, -4, 'd')"
144144
sql "sync"
145145
qt_mv "select * from agg_have_dup_base_value"
146+
147+
multi_sql """
148+
DROP TABLE IF EXISTS test_insert_cast_interval;
149+
CREATE TABLE test_insert_cast_interval (
150+
`id` int NULL,
151+
`dt` date NULL
152+
) ENGINE=OLAP
153+
DUPLICATE KEY(`id`, `dt`)
154+
DISTRIBUTED BY HASH(`id`) BUCKETS 10
155+
PROPERTIES (
156+
"replication_allocation" = "tag.location.default: 1"
157+
);
158+
159+
INSERT INTO test_insert_cast_interval values(1, date_floor('2020-02-02', interval 1 second));
160+
"""
161+
162+
qt_select_test_insert_cast_interval "select * from test_insert_cast_interval"
163+
164+
multi_sql """
165+
drop table if exists test_insert_more_string;
166+
CREATE TABLE test_insert_more_string (
167+
`r_regionkey` int NULL,
168+
`r_name` varchar(25) NULL,
169+
`r_comment` varchar(152) NULL
170+
)
171+
DUPLICATE KEY(`r_regionkey`)
172+
DISTRIBUTED BY HASH(`r_regionkey`)
173+
BUCKETS 1 PROPERTIES (
174+
"replication_allocation" = "tag.location.default: 1"
175+
);
176+
insert into test_insert_more_string values (3, "akljalkjbalkjsldkrjewokjfalksdjflaksjfdlaskjfalsdkfjalsdfjkasfdl", "aa")
177+
"""
178+
179+
qt_select_test_insert_more_string "select * from test_insert_more_string"
146180
}

0 commit comments

Comments
 (0)