[fix](autoinc) fix _fill_auto_inc_cols when the input column is ColumnConst (#22175)

This commit is contained in:
bobhan1
2023-07-25 14:41:36 +08:00
committed by GitHub
parent 28b714c371
commit 2b4bfe5be7
3 changed files with 115 additions and 18 deletions

View File

@ -457,29 +457,55 @@ Status OlapTableBlockConvertor::_fill_auto_inc_cols(vectorized::Block* block, si
DCHECK(slot->type().type == PrimitiveType::TYPE_BIGINT);
DCHECK(!slot->is_nullable());
vectorized::ColumnPtr src_column_ptr = block->get_by_position(idx).column;
const auto& src_nullable_column =
assert_cast<const vectorized::ColumnNullable&>(*src_column_ptr);
auto src_nested_column_ptr = src_nullable_column.get_nested_column_ptr();
const auto& null_map_data = src_nullable_column.get_null_map_data();
size_t null_value_count = 0;
auto dst_column = vectorized::ColumnInt64::create();
vectorized::ColumnInt64::Container& dst_values = dst_column->get_data();
dst_values.reserve(rows);
size_t null_value_count = 0;
for (size_t i = 0; i < rows; i++) {
null_value_count += null_map_data[i];
}
vectorized::ColumnPtr src_column_ptr = block->get_by_position(idx).column;
DCHECK(vectorized::is_column_const(*src_column_ptr) || src_column_ptr->is_nullable());
if (const vectorized::ColumnConst* const_column =
check_and_get_column<vectorized::ColumnConst>(src_column_ptr)) {
// for insert stmt like "insert into tbl1 select null,col1,col2,... from tbl2" or
// "insert into tbl1 select 1,col1,col2,... from tbl2", the type of literal's column
// will be `ColumnConst`
if (const_column->is_null_at(0)) {
// the input of autoinc column are all null literals
// fill the column with generated ids
null_value_count = rows;
std::vector<std::pair<int64_t, size_t>> res;
RETURN_IF_ERROR(_auto_inc_id_buffer->sync_request_ids(null_value_count, &res));
for (auto [start, length] : res) {
_auto_inc_id_allocator.insert_ids(start, length);
}
std::vector<std::pair<int64_t, size_t>> res;
RETURN_IF_ERROR(_auto_inc_id_buffer->sync_request_ids(null_value_count, &res));
for (auto [start, length] : res) {
_auto_inc_id_allocator.insert_ids(start, length);
}
for (size_t i = 0; i < rows; i++) {
dst_values.emplace_back(_auto_inc_id_allocator.next_id());
}
} else {
// the input of autoinc column are all int64 literals
// fill the column with that literal
int64_t value = const_column->get_int(0);
dst_values.resize_fill(rows, value);
}
} else {
const auto& src_nullable_column =
assert_cast<const vectorized::ColumnNullable&>(*src_column_ptr);
auto src_nested_column_ptr = src_nullable_column.get_nested_column_ptr();
const auto& null_map_data = src_nullable_column.get_null_map_data();
dst_values.reserve(rows);
for (size_t i = 0; i < rows; i++) {
null_value_count += null_map_data[i];
}
std::vector<std::pair<int64_t, size_t>> res;
RETURN_IF_ERROR(_auto_inc_id_buffer->sync_request_ids(null_value_count, &res));
for (auto [start, length] : res) {
_auto_inc_id_allocator.insert_ids(start, length);
}
for (size_t i = 0; i < rows; i++) {
dst_values.emplace_back((null_map_data[i] != 0) ? _auto_inc_id_allocator.next_id()
: src_nested_column_ptr->get_int(i));
for (size_t i = 0; i < rows; i++) {
dst_values.emplace_back((null_map_data[i] != 0) ? _auto_inc_id_allocator.next_id()
: src_nested_column_ptr->get_int(i));
}
}
block->get_by_position(idx).column = std::move(dst_column);
block->get_by_position(idx).type =

View File

@ -54,3 +54,19 @@
7 Doris 800
8 Nereids 900
-- !sql --
0 0
1 1
2 2
3 3
4 4
5 5
-- !sql --
0 0
1 1
2 2
3 3
4 4
5 5

View File

@ -173,4 +173,59 @@ suite("test_dup_table_auto_inc_basic_with_null") {
(null, "Doris", 800),
(null, "Nereids", 900);"""
qt_sql "select * from ${table5} order by id;"
def table6 = "test_dup_table_auto_inc_basic_insert_stmt2"
def table7 = "test_dup_table_auto_inc_basic_insert_stmt3"
sql "drop table if exists ${table6}"
sql """
CREATE TABLE IF NOT EXISTS `${table6}` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT "",
`value` int(11) NOT NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
)
"""
sql "drop table if exists ${table7}"
sql """
CREATE TABLE IF NOT EXISTS `${table7}` (
`x` BIGINT NOT NULL,
`y` BIGINT NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(`x`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`x`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
)
"""
sql "insert into ${table7} values(0,0),(1,1),(2,2),(3,3),(4,4),(5,5);"
sql "insert into ${table6} select null, y from ${table7};"
qt_sql "select * from ${table6} order by id"
def table8 = "test_dup_table_auto_inc_basic_insert_stmt4"
sql "drop table if exists ${table8}"
sql """
CREATE TABLE IF NOT EXISTS `${table8}` (
`id` BIGINT NOT NULL COMMENT "",
`value` BIGINT NOT NULL AUTO_INCREMENT COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
)
"""
sql "insert into ${table8} select x, null from ${table7};"
qt_sql "select * from ${table8} order by id"
}