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>
|
2020-03-03 14:25:45 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2018-11-02 18:53:23 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-08-13 12:39:03 +00:00
|
|
|
class ASTFunction;
|
2019-09-03 16:56:32 +00:00
|
|
|
class AnalyzedJoin;
|
|
|
|
class Context;
|
2020-02-26 19:33:09 +00:00
|
|
|
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>;
|
2019-08-13 12:39:03 +00:00
|
|
|
|
2018-11-02 18:53:23 +00:00
|
|
|
struct SyntaxAnalyzerResult
|
|
|
|
{
|
|
|
|
StoragePtr storage;
|
2019-09-02 19:58:45 +00:00
|
|
|
std::shared_ptr<AnalyzedJoin> analyzed_join;
|
2018-11-02 18:53:23 +00:00
|
|
|
|
2018-11-08 17:28:52 +00:00
|
|
|
NamesAndTypesList source_columns;
|
2020-03-03 14:25:45 +00:00
|
|
|
NameSet source_columns_set; /// Set of names of source_columns.
|
2019-08-09 14:50:04 +00:00
|
|
|
/// 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-08 17:28:52 +00:00
|
|
|
|
2018-11-02 18:53:23 +00:00
|
|
|
Aliases aliases;
|
2019-08-13 12:39:03 +00:00
|
|
|
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.
|
2018-11-08 15:43:14 +00:00
|
|
|
/// 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.
|
2018-11-08 15:43:14 +00:00
|
|
|
/// 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-08-09 14:50:04 +00:00
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
/// Results of scalar sub queries
|
|
|
|
Scalars scalars;
|
|
|
|
|
2019-10-28 17:27:43 +00:00
|
|
|
bool maybe_optimize_trivial_count = false;
|
|
|
|
|
2020-03-03 14:25:45 +00:00
|
|
|
SyntaxAnalyzerResult(const NamesAndTypesList & source_columns_, StoragePtr storage_ = {}, bool add_virtuals = true)
|
|
|
|
: storage(storage_)
|
|
|
|
, source_columns(source_columns_)
|
|
|
|
{
|
|
|
|
collectSourceColumns(add_virtuals);
|
|
|
|
}
|
|
|
|
|
|
|
|
void collectSourceColumns(bool add_virtuals);
|
|
|
|
void collectUsedColumns(const ASTPtr & query);
|
2019-08-09 14:50:04 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-11-08 15:43:14 +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:
|
2020-02-26 19:33:09 +00:00
|
|
|
SyntaxAnalyzer(const Context & context_)
|
2019-01-09 16:16:59 +00:00
|
|
|
: context(context_)
|
|
|
|
{}
|
2018-11-08 11:17:31 +00:00
|
|
|
|
2020-02-26 19:33:09 +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(
|
2018-11-08 15:43:14 +00:00
|
|
|
ASTPtr & query,
|
2020-03-03 14:25:45 +00:00
|
|
|
SyntaxAnalyzerResult && result,
|
2020-02-26 19:33:09 +00:00
|
|
|
const SelectQueryOptions & select_options = {},
|
2020-03-03 14:25:45 +00:00
|
|
|
const std::vector<TableWithColumnNamesAndTypes> & tables_with_columns = {},
|
2020-02-26 19:33:09 +00:00
|
|
|
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;
|
2020-02-26 19:33:09 +00:00
|
|
|
|
2020-02-27 15:06:04 +00:00
|
|
|
void normalize(ASTPtr & query, Aliases & aliases, const Settings & settings) const;
|
2018-11-02 18:53:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|