diff --git a/src/Storages/MergeTree/MutateTask.cpp b/src/Storages/MergeTree/MutateTask.cpp index bb41608eb00..25fa45e7b68 100644 --- a/src/Storages/MergeTree/MutateTask.cpp +++ b/src/Storages/MergeTree/MutateTask.cpp @@ -76,8 +76,8 @@ static void splitAndModifyMutationCommands( { if (command.type == MutationCommand::Type::MATERIALIZE_COLUMN) { - /// For ordinary column with default expression, materialize column should not override past values - /// So we only mutated column if `command.column_name` is a materialized column or if the part does not have physical column file + /// For ordinary column with default or materialized expression, MATERIALIZE COLUMN should not override past values + /// So we only mutate column if `command.column_name` is a default/materialized column or if the part does not have physical column file auto column_ordinary = table_columns.getOrdinary().tryGetByName(command.column_name); if (!column_ordinary || !part->tryGetColumn(command.column_name) || !part->hasColumnFiles(*column_ordinary)) { @@ -206,8 +206,8 @@ static void splitAndModifyMutationCommands( { if (command.type == MutationCommand::Type::MATERIALIZE_COLUMN) { - /// For ordinary column with default expression, materialize column should not override past values - /// So we only mutated column if `command.column_name` is a materialized column or if the part does not have physical column file + /// For ordinary column with default or materialized expression, MATERIALIZE COLUMN should not override past values + /// So we only mutate column if `command.column_name` is a default/materialized column or if the part does not have physical column file auto column_ordinary = table_columns.getOrdinary().tryGetByName(command.column_name); if (!column_ordinary || !part->tryGetColumn(command.column_name) || !part->hasColumnFiles(*column_ordinary)) for_interpreter.push_back(command); diff --git a/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.reference b/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.reference new file mode 100644 index 00000000000..a5a0370620b --- /dev/null +++ b/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.reference @@ -0,0 +1,33 @@ +-- Compact parts +Origin +1 2 +2 54321 +After materialize +1 2 +2 54321 +-- Wide parts +Origin +1 2 +2 54321 +After materialize +1 2 +2 54321 +-- Nullable column != physically absent +Origin +1 2 +2 \N +3 54321 +After materialize +1 2 +2 \N +3 54321 +-- Parts with renamed column +Origin +1 2 +2 54321 +After rename +1 2 +2 54321 +After materialize +1 2 +2 54321 diff --git a/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.sql b/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.sql new file mode 100644 index 00000000000..825c7eab048 --- /dev/null +++ b/tests/queries/0_stateless/02946_materialize_column_must_not_override_past_values.sql @@ -0,0 +1,53 @@ +SET mutations_sync = 2; + +SELECT '-- Compact parts'; + +CREATE TABLE tab (id Int64, dflt Int64 DEFAULT 54321) ENGINE MergeTree ORDER BY id; +INSERT INTO tab (id, dflt) VALUES (1, 2); +INSERT INTO tab (id) VALUES (2); +SELECT 'Origin'; +SELECT * FROM tab ORDER BY id; +ALTER TABLE tab MATERIALIZE COLUMN dflt; +SELECT 'After materialize'; +SELECT * FROM tab ORDER BY id; +DROP TABLE tab; + +SELECT '-- Wide parts'; + +CREATE TABLE tab (id Int64, dflt Int64 DEFAULT 54321) ENGINE MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 1; +INSERT INTO tab (id, dflt) VALUES (1, 2); +INSERT INTO tab (id) VALUES (2); +SELECT 'Origin'; +SELECT * FROM tab ORDER BY id; +ALTER TABLE tab MATERIALIZE COLUMN dflt; +SELECT 'After materialize'; +SELECT * FROM tab ORDER BY id; +DROP TABLE tab; + +SELECT '-- Nullable column != physically absent'; + +CREATE TABLE tab (id Int64, dflt Nullable(Int64) DEFAULT 54321) ENGINE MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 1; +INSERT INTO tab (id, dflt) VALUES (1, 2); +INSERT INTO tab (id, dflt) VALUES (2, NULL); +INSERT INTO tab (id) VALUES (3); +SELECT 'Origin'; +SELECT * FROM tab ORDER BY id; +ALTER TABLE tab MATERIALIZE COLUMN dflt; +SELECT 'After materialize'; +SELECT * FROM tab ORDER BY id; +DROP TABLE tab; + +SELECT '-- Parts with renamed column'; + +CREATE TABLE tab (id Int64, dflt Int64 DEFAULT 54321) ENGINE MergeTree ORDER BY id; +INSERT INTO tab (id, dflt) VALUES (1, 2); +INSERT INTO tab (id) VALUES (2); +SELECT 'Origin'; +SELECT * FROM tab ORDER BY id; +ALTER TABLE tab RENAME COLUMN dflt TO dflt2; +SELECT 'After rename'; +SELECT * FROM tab ORDER BY id; +ALTER TABLE tab MATERIALIZE COLUMN bar; +SELECT 'After materialize'; +SELECT * FROM tab ORDER BY id; +DROP TABLE tab; diff --git a/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.reference b/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.reference deleted file mode 100644 index 6b0d88bd09b..00000000000 --- a/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.reference +++ /dev/null @@ -1,29 +0,0 @@ ---Origin-- -1 2 -2 54321 ---After materialize-- -1 2 -2 54321 ---Origin-- -1 2 -2 54321 ---After materialize-- -1 2 -2 54321 ---Origin-- -1 2 -2 \N -3 54321 ---After materialize-- -1 2 -2 \N -3 54321 ---Origin-- -1 2 -2 54321 ---After rename-- -1 2 -2 54321 ---After materialize-- -1 2 -2 54321 diff --git a/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.sql b/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.sql deleted file mode 100644 index 1815661e097..00000000000 --- a/tests/queries/0_stateless/02946_materialize_column_not_override_past_values.sql +++ /dev/null @@ -1,49 +0,0 @@ - -SET mutations_sync = 2; --- Compact parts -CREATE TABLE test (id Int64, foo Int64 default 54321) ENGINE MergeTree ORDER BY id; -INSERT INTO test ( id, foo ) values ( 1, 2 ); -INSERT INTO test ( id ) values ( 2 ); -SELECT '--Origin--'; -SELECT * FROM test ORDER BY id; -ALTER TABLE test MATERIALIZE COLUMN foo; -SELECT '--After materialize--'; -SELECT * FROM test ORDER BY id; -DROP TABLE test; - --- Wide parts -CREATE TABLE test (id Int64, foo Nullable(Int64) default 54321) ENGINE MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 1; -INSERT INTO test ( id, foo ) values ( 1, 2 ); -INSERT INTO test ( id ) values ( 2 ); -SELECT '--Origin--'; -SELECT * FROM test ORDER BY id; -ALTER TABLE test MATERIALIZE COLUMN foo; -SELECT '--After materialize--'; -SELECT * FROM test ORDER BY id; -DROP TABLE test; - --- Nullable column != physically absent -CREATE TABLE test (id Int64, foo Nullable(Int64) default 54321) ENGINE MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 1; -INSERT INTO test ( id, foo ) values ( 1, 2 ); -INSERT INTO test ( id, foo ) values ( 2, NULL ); -INSERT INTO test ( id ) values ( 3 ); -SELECT '--Origin--'; -SELECT * FROM test ORDER BY id; -ALTER TABLE test MATERIALIZE COLUMN foo; -SELECT '--After materialize--'; -SELECT * FROM test ORDER BY id; -DROP TABLE test; - --- Parts with renamed column -CREATE TABLE test (id Int64, foo Int64 default 54321) ENGINE MergeTree ORDER BY id; -INSERT INTO test ( id, foo ) values ( 1, 2 ); -INSERT INTO test ( id ) values ( 2 ); -SELECT '--Origin--'; -SELECT * FROM test ORDER BY id; -ALTER TABLE test RENAME COLUMN foo TO bar; -SELECT '--After rename--'; -SELECT * FROM test ORDER BY id; -ALTER TABLE test MATERIALIZE COLUMN bar; -SELECT '--After materialize--'; -SELECT * FROM test ORDER BY id; -DROP TABLE test; \ No newline at end of file