Skip to content

Commit ff810ce

Browse files
Try another fix and add simplier test.
1 parent 6f2a39b commit ff810ce

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

src/Storages/MergeTree/IMergeTreeReader.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,7 @@ void IMergeTreeReader::evaluateMissingDefaults(Block additional_columns, Columns
152152
if (res_columns[pos] == nullptr)
153153
continue;
154154

155-
/// We must take column type from part if it exists. Because at the end of defaults
156-
/// calculations we will materialize ALL the columns, not only missing.
157-
/// If column doesn't exist in part than it will be substituted with default expression
158-
const auto * column_in_part = part_columns.tryGet(name_and_type->name);
159-
if (column_in_part != nullptr)
160-
additional_columns.insert({res_columns[pos], column_in_part->type, name_and_type->name});
161-
else
162-
additional_columns.insert({res_columns[pos], name_and_type->type, name_and_type->name});
155+
additional_columns.insert({res_columns[pos], name_and_type->type, name_and_type->name});
163156
}
164157

165158
auto dag = DB::evaluateMissingDefaults(
@@ -271,7 +264,8 @@ void IMergeTreeReader::performRequiredConversions(Columns & res_columns) const
271264
/// Move columns from block.
272265
name_and_type = requested_columns.begin();
273266
for (size_t pos = 0; pos < num_columns; ++pos, ++name_and_type)
274-
res_columns[pos] = std::move(copy_block.getByName(name_and_type->name).column);
267+
if (copy_block.has(name_and_type->name))
268+
res_columns[pos] = std::move(copy_block.getByName(name_and_type->name).column);
275269
}
276270
catch (Exception & e)
277271
{

src/Storages/MergeTree/MergeTreeRangeReader.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,10 @@ MergeTreeRangeReader::ReadResult MergeTreeRangeReader::read(size_t max_rows, Mar
10071007
filterColumns(columns, read_result.final_filter);
10081008
}
10091009

1010+
/// If columns not empty, then apply on-fly alter conversions if any required
1011+
if (!prewhere_info || prewhere_info->perform_alter_conversions)
1012+
merge_tree_reader->performRequiredConversions(columns);
1013+
10101014
/// If some columns absent in part, then evaluate default values
10111015
if (should_evaluate_missing_defaults)
10121016
{
@@ -1017,10 +1021,6 @@ MergeTreeRangeReader::ReadResult MergeTreeRangeReader::read(size_t max_rows, Mar
10171021
addDummyColumnWithRowCount(additional_columns, read_result.num_rows);
10181022
merge_tree_reader->evaluateMissingDefaults(additional_columns, columns);
10191023
}
1020-
1021-
/// If columns not empty, then apply on-fly alter conversions if any required
1022-
if (!prewhere_info || prewhere_info->perform_alter_conversions)
1023-
merge_tree_reader->performRequiredConversions(columns);
10241024
}
10251025

10261026
read_result.columns.reserve(read_result.columns.size() + columns.size());
@@ -1046,14 +1046,14 @@ MergeTreeRangeReader::ReadResult MergeTreeRangeReader::read(size_t max_rows, Mar
10461046
bool should_evaluate_missing_defaults;
10471047
merge_tree_reader->fillMissingColumns(columns, should_evaluate_missing_defaults, read_result.num_rows);
10481048

1049-
/// If some columns absent in part, then evaluate default values
1050-
if (should_evaluate_missing_defaults)
1051-
merge_tree_reader->evaluateMissingDefaults({}, columns);
1052-
10531049
/// If result not empty, then apply on-fly alter conversions if any required
10541050
if (!prewhere_info || prewhere_info->perform_alter_conversions)
10551051
merge_tree_reader->performRequiredConversions(columns);
10561052

1053+
/// If some columns absent in part, then evaluate default values
1054+
if (should_evaluate_missing_defaults)
1055+
merge_tree_reader->evaluateMissingDefaults({}, columns);
1056+
10571057
for (size_t i = 0; i < columns.size(); ++i)
10581058
read_result.columns[i] = std::move(columns[i]);
10591059
}

src/Storages/MergeTree/MergeTreeSequentialSource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ try
251251
bool should_evaluate_missing_defaults = false;
252252
reader->fillMissingColumns(columns, should_evaluate_missing_defaults, rows_read);
253253

254+
reader->performRequiredConversions(columns);
255+
254256
if (should_evaluate_missing_defaults)
255257
reader->evaluateMissingDefaults({}, columns);
256258

257-
reader->performRequiredConversions(columns);
258-
259259
/// Reorder columns and fill result block.
260260
size_t num_columns = sample.size();
261261
Columns res_columns;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
0 0_42
2+
1 1_42
3+
2 2_42
4+
3 3_42
5+
4 4_42
6+
5 5_42
7+
6 6_42
8+
7 7_42
9+
8 8_42
10+
9 9_42
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
drop table if exists tab;
2+
create table tab (x UInt32) engine = MergeTree order by tuple();
3+
4+
insert into tab select number from numbers(10);
5+
6+
set alter_sync = 0;
7+
alter table tab update x = x + sleepEachRow(0.1) where 1;
8+
alter table tab modify column x String;
9+
alter table tab add column y String default x || '_42';
10+
11+
select x, y from tab order by x;

0 commit comments

Comments
 (0)