2018-09-12 05:41:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
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-09-24 03:20:22 +00:00
|
|
|
#include <Interpreters/evaluateQualified.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";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-10 20:35:21 +00:00
|
|
|
using TableNameAndColumnNames = std::pair<DatabaseAndTableWithAlias, Names>;
|
|
|
|
using TableNamesAndColumnNames = std::vector<TableNameAndColumnNames>;
|
2018-09-24 03:20:22 +00:00
|
|
|
|
2018-10-18 15:03:14 +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:
|
|
|
|
using Aliases = std::unordered_map<String, ASTPtr>;
|
|
|
|
|
2018-10-18 15:03:14 +00:00
|
|
|
QueryNormalizer(ASTPtr & query, const Aliases & aliases, ExtractedSettings && settings, const Names & all_columns_name,
|
2018-10-10 20:35:21 +00:00
|
|
|
const TableNamesAndColumnNames & table_names_and_column_names);
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
void perform();
|
|
|
|
|
|
|
|
private:
|
|
|
|
using SetOfASTs = std::set<const IAST *>;
|
|
|
|
using MapOfASTs = std::map<ASTPtr, ASTPtr>;
|
|
|
|
|
|
|
|
ASTPtr & query;
|
|
|
|
const Aliases & aliases;
|
2018-10-18 15:03:14 +00:00
|
|
|
const ExtractedSettings settings;
|
2018-10-10 20:35:21 +00:00
|
|
|
const Names & all_column_names;
|
|
|
|
const TableNamesAndColumnNames & table_names_and_column_names;
|
2018-09-12 05:41:09 +00:00
|
|
|
|
2018-09-24 03:20:22 +00:00
|
|
|
void performImpl(ASTPtr & ast, MapOfASTs & finished_asts, SetOfASTs & current_asts, std::string current_alias, size_t level);
|
2018-09-12 05:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|