ClickHouse/dbms/src/Interpreters/SyntaxAnalyzer.h

96 lines
3.1 KiB
C++
Raw Normal View History

2018-11-02 18:53:23 +00:00
#pragma once
2019-10-19 20:36:35 +00:00
#include <Core/Block.h>
2019-09-03 16:56:32 +00:00
#include <Core/NamesAndTypes.h>
2019-01-25 11:43:19 +00:00
#include <Interpreters/Aliases.h>
2019-03-18 14:56:33 +00:00
#include <Interpreters/SelectQueryOptions.h>
#include <Storages/IStorage_fwd.h>
2018-11-02 18:53:23 +00:00
namespace DB
{
class ASTFunction;
2019-09-03 16:56:32 +00:00
class AnalyzedJoin;
class Context;
struct Settings;
2019-09-03 16:56:32 +00:00
struct SelectQueryOptions;
2019-10-19 20:36:35 +00:00
using Scalars = std::map<String, Block>;
2018-11-02 18:53:23 +00:00
struct SyntaxAnalyzerResult
{
StoragePtr storage;
std::shared_ptr<AnalyzedJoin> analyzed_join;
2018-11-02 18:53:23 +00:00
NamesAndTypesList source_columns;
/// Set of columns that are enough to read from the table to evaluate the expression. It does not include joined columns.
NamesAndTypesList required_source_columns;
2018-11-02 18:53:23 +00:00
Aliases aliases;
std::vector<const ASTFunction *> aggregates;
2018-11-02 18:53:23 +00:00
/// 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;
/// Predicate optimizer overrides the sub queries
bool rewrite_subqueries = false;
2019-10-19 20:36:35 +00:00
/// Results of scalar sub queries
Scalars scalars;
bool maybe_optimize_trivial_count = false;
void collectUsedColumns(const ASTPtr & query, const NamesAndTypesList & additional_source_columns);
Names requiredSourceColumns() const { return required_source_columns.getNames(); }
2019-10-19 20:36:35 +00:00
const Scalars & getScalars() const { return scalars; }
2018-11-02 18:53:23 +00:00
};
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:
SyntaxAnalyzer(const Context & context_)
2019-01-09 16:16:59 +00:00
: context(context_)
{}
2018-11-08 11:17:31 +00:00
/// Analyze and rewrite not select query
SyntaxAnalyzerResultPtr analyze(ASTPtr & query, const NamesAndTypesList & source_columns_, StoragePtr storage = {}) const;
/// Analyze and rewrite select query
SyntaxAnalyzerResultPtr analyzeSelect(
ASTPtr & query,
const NamesAndTypesList & source_columns,
StoragePtr storage = {},
const SelectQueryOptions & select_options = {},
const Names & required_result_columns = {}) 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;
void rewriteAst(ASTPtr & query, const Settings & settings, Aliases & aliases) const;
2018-11-02 18:53:23 +00:00
};
}