More compatible way

This commit is contained in:
alesapin 2020-10-23 19:31:51 +03:00
parent 59ca0d071c
commit b28894c43b
2 changed files with 15 additions and 21 deletions

View File

@ -608,29 +608,23 @@ static StoragePtr create(const StorageFactory::Arguments & args)
/// single default partition with name "all". /// single default partition with name "all".
metadata.partition_key = KeyDescription::getKeyFromAST(partition_by_key, metadata.columns, args.context); metadata.partition_key = KeyDescription::getKeyFromAST(partition_by_key, metadata.columns, args.context);
/// PRIMARY KEY without ORDER BY is allowed and considered as ORDER BY.
if (!args.storage_def->order_by && args.storage_def->primary_key)
args.storage_def->order_by = args.storage_def->primary_key;
/// Get sorting key from engine arguments. PRIMARY KEY without ORDER BY is allowed and considered as ORDER BY. if (!args.storage_def->order_by)
///
/// NOTE: store merging_param_key_arg as additional key column. We do it
/// before storage creation. After that storage will just copy this
/// column if sorting key will be changed.
if (args.storage_def->order_by)
{
metadata.sorting_key = KeyDescription::getSortingKeyFromAST(
args.storage_def->order_by->ptr(), metadata.columns, args.context, merging_param_key_arg);
}
else if (args.storage_def->primary_key)
{
metadata.sorting_key = KeyDescription::getSortingKeyFromAST(
args.storage_def->primary_key->ptr(), metadata.columns, args.context, merging_param_key_arg);
}
else
{
throw Exception( throw Exception(
"You must provide an ORDER BY or PRIMARY KEY expression in the table definition. " "You must provide an ORDER BY or PRIMARY KEY expression in the table definition. "
"If you don't want this table to be sorted, use ORDER BY/PRIMARY KEY tuple()", "If you don't want this table to be sorted, use ORDER BY/PRIMARY KEY tuple()",
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
}
/// Get sorting key from engine arguments.
///
/// NOTE: store merging_param_key_arg as additional key column. We do it
/// before storage creation. After that storage will just copy this
/// column if sorting key will be changed.
metadata.sorting_key = KeyDescription::getSortingKeyFromAST(
args.storage_def->order_by->ptr(), metadata.columns, args.context, merging_param_key_arg);
/// If primary key explicitly defined, than get it from AST /// If primary key explicitly defined, than get it from AST
if (args.storage_def->primary_key) if (args.storage_def->primary_key)

View File

@ -1,9 +1,9 @@
CREATE TABLE default.merge_tree_pk\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nSETTINGS index_granularity = 8192 CREATE TABLE default.merge_tree_pk\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nORDER BY key\nSETTINGS index_granularity = 8192
1 a 1 a
2 b 2 b
1 c 1 c
2 b 2 b
CREATE TABLE default.merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nSETTINGS index_granularity = 8192 CREATE TABLE default.merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nORDER BY key\nSETTINGS index_granularity = 8192
1 a 1 a
2 b 2 b
1 c 1 c
@ -12,7 +12,7 @@ CREATE TABLE default.merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String
2 e 555 2 e 555
2 b 0 2 b 0
CREATE TABLE default.merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String,\n `key2` UInt64\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nORDER BY (key, key2)\nSETTINGS index_granularity = 8192 CREATE TABLE default.merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String,\n `key2` UInt64\n)\nENGINE = ReplacingMergeTree()\nPRIMARY KEY key\nORDER BY (key, key2)\nSETTINGS index_granularity = 8192
CREATE TABLE default.replicated_merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplicatedReplacingMergeTree(\'/clickhouse/test/01532_primary_key_without\', \'r1\')\nPRIMARY KEY key\nSETTINGS index_granularity = 8192 CREATE TABLE default.replicated_merge_tree_pk_sql\n(\n `key` UInt64,\n `value` String\n)\nENGINE = ReplicatedReplacingMergeTree(\'/clickhouse/test/01532_primary_key_without\', \'r1\')\nPRIMARY KEY key\nORDER BY key\nSETTINGS index_granularity = 8192
1 a 1 a
2 b 2 b
1 c 1 c