提交 7090d086 编写于 作者: A Alexey Zatelepin 提交者: alexey-milovidov

reinitialize primary key after ALTER [#CLICKHOUSE-2853]

上级 65d71878
......@@ -474,6 +474,7 @@ public:
private:
friend struct MergeTreeDataPart;
friend class StorageMergeTree;
friend class ReplicatedMergeTreeAlterThread;
friend class MergeTreeDataMerger;
bool require_part_metadata;
......
......@@ -109,14 +109,14 @@ MergeTreeData::MergeTreeData(
merging_params.check(*columns);
if (!primary_expr_ast && merging_params.mode != MergingParams::Unsorted)
throw Exception("Primary key could be empty only for UnsortedMergeTree", ErrorCodes::BAD_ARGUMENTS);
initPrimaryKey();
/// Creating directories, if not exist.
Poco::File(full_path).createDirectories();
Poco::File(full_path + "detached").createDirectory();
if (primary_expr_ast)
initPrimaryKey();
else if (merging_params.mode != MergingParams::Unsorted)
throw Exception("Primary key could be empty only for UnsortedMergeTree", ErrorCodes::BAD_ARGUMENTS);
}
......@@ -143,6 +143,9 @@ void MergeTreeData::checkNoMultidimensionalArrays(const NamesAndTypesList & colu
void MergeTreeData::initPrimaryKey()
{
if (!primary_expr_ast)
return;
/// Initialize description of sorting.
sort_descr.clear();
sort_descr.reserve(primary_expr_ast->children.size());
......
......@@ -129,6 +129,11 @@ void ReplicatedMergeTreeAlterThread::run()
storage.data.column_defaults = std::move(column_defaults);
}
/// Reinitialize primary key because primary key column types might have changed.
storage.data.initPrimaryKey();
if (storage.unreplicated_data)
storage.unreplicated_data->initPrimaryKey();
LOG_INFO(log, "Applied changes to table.");
}
else
......
......@@ -233,8 +233,9 @@ void StorageMergeTree::alter(
if (primary_key_is_modified)
{
data.primary_expr_ast = new_primary_key_ast;
data.initPrimaryKey();
}
/// Reinitialize primary key because primary key column types might have changed.
data.initPrimaryKey();
for (auto & transaction : transactions)
transaction->commit();
......
query failed
query failed
2017-02-09
foo 1486598400 1
bar 1486598400 1
......@@ -28,6 +28,8 @@ perform "ALTER TABLE test.alter MODIFY COLUMN c UInt32"
perform "INSERT INTO test.alter VALUES ('2017-02-09', 'bar', 1486598400, '2017-02-09 00:00:00')"
perform "SELECT d FROM test.alter WHERE a = 'bar'"
perform "SELECT a, b, b = toUnixTimestamp(c) FROM test.alter ORDER BY a FORMAT TSV"
perform "DROP TABLE test.alter"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册