2018-09-12 05:41:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-24 14:22:58 +00:00
|
|
|
#include <unordered_set>
|
2019-01-25 11:43:19 +00:00
|
|
|
#include <map>
|
2019-01-24 14:22:58 +00:00
|
|
|
|
2018-10-11 19:28:59 +00:00
|
|
|
#include <Core/Names.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2018-10-30 16:31:21 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-01-25 11:43:19 +00:00
|
|
|
#include <Interpreters/Aliases.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-10-11 19:28:59 +00:00
|
|
|
inline bool functionIsInOperator(const String & name)
|
|
|
|
{
|
|
|
|
return name == "in" || name == "notIn";
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool functionIsInOrGlobalInOperator(const String & name)
|
|
|
|
{
|
|
|
|
return functionIsInOperator(name) || name == "globalIn" || name == "globalNotIn";
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:14:17 +00:00
|
|
|
class ASTFunction;
|
|
|
|
class ASTIdentifier;
|
|
|
|
class ASTExpressionList;
|
|
|
|
struct ASTTablesInSelectQueryElement;
|
2019-01-24 14:22:58 +00:00
|
|
|
class Context;
|
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;
|
|
|
|
const String count_distinct_implementation;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
ExtractedSettings(const T & settings)
|
|
|
|
: max_ast_depth(settings.max_ast_depth),
|
|
|
|
max_expanded_ast_elements(settings.max_expanded_ast_elements),
|
|
|
|
count_distinct_implementation(settings.count_distinct_implementation)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
public:
|
2019-01-10 18:58:55 +00:00
|
|
|
using TableWithColumnNames = std::pair<DatabaseAndTableWithAlias, Names>;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
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
|
|
|
|
2019-01-11 14:09:23 +00:00
|
|
|
const Aliases & aliases;
|
|
|
|
const ExtractedSettings settings;
|
2019-01-24 14:22:58 +00:00
|
|
|
const Context * context;
|
|
|
|
const NameSet * source_columns_set;
|
|
|
|
std::vector<TableWithColumnNames> tables_with_columns;
|
|
|
|
std::unordered_set<String> join_using_columns;
|
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)
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2019-01-24 14:22:58 +00:00
|
|
|
Data(const Aliases & aliases_, ExtractedSettings && settings_, const Context & context_,
|
|
|
|
const NameSet & source_columns_set, Names && all_columns)
|
2019-01-11 14:09:23 +00:00
|
|
|
: aliases(aliases_)
|
|
|
|
, settings(settings_)
|
2019-01-24 14:22:58 +00:00
|
|
|
, context(&context_)
|
|
|
|
, source_columns_set(&source_columns_set)
|
|
|
|
, level(0)
|
|
|
|
{
|
|
|
|
tables_with_columns.emplace_back(DatabaseAndTableWithAlias{}, std::move(all_columns));
|
|
|
|
}
|
|
|
|
|
|
|
|
Data(const Aliases & aliases_, ExtractedSettings && settings_)
|
|
|
|
: aliases(aliases_)
|
|
|
|
, settings(settings_)
|
|
|
|
, context(nullptr)
|
|
|
|
, source_columns_set(nullptr)
|
2019-01-11 14:09:23 +00:00
|
|
|
, level(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool processAsterisks() const { return !tables_with_columns.empty(); }
|
|
|
|
};
|
2019-01-10 18:58:55 +00:00
|
|
|
|
2019-01-11 14:09:23 +00:00
|
|
|
QueryNormalizer(Data & data)
|
|
|
|
: visitor_data(data)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
visit(ast, visitor_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Data & visitor_data;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2019-01-11 17:14:17 +00:00
|
|
|
static void visit(ASTPtr & query, Data & data);
|
|
|
|
|
|
|
|
static void visit(ASTIdentifier &, ASTPtr &, Data &);
|
|
|
|
static void visit(ASTFunction &, const ASTPtr &, Data &);
|
|
|
|
static void visit(ASTExpressionList &, const ASTPtr &, Data &);
|
|
|
|
static void visit(ASTTablesInSelectQueryElement &, const ASTPtr &, Data &);
|
|
|
|
static void visit(ASTSelectQuery &, const ASTPtr &, Data &);
|
|
|
|
|
|
|
|
static void visitChildren(const ASTPtr &, Data & data);
|
2019-01-24 14:22:58 +00:00
|
|
|
|
|
|
|
static void extractTablesWithColumns(const ASTSelectQuery & select_query, Data & data);
|
|
|
|
static void extractJoinUsingColumns(const ASTPtr ast, Data & data);
|
2018-09-12 05:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|