2018-09-12 05:41:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-25 11:43:19 +00:00
|
|
|
#include <map>
|
2019-01-24 14:22:58 +00:00
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2019-01-25 11:43:19 +00:00
|
|
|
#include <Interpreters/Aliases.h>
|
2021-03-30 13:51:45 +00:00
|
|
|
#include <Core/Names.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-02-11 14:19:09 +00:00
|
|
|
class ASTSelectQuery;
|
2019-01-11 17:14:17 +00:00
|
|
|
class ASTIdentifier;
|
|
|
|
struct ASTTablesInSelectQueryElement;
|
2019-01-24 14:22:58 +00:00
|
|
|
class Context;
|
2022-09-23 11:35:22 +00:00
|
|
|
class ASTQueryParameter;
|
2019-01-11 17:14:17 +00:00
|
|
|
|
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
class QueryNormalizer
|
|
|
|
{
|
2018-10-18 15:03:14 +00:00
|
|
|
/// Extracts settings, mostly to show which are used and which are not.
|
|
|
|
struct ExtractedSettings
|
|
|
|
{
|
|
|
|
const UInt64 max_ast_depth;
|
|
|
|
const UInt64 max_expanded_ast_elements;
|
2021-03-30 13:51:45 +00:00
|
|
|
bool prefer_column_name_to_alias;
|
2018-10-18 15:03:14 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2022-03-13 11:59:20 +00:00
|
|
|
ExtractedSettings(const T & settings) /// NOLINT
|
2021-03-30 13:51:45 +00:00
|
|
|
: max_ast_depth(settings.max_ast_depth)
|
|
|
|
, max_expanded_ast_elements(settings.max_expanded_ast_elements)
|
|
|
|
, prefer_column_name_to_alias(settings.prefer_column_name_to_alias)
|
|
|
|
{
|
|
|
|
}
|
2018-10-18 15:03:14 +00:00
|
|
|
};
|
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
public:
|
2019-01-11 14:09:23 +00:00
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
using SetOfASTs = std::set<const IAST *>;
|
|
|
|
using MapOfASTs = std::map<ASTPtr, ASTPtr>;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2021-08-05 16:52:00 +00:00
|
|
|
Aliases & aliases;
|
2021-03-30 13:51:45 +00:00
|
|
|
const NameSet & source_columns_set;
|
|
|
|
ExtractedSettings settings;
|
2022-10-17 16:11:22 +00:00
|
|
|
NameSet query_parameters;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2019-01-11 14:09:23 +00:00
|
|
|
/// tmp data
|
|
|
|
size_t level;
|
|
|
|
MapOfASTs finished_asts; /// already processed vertices (and by what they replaced)
|
|
|
|
SetOfASTs current_asts; /// vertices in the current call stack of this method
|
|
|
|
std::string current_alias; /// the alias referencing to the ancestor of ast (the deepest ancestor with aliases)
|
2021-06-07 20:59:38 +00:00
|
|
|
const bool ignore_alias; /// normalize query without any aliases
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2021-06-07 20:59:38 +00:00
|
|
|
/// It's Ok to have "c + 1 AS c" in queries, but not in table definition
|
|
|
|
const bool allow_self_aliases; /// for constructs like "SELECT column + 1 AS column"
|
2022-10-19 16:30:03 +00:00
|
|
|
bool is_create_parameterized_view;
|
2021-06-07 20:59:38 +00:00
|
|
|
|
2022-10-19 16:30:03 +00:00
|
|
|
Data(Aliases & aliases_, const NameSet & source_columns_set_, bool ignore_alias_, ExtractedSettings && settings_, bool allow_self_aliases_, bool is_create_parameterized_view_ = false)
|
2019-01-24 14:22:58 +00:00
|
|
|
: aliases(aliases_)
|
2021-03-30 13:51:45 +00:00
|
|
|
, source_columns_set(source_columns_set_)
|
2019-01-24 14:22:58 +00:00
|
|
|
, settings(settings_)
|
2019-01-11 14:09:23 +00:00
|
|
|
, level(0)
|
2021-02-10 14:12:49 +00:00
|
|
|
, ignore_alias(ignore_alias_)
|
2021-06-07 20:59:38 +00:00
|
|
|
, allow_self_aliases(allow_self_aliases_)
|
2022-10-19 16:30:03 +00:00
|
|
|
, is_create_parameterized_view(is_create_parameterized_view_)
|
2019-01-11 14:09:23 +00:00
|
|
|
{}
|
|
|
|
};
|
2019-01-10 18:58:55 +00:00
|
|
|
|
2021-06-07 20:59:38 +00:00
|
|
|
explicit QueryNormalizer(Data & data)
|
2019-01-11 14:09:23 +00:00
|
|
|
: visitor_data(data)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
visit(ast, visitor_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Data & visitor_data;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2020-03-09 00:28:05 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data);
|
2019-01-11 17:14:17 +00:00
|
|
|
|
|
|
|
static void visit(ASTIdentifier &, ASTPtr &, Data &);
|
|
|
|
static void visit(ASTTablesInSelectQueryElement &, const ASTPtr &, Data &);
|
|
|
|
static void visit(ASTSelectQuery &, const ASTPtr &, Data &);
|
|
|
|
|
2020-12-28 09:56:38 +00:00
|
|
|
static void visitChildren(IAST * node, Data & data);
|
2018-09-12 05:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|