ClickHouse/dbms/src/Interpreters/SyntaxAnalyzer.h

75 lines
2.1 KiB
C++
Raw Normal View History

2018-11-02 18:53:23 +00:00
#pragma once
#include <Interpreters/AnalyzedJoin.h>
2019-01-25 11:43:19 +00:00
#include <Interpreters/Aliases.h>
2018-11-02 18:53:23 +00:00
namespace DB
{
class IStorage;
using StoragePtr = std::shared_ptr<IStorage>;
2019-01-10 21:05:01 +00:00
NameSet removeDuplicateColumns(NamesAndTypesList & columns);
2018-11-02 18:53:23 +00:00
struct SyntaxAnalyzerResult
{
StoragePtr storage;
NamesAndTypesList source_columns;
2018-11-02 18:53:23 +00:00
Aliases aliases;
/// Which column is needed to be ARRAY-JOIN'ed to get the specified.
/// For example, for `SELECT s.v ... ARRAY JOIN a AS s` will get "s.v" -> "a.v".
NameToNameMap array_join_result_to_source;
/// For the ARRAY JOIN section, mapping from the alias to the full column name.
/// For example, for `ARRAY JOIN [1,2] AS b` "b" -> "array(1,2)" will enter here.
/// Note: not used further.
2018-11-02 18:53:23 +00:00
NameToNameMap array_join_alias_to_name;
/// The backward mapping for array_join_alias_to_name.
/// Note: not used further.
2018-11-02 18:53:23 +00:00
NameToNameMap array_join_name_to_alias;
AnalyzedJoin analyzed_join;
/// Predicate optimizer overrides the sub queries
bool rewrite_subqueries = false;
};
using SyntaxAnalyzerResultPtr = std::shared_ptr<const SyntaxAnalyzerResult>;
2018-11-08 11:17:31 +00:00
/// AST syntax analysis.
/// Optimises AST tree and collect information for further expression analysis.
/// Result AST has the following invariants:
/// * all aliases are substituted
/// * qualified names are translated
/// * scalar subqueries are executed replaced with constants
/// * unneeded columns are removed from SELECT clause
/// * duplicated columns are removed from ORDER BY, LIMIT BY, USING(...).
/// Motivation:
/// * group most of the AST-changing operations in single place
/// * avoid AST rewriting in ExpressionAnalyzer
/// * decompose ExpressionAnalyzer
2018-11-02 18:53:23 +00:00
class SyntaxAnalyzer
{
public:
2019-01-09 16:16:59 +00:00
SyntaxAnalyzer(const Context & context_, size_t subquery_depth_ = 0)
: context(context_)
, subquery_depth(subquery_depth_)
{}
2018-11-08 11:17:31 +00:00
SyntaxAnalyzerResultPtr analyze(
ASTPtr & query,
const NamesAndTypesList & source_columns_,
const Names & required_result_columns = {},
2019-01-09 16:16:59 +00:00
StoragePtr storage = {}) const;
2018-11-08 11:17:31 +00:00
2019-01-09 16:16:59 +00:00
private:
2018-11-08 11:17:31 +00:00
const Context & context;
2019-01-09 16:16:59 +00:00
size_t subquery_depth;
2018-11-02 18:53:23 +00:00
};
}