[fix](sequence-column) Fix sequence_col column used default expr insert failed (#18933)

This commit is contained in:
lvshaokang
2023-05-08 17:18:25 +08:00
committed by GitHub
parent bc1bf420d1
commit af04c3acab
3 changed files with 74 additions and 5 deletions

View File

@ -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<Pair<String, Expr>> 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 {

View File

@ -0,0 +1,4 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
1

View File

@ -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} "
}