Compare commits

...

8 Commits

Author SHA1 Message Date
Peter Nguyen
ba250c539b
Merge 05d23dc932 into 334b28f6db 2024-11-19 17:49:33 -08:00
Peter Nguyen
05d23dc932 Add DROP statements to test 2024-11-17 10:09:41 -08:00
Peter Nguyen
8f44818d99 Empty commit (just wanna see what happens) 2024-11-16 23:56:03 -08:00
Peter Nguyen
05fe163898 Add additional test case from issue #53421 2024-11-13 11:40:02 -08:00
Peter Nguyen
e29ee5b96d Fix style in src code (bracket) 2024-11-13 10:31:24 -08:00
Peter Nguyen
7166df0776 Update docs to explicitly mention that it's an invalid operation 2024-11-13 10:12:53 -08:00
Peter Nguyen
c70a7f7573 Add test 03263_forbid_materialize_sort_key 2024-11-13 10:03:59 -08:00
Peter Nguyen
6672904cb0 Fix bug with materializing a column in the sort key by throwing an exception 2024-11-13 10:01:51 -08:00
4 changed files with 32 additions and 1 deletions

View File

@ -272,7 +272,7 @@ ALTER TABLE table_name MODIFY COLUMN column_name RESET SETTING max_compress_bloc
## MATERIALIZE COLUMN
Materializes a column with a `DEFAULT` or `MATERIALIZED` value expression. When adding a materialized column using `ALTER TABLE table_name ADD COLUMN column_name MATERIALIZED`, existing rows without materialized values are not automatically filled. `MATERIALIZE COLUMN` statement can be used to rewrite existing column data after a `DEFAULT` or `MATERIALIZED` expression has been added or updated (which only updates the metadata but does not change existing data).
Materializes a column with a `DEFAULT` or `MATERIALIZED` value expression. When adding a materialized column using `ALTER TABLE table_name ADD COLUMN column_name MATERIALIZED`, existing rows without materialized values are not automatically filled. `MATERIALIZE COLUMN` statement can be used to rewrite existing column data after a `DEFAULT` or `MATERIALIZED` expression has been added or updated (which only updates the metadata but does not change existing data). Note that materializing a column in the sort key is an invalid operation because it would break the sort order.
Implemented as a [mutation](/docs/en/sql-reference/statements/alter/index.md#mutations).
For columns with a new or updated `MATERIALIZED` value expression, all existing rows are rewritten.

View File

@ -791,6 +791,13 @@ void MutationsInterpreter::prepare(bool dry_run)
if (stages.size() == 1) /// First stage only supports filtering and can't update columns.
stages.emplace_back(context);
// Can't materialize a column in the sort key
Names sort_columns = metadata_snapshot->getSortingKeyColumns();
if (std::find(sort_columns.begin(), sort_columns.end(), command.column_name) != sort_columns.end())
{
throw Exception(ErrorCodes::CANNOT_UPDATE_COLUMN, "Failed to materialize column {} because it's in the sort key. Doing so would break sort order", command.column_name);
}
const auto & column = columns_desc.get(command.column_name);
if (!column.default_desc.expression)

View File

@ -0,0 +1,24 @@
CREATE TABLE IF NOT EXISTS test (a UInt64) ENGINE=MergeTree() ORDER BY a;
INSERT INTO test (a) SELECT 1 FROM numbers(1000);
ALTER TABLE test ADD COLUMN b Float64 AFTER a, MODIFY ORDER BY (a, b);
ALTER TABLE test MODIFY COLUMN b DEFAULT rand64() % 100000;
ALTER TABLE test MATERIALIZE COLUMN b; -- { serverError CANNOT_UPDATE_COLUMN }
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS tab (x UInt32, y UInt32) engine = MergeTree ORDER BY tuple();
CREATE DICTIONARY IF NOT EXISTS dict (x UInt32, y UInt32) primary key x source(clickhouse(table 'tab')) LAYOUT(FLAT()) LIFETIME(MIN 0 MAX 1000);
INSERT INTO tab VALUES (1, 2), (3, 4);
SYSTEM RELOAD DICTIONARY dict;
CREATE TABLE IF NOT EXISTS tab2 (x UInt32, y UInt32 materialized dictGet(dict, 'y', x)) engine = MergeTree ORDER BY (y);
INSERT INTO tab2 (x) VALUES (1), (3);
TRUNCATE TABLE tab;
INSERT INTO tab VALUES (1, 4), (3, 2);
SYSTEM RELOAD DICTIONARY dict;
SET mutations_sync=2;
ALTER TABLE tab2 materialize column y; -- { serverError CANNOT_UPDATE_COLUMN }
DROP TABLE IF EXISTS tab2;
DROP DICTIONARY IF EXISTS dict;
DROP TABLE IF EXISTS tab;