diff --git a/src/Storages/MergeTree/IMergeTreeReader.cpp b/src/Storages/MergeTree/IMergeTreeReader.cpp index 1f46d6b8e1b..a154b687b4f 100644 --- a/src/Storages/MergeTree/IMergeTreeReader.cpp +++ b/src/Storages/MergeTree/IMergeTreeReader.cpp @@ -156,13 +156,21 @@ void IMergeTreeReader::evaluateMissingDefaults(Block additional_columns, Columns auto it = original_requested_columns.begin(); for (size_t pos = 0; pos < num_columns; ++pos, ++it) { - auto name_in_storage = it->getNameInStorage(); - - if (full_requested_columns_set.emplace(name_in_storage).second) - full_requested_columns.emplace_back(name_in_storage, it->getTypeInStorage()); - if (res_columns[pos]) + { + /// If column is already read, request it as is. + if (full_requested_columns_set.emplace(it->name).second) + full_requested_columns.emplace_back(it->name, it->type); + additional_columns.insert({res_columns[pos], it->type, it->name}); + } + else + { + /// If column or subcolumn is missed, request full column for correct evaluation of defaults of subcolumns. + auto name_in_storage = it->getNameInStorage(); + if (full_requested_columns_set.emplace(name_in_storage).second) + full_requested_columns.emplace_back(name_in_storage, it->getTypeInStorage()); + } } auto dag = DB::evaluateMissingDefaults( @@ -183,6 +191,12 @@ void IMergeTreeReader::evaluateMissingDefaults(Block additional_columns, Columns it = original_requested_columns.begin(); for (size_t pos = 0; pos < num_columns; ++pos, ++it) { + if (additional_columns.has(it->name)) + { + res_columns[pos] = additional_columns.getByName(it->name).column; + continue; + } + auto name_in_storage = it->getNameInStorage(); res_columns[pos] = additional_columns.getByName(name_in_storage).column; diff --git a/tests/queries/0_stateless/03203_fill_missed_subcolumns.reference b/tests/queries/0_stateless/03203_fill_missed_subcolumns.reference index 872eb7da3c8..9faa3b35848 100644 --- a/tests/queries/0_stateless/03203_fill_missed_subcolumns.reference +++ b/tests/queries/0_stateless/03203_fill_missed_subcolumns.reference @@ -27,5 +27,5 @@ 2 ('aaa','bbb') [1,NULL,3] 3 ('ccc','ddd') [4,5,6] 1 foo bar 3 [0,1,0] -2 foo bar 3 [0,1,0] +2 aaa bbb 3 [0,1,0] 3 ccc ddd 3 [0,0,0] diff --git a/tests/queries/0_stateless/03252_fill_missed_arrays.reference b/tests/queries/0_stateless/03252_fill_missed_arrays.reference new file mode 100644 index 00000000000..b92677edb95 --- /dev/null +++ b/tests/queries/0_stateless/03252_fill_missed_arrays.reference @@ -0,0 +1 @@ +20000 diff --git a/tests/queries/0_stateless/03252_fill_missed_arrays.sql b/tests/queries/0_stateless/03252_fill_missed_arrays.sql new file mode 100644 index 00000000000..585cd370673 --- /dev/null +++ b/tests/queries/0_stateless/03252_fill_missed_arrays.sql @@ -0,0 +1,22 @@ +DROP TABLE IF EXISTS t_fill_arrays; + +CREATE TABLE t_fill_arrays +( + `id` String, + `mapCol` Map(String, Array(String)), +) +ENGINE = MergeTree +ORDER BY id +SETTINGS vertical_merge_algorithm_min_rows_to_activate = 1, vertical_merge_algorithm_min_columns_to_activate = 1, min_bytes_for_full_part_storage = 0; + +INSERT INTO t_fill_arrays (id) SELECT hex(number) FROM numbers(10000); + +ALTER TABLE t_fill_arrays ADD COLUMN arrCol Array(String) DEFAULT []; + +INSERT INTO t_fill_arrays (id) SELECT hex(number) FROM numbers(10000); + +SELECT count() FROM t_fill_arrays WHERE NOT ignore(arrCol, mapCol.values); + +OPTIMIZE TABLE t_fill_arrays FINAL; + +DROP TABLE t_fill_arrays;