From af04c3acabd9c36a7efb643fd4f04503cf1c8ede Mon Sep 17 00:00:00 2001 From: lvshaokang Date: Mon, 8 May 2023 17:18:25 +0800 Subject: [PATCH] [fix](sequence-column) Fix sequence_col column used default expr insert failed (#18933) --- .../org/apache/doris/analysis/InsertStmt.java | 20 +++++-- .../test_sequence_col_default_value.out | 4 ++ .../test_sequence_col_default_value.groovy | 55 +++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 regression-test/data/correctness_p0/test_sequence_col_default_value.out create mode 100644 regression-test/suites/correctness_p0/test_sequence_col_default_value.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/InsertStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/InsertStmt.java index c9d3f409f7..4ca2435fb2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/InsertStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/InsertStmt.java @@ -70,6 +70,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * Insert into is performed to load data from the result of query stmt. @@ -715,29 +716,38 @@ public class InsertStmt extends DdlStmt { selectList.set(i, expr); exprByName.put(col.getName(), expr); } + List> resultExprByName = Lists.newArrayList(); // reorder resultExprs in table column order for (Column col : targetTable.getFullSchema()) { if (exprByName.containsKey(col.getName())) { - resultExprs.add(exprByName.get(col.getName())); + resultExprByName.add(Pair.of(col.getName(), exprByName.get(col.getName()))); } else { // process sequence col, map sequence column to other column if (targetTable instanceof OlapTable && ((OlapTable) targetTable).hasSequenceCol() && col.getName().equals(Column.SEQUENCE_COL) && ((OlapTable) targetTable).getSequenceMapCol() != null) { - resultExprs.add(exprByName.get(((OlapTable) targetTable).getSequenceMapCol())); + if (resultExprByName.stream().map(Pair::key) + .anyMatch(key -> key.equals(((OlapTable) targetTable).getSequenceMapCol()))) { + resultExprByName.add(Pair.of(Column.SEQUENCE_COL, + resultExprByName.stream() + .filter(p -> p.key().equals(((OlapTable) targetTable).getSequenceMapCol())) + .map(Pair::value).findFirst().orElse(null))); + } } else if (col.getDefaultValue() == null) { - resultExprs.add(NullLiteral.create(col.getType())); + resultExprByName.add(Pair.of(col.getName(), NullLiteral.create(col.getType()))); } else { if (col.getDefaultValueExprDef() != null) { - resultExprs.add(col.getDefaultValueExpr()); + resultExprByName.add(Pair.of(col.getName(), col.getDefaultValueExpr())); } else { StringLiteral defaultValueExpr; defaultValueExpr = new StringLiteral(col.getDefaultValue()); - resultExprs.add(defaultValueExpr.checkTypeCompatibility(col.getType())); + resultExprByName.add(Pair.of(col.getName(), + defaultValueExpr.checkTypeCompatibility(col.getType()))); } } } } + resultExprs.addAll(resultExprByName.stream().map(Pair::value).collect(Collectors.toList())); } private DataSink createDataSink() throws AnalysisException { diff --git a/regression-test/data/correctness_p0/test_sequence_col_default_value.out b/regression-test/data/correctness_p0/test_sequence_col_default_value.out new file mode 100644 index 0000000000..095c7b2035 --- /dev/null +++ b/regression-test/data/correctness_p0/test_sequence_col_default_value.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +1 + diff --git a/regression-test/suites/correctness_p0/test_sequence_col_default_value.groovy b/regression-test/suites/correctness_p0/test_sequence_col_default_value.groovy new file mode 100644 index 0000000000..eb30f3d8f6 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_sequence_col_default_value.groovy @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_sequence_col_default_value") { + + def tableName = "test_sequence_col_default_value" + + sql """ DROP TABLE IF EXISTS ${tableName} """ + + sql """ + CREATE TABLE ${tableName} + ( + project_id BIGINT NOT NULL , + consume_date DATETIMEV2 NOT NULL, + order_id BIGINT NOT NULL , + combo_extend JSONB DEFAULT NULL, + age INT, + name varchar(20), + write_time DATETIME DEFAULT CURRENT_TIMESTAMP + ) UNIQUE KEY(project_id, consume_date,order_id) + DISTRIBUTED BY HASH(project_id) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "enable_unique_key_merge_on_write" = "true", + "function_column.sequence_col" = 'write_time' + ); + """ + + // test insert into. + sql " insert into ${tableName} (order_id,project_id,consume_date,age,name) values (1231234356,370040365,'2023-01-12',NULL,'a'); " + sql " insert into ${tableName} (order_id,project_id,consume_date,age,name) values (1231234356,370040365,'2023-01-12',NULL,'b'); " + + sql "set show_hidden_columns=true" + qt_sql """ + select count(*) from ${tableName} + where to_date(__DORIS_SEQUENCE_COL__) = to_date(write_time) + """ + + sql " DROP TABLE IF EXISTS ${tableName} " + +}