diff --git a/src/Interpreters/ColumnAliasesVisitor.h b/src/Interpreters/ColumnAliasesVisitor.h index 0bd4bd9fb9d..b886b40af20 100644 --- a/src/Interpreters/ColumnAliasesVisitor.h +++ b/src/Interpreters/ColumnAliasesVisitor.h @@ -14,8 +14,35 @@ class ASTFunction; class ASTIdentifier; using DataTypePtr = std::shared_ptr; -/// Visits AST node to rewrite alias columns in filter query -/// Currently works only in `KeyCondition` of select query and `required_columns` in `InterpreterSelectQuery.cpp` +/// Visits AST node to rewrite alias columns in query +/// Currently works only 3 kind ways below + +/// For example: +// CREATE TABLE test_table +// ( +// `timestamp` DateTime, +// `value` UInt64, +// `day` Date ALIAS toDate(timestamp), +// `day1` Date ALIAS day + 1, +// `day2` Date ALIAS day1 + 1, +// `time` DateTime ALIAS timestamp +// )ENGINE = MergeTree +// PARTITION BY toYYYYMMDD(timestamp) +// ORDER BY timestamp SETTINGS index_granularity = 1; + +/// 1. Rewrite the filters in query when enable optimize_alias_column_prediction +/// this could help with `optimize_trivial_count`, Partition Prune in `KeyCondition` and secondary indexes. +/// eg: select max(value) from test_table where day2 = today(), filters will be: ((toDate(timestamp) + 1) + 1) = today() . + +/// 2. Alias on alias for `required_columns` extracted in `InterpreterSelectQuery.cpp`, it could help get all dependent physical columns for query. +/// eg: select day2 from test_table. `required_columns` can got require columns from the tempory rewrited AST `((toDate(timestamp) + 1) + 1)`. + +/// 3. Help with `optimize_aggregation_in_order` and `optimize_read_in_order` in `ReadInOrderOptimizer.cpp`: +/// For queries with alias columns in `orderBy` and `groupBy`, these ASTs will not change. +/// But we generate tempory asts and generate tempory Actions to get the `InputOrderInfo` +/// eg: select day1 from test_table order by day1; + + class ColumnAliasesMatcher { public: diff --git a/src/Interpreters/addTypeConversionToAST.h b/src/Interpreters/addTypeConversionToAST.h index 61334403fe2..16fa98f6e0c 100644 --- a/src/Interpreters/addTypeConversionToAST.h +++ b/src/Interpreters/addTypeConversionToAST.h @@ -2,12 +2,12 @@ #include #include -#include namespace DB { - +class Context; +class NamesAndTypesList; /// It will produce an expression with CAST to get an AST with the required type. ASTPtr addTypeConversionToAST(ASTPtr && ast, const String & type_name); diff --git a/src/Storages/ReadInOrderOptimizer.cpp b/src/Storages/ReadInOrderOptimizer.cpp index e718583e7b9..6d65d73676e 100644 --- a/src/Storages/ReadInOrderOptimizer.cpp +++ b/src/Storages/ReadInOrderOptimizer.cpp @@ -139,11 +139,12 @@ InputOrderInfoPtr ReadInOrderOptimizer::getInputOrder(const StorageMetadataPtr & if (ok) order_key_prefix_descr.push_back(required_sort_description[i]); + else + break; } if (order_key_prefix_descr.empty()) return {}; - return std::make_shared(std::move(order_key_prefix_descr), read_direction); }