Fix alter modify for decimal columns when type size doesn't change

This commit is contained in:
alesapin 2021-03-15 11:22:15 +03:00
parent afa24938bc
commit 69b8ded578
3 changed files with 75 additions and 39 deletions

View File

@ -907,8 +907,7 @@ void StorageReplicatedMergeTree::setTableStructure(
StorageInMemoryMetadata new_metadata = getInMemoryMetadata();
StorageInMemoryMetadata old_metadata = getInMemoryMetadata();
if (new_columns != new_metadata.columns)
new_metadata.columns = new_columns;
new_metadata.columns = new_columns;
if (!metadata_diff.empty())
{
@ -976,46 +975,43 @@ void StorageReplicatedMergeTree::setTableStructure(
}
/// Changes in columns may affect following metadata fields
if (new_metadata.columns != old_metadata.columns)
new_metadata.column_ttls_by_name.clear();
for (const auto & [name, ast] : new_metadata.columns.getColumnTTLs())
{
new_metadata.column_ttls_by_name.clear();
for (const auto & [name, ast] : new_metadata.columns.getColumnTTLs())
{
auto new_ttl_entry = TTLDescription::getTTLFromAST(ast, new_metadata.columns, global_context, new_metadata.primary_key);
new_metadata.column_ttls_by_name[name] = new_ttl_entry;
}
if (new_metadata.partition_key.definition_ast != nullptr)
new_metadata.partition_key.recalculateWithNewColumns(new_metadata.columns, global_context);
if (!metadata_diff.sorting_key_changed) /// otherwise already updated
new_metadata.sorting_key.recalculateWithNewColumns(new_metadata.columns, global_context);
/// Primary key is special, it exists even if not defined
if (new_metadata.primary_key.definition_ast != nullptr)
{
new_metadata.primary_key.recalculateWithNewColumns(new_metadata.columns, global_context);
}
else
{
new_metadata.primary_key = KeyDescription::getKeyFromAST(new_metadata.sorting_key.definition_ast, new_metadata.columns, global_context);
new_metadata.primary_key.definition_ast = nullptr;
}
if (!metadata_diff.sampling_expression_changed && new_metadata.sampling_key.definition_ast != nullptr)
new_metadata.sampling_key.recalculateWithNewColumns(new_metadata.columns, global_context);
if (!metadata_diff.skip_indices_changed) /// otherwise already updated
{
for (auto & index : new_metadata.secondary_indices)
index.recalculateWithNewColumns(new_metadata.columns, global_context);
}
if (!metadata_diff.ttl_table_changed && new_metadata.table_ttl.definition_ast != nullptr)
new_metadata.table_ttl = TTLTableDescription::getTTLForTableFromAST(
new_metadata.table_ttl.definition_ast, new_metadata.columns, global_context, new_metadata.primary_key);
auto new_ttl_entry = TTLDescription::getTTLFromAST(ast, new_metadata.columns, global_context, new_metadata.primary_key);
new_metadata.column_ttls_by_name[name] = new_ttl_entry;
}
if (new_metadata.partition_key.definition_ast != nullptr)
new_metadata.partition_key.recalculateWithNewColumns(new_metadata.columns, global_context);
if (!metadata_diff.sorting_key_changed) /// otherwise already updated
new_metadata.sorting_key.recalculateWithNewColumns(new_metadata.columns, global_context);
/// Primary key is special, it exists even if not defined
if (new_metadata.primary_key.definition_ast != nullptr)
{
new_metadata.primary_key.recalculateWithNewColumns(new_metadata.columns, global_context);
}
else
{
new_metadata.primary_key = KeyDescription::getKeyFromAST(new_metadata.sorting_key.definition_ast, new_metadata.columns, global_context);
new_metadata.primary_key.definition_ast = nullptr;
}
if (!metadata_diff.sampling_expression_changed && new_metadata.sampling_key.definition_ast != nullptr)
new_metadata.sampling_key.recalculateWithNewColumns(new_metadata.columns, global_context);
if (!metadata_diff.skip_indices_changed) /// otherwise already updated
{
for (auto & index : new_metadata.secondary_indices)
index.recalculateWithNewColumns(new_metadata.columns, global_context);
}
if (!metadata_diff.ttl_table_changed && new_metadata.table_ttl.definition_ast != nullptr)
new_metadata.table_ttl = TTLTableDescription::getTTLForTableFromAST(
new_metadata.table_ttl.definition_ast, new_metadata.columns, global_context, new_metadata.primary_key);
/// Even if the primary/sorting/partition keys didn't change we must reinitialize it
/// because primary/partition key column types might have changed.
checkTTLExpressions(new_metadata, old_metadata);

View File

@ -0,0 +1,9 @@
1 5.00000000
2 6.00000000
CREATE TABLE default.test_alter_decimal\n(\n `n` UInt64,\n `d` Decimal(18, 8)\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/01761_alter_decimal_zookeeper\', \'r1\')\nORDER BY tuple()\nSETTINGS index_granularity = 8192
1 5.00000000
2 6.00000000
CREATE TABLE default.test_alter_decimal\n(\n `n` UInt64,\n `d` Decimal(18, 8)\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/01761_alter_decimal_zookeeper\', \'r1\')\nORDER BY tuple()\nSETTINGS index_granularity = 8192
1 5.00000000
2 6.00000000
3 7.00000000

View File

@ -0,0 +1,31 @@
DROP TABLE IF EXISTS test_alter_decimal;
CREATE TABLE test_alter_decimal
(n UInt64, d Decimal(15, 8))
ENGINE = ReplicatedMergeTree('/clickhouse/01761_alter_decimal_zookeeper', 'r1')
ORDER BY tuple();
INSERT INTO test_alter_decimal VALUES (1, toDecimal32(5, 5));
INSERT INTO test_alter_decimal VALUES (2, toDecimal32(6, 6));
SELECT * FROM test_alter_decimal ORDER BY n;
ALTER TABLE test_alter_decimal MODIFY COLUMN d Decimal(18, 8);
SHOW CREATE TABLE test_alter_decimal;
SELECT * FROM test_alter_decimal ORDER BY n;
DETACH TABLE test_alter_decimal;
ATTACH TABLE test_alter_decimal;
SHOW CREATE TABLE test_alter_decimal;
INSERT INTO test_alter_decimal VALUES (3, toDecimal32(7, 7));
OPTIMIZE TABLE test_alter_decimal FINAL;
SELECT * FROM test_alter_decimal ORDER BY n;
DROP TABLE IF EXISTS test_alter_decimal;