ClickHouse/src/Interpreters/ColumnAliasesVisitor.h

82 lines
2.8 KiB
C++
Raw Normal View History

#pragma once
2020-12-12 16:42:15 +00:00
#include <Core/Names.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <Storages/ColumnsDescription.h>
namespace DB
{
class IAST;
using ASTPtr = std::shared_ptr<IAST>;
class IDataType;
2020-12-12 16:42:15 +00:00
class ASTFunction;
class ASTIdentifier;
using DataTypePtr = std::shared_ptr<const IDataType>;
2020-12-13 11:30:53 +00:00
/// 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;
2020-12-17 02:17:59 +00:00
/// 1. Rewrite the filters in query when enable optimize_respect_aliases
2020-12-13 11:30:53 +00:00
/// this could help with `optimize_trivial_count`, Partition Prune in `KeyCondition` and secondary indexes.
2021-01-12 20:24:18 +00:00
/// eg: select max(value) from test_table where day2 = today(), filters will be: ((toDate(timestamp) + 1) + 1) = today() .
2020-12-13 11:30:53 +00:00
/// 2. Alias on alias for `required_columns` extracted in `InterpreterSelectQuery.cpp`, it could help get all dependent physical columns for query.
2021-01-12 20:24:18 +00:00
/// eg: select day2 from test_table. `required_columns` can got require columns from the temporary rewritten AST `((toDate(timestamp) + 1) + 1)`.
2020-12-13 11:30:53 +00:00
/// 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.
2021-01-08 08:18:12 +00:00
/// But we generate temporary asts and generate temporary Actions to get the `InputOrderInfo`
2020-12-13 11:30:53 +00:00
/// eg: select day1 from test_table order by day1;
class ColumnAliasesMatcher
{
public:
using Visitor = InDepthNodeVisitor<ColumnAliasesMatcher, false>;
struct Data
{
const ColumnsDescription & columns;
2020-12-15 08:35:19 +00:00
/// forbidden_columns are from array join, we can't rewrite alias columns involved in array join.
/// Do not analyze joined columns.
/// They may have aliases and come to description as is.
2020-12-12 16:42:15 +00:00
const NameSet & forbidden_columns;
ContextPtr context;
2020-12-15 08:35:19 +00:00
/// private_aliases are from lambda, so these are local names.
2020-12-12 16:42:15 +00:00
NameSet private_aliases;
Data(const ColumnsDescription & columns_, const NameSet & forbidden_columns_, ContextPtr context_)
: columns(columns_)
2020-12-12 16:42:15 +00:00
, forbidden_columns(forbidden_columns_)
, context(context_)
{}
};
static void visit(ASTPtr & ast, Data & data);
static bool needChildVisit(const ASTPtr & node, const ASTPtr & child);
2020-12-12 16:42:15 +00:00
private:
2020-12-13 01:55:56 +00:00
static void visit(ASTIdentifier & node, ASTPtr & ast, Data & data);
2020-12-12 16:42:15 +00:00
static void visit(ASTFunction & node, ASTPtr & ast, Data & data);
};
using ColumnAliasesVisitor = ColumnAliasesMatcher::Visitor;
}