From dd7fef11a2c9e70523b8563740b81688ebd38bfd Mon Sep 17 00:00:00 2001 From: Nikolay Degterinsky Date: Thu, 19 Jan 2023 20:52:38 +0000 Subject: [PATCH 1/2] Add default granularity --- src/Parsers/ParserCreateQuery.cpp | 12 ++++++------ .../0_stateless/02534_default_granularity.reference | 1 + .../0_stateless/02534_default_granularity.sql | 4 ++++ 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 tests/queries/0_stateless/02534_default_granularity.reference create mode 100644 tests/queries/0_stateless/02534_default_granularity.sql diff --git a/src/Parsers/ParserCreateQuery.cpp b/src/Parsers/ParserCreateQuery.cpp index 90df8a8f79a..0d522b192e4 100644 --- a/src/Parsers/ParserCreateQuery.cpp +++ b/src/Parsers/ParserCreateQuery.cpp @@ -131,15 +131,15 @@ bool ParserIndexDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expe if (!data_type_p.parse(pos, type, expected)) return false; - if (!s_granularity.ignore(pos, expected)) - return false; - - if (!granularity_p.parse(pos, granularity, expected)) - return false; + if (s_granularity.ignore(pos, expected)) + { + if (!granularity_p.parse(pos, granularity, expected)) + return false; + } auto index = std::make_shared(); index->name = name->as().name(); - index->granularity = granularity->as().value.safeGet(); + index->granularity = granularity ? granularity->as().value.safeGet() : 1; index->set(index->expr, expr); index->set(index->type, type); node = index; diff --git a/tests/queries/0_stateless/02534_default_granularity.reference b/tests/queries/0_stateless/02534_default_granularity.reference new file mode 100644 index 00000000000..e60036653c9 --- /dev/null +++ b/tests/queries/0_stateless/02534_default_granularity.reference @@ -0,0 +1 @@ +CREATE TABLE default.users_02534\n(\n `id` Int16,\n `name` String,\n INDEX bf_idx name TYPE minmax GRANULARITY 1\n)\nENGINE = MergeTree\nORDER BY id\nSETTINGS index_granularity = 8192 diff --git a/tests/queries/0_stateless/02534_default_granularity.sql b/tests/queries/0_stateless/02534_default_granularity.sql new file mode 100644 index 00000000000..781df3ce934 --- /dev/null +++ b/tests/queries/0_stateless/02534_default_granularity.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS users_02534; +CREATE TABLE users_02534 (id Int16, name String, INDEX bf_idx(name) TYPE minmax) ENGINE=MergeTree ORDER BY id; +SHOW CREATE TABLE users_02534; +DROP TABLE users_02534; \ No newline at end of file From 02142596fb2e385e08a8c945ade9da45b4073cf6 Mon Sep 17 00:00:00 2001 From: Nikolay Degterinsky Date: Fri, 20 Jan 2023 15:22:13 +0000 Subject: [PATCH 2/2] Add docs --- .../engines/table-engines/mergetree-family/mergetree.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/en/engines/table-engines/mergetree-family/mergetree.md b/docs/en/engines/table-engines/mergetree-family/mergetree.md index 061b3996da6..80384234188 100644 --- a/docs/en/engines/table-engines/mergetree-family/mergetree.md +++ b/docs/en/engines/table-engines/mergetree-family/mergetree.md @@ -40,8 +40,8 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster] name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1], name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2], ... - INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1, - INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2, + INDEX index_name1 expr1 TYPE type1(...) [GRANULARITY value1], + INDEX index_name2 expr2 TYPE type2(...) [GRANULARITY value2], ... PROJECTION projection_name_1 (SELECT [GROUP BY] [ORDER BY]), PROJECTION projection_name_2 (SELECT [GROUP BY] [ORDER BY]) @@ -359,13 +359,15 @@ ClickHouse uses this logic not only for days of the month sequences, but for any The index declaration is in the columns section of the `CREATE` query. ``` sql -INDEX index_name expr TYPE type(...) GRANULARITY granularity_value +INDEX index_name expr TYPE type(...) [GRANULARITY granularity_value] ``` For tables from the `*MergeTree` family, data skipping indices can be specified. These indices aggregate some information about the specified expression on blocks, which consist of `granularity_value` granules (the size of the granule is specified using the `index_granularity` setting in the table engine). Then these aggregates are used in `SELECT` queries for reducing the amount of data to read from the disk by skipping big blocks of data where the `where` query cannot be satisfied. +The `GRANULARITY` clause can be omitted, the default value of `granularity_value` is 1. + **Example** ``` sql