2018-10-12 15:41:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-13 22:39:18 +00:00
|
|
|
#include <string_view>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <Core/ColumnNumbers.h>
|
|
|
|
#include <Core/ColumnWithTypeAndName.h>
|
2022-05-12 16:39:50 +00:00
|
|
|
#include <Core/NamesAndTypes.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2019-01-18 16:30:35 +00:00
|
|
|
#include <Interpreters/PreparedSets.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <QueryPipeline/SizeLimits.h>
|
2018-10-12 15:41:28 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-03-18 07:32:42 +00:00
|
|
|
class ASTExpressionList;
|
2018-10-12 15:41:28 +00:00
|
|
|
class ASTFunction;
|
|
|
|
|
2020-02-10 15:50:12 +00:00
|
|
|
class ExpressionActions;
|
|
|
|
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
|
|
|
|
|
2020-09-10 07:30:03 +00:00
|
|
|
class ActionsDAG;
|
|
|
|
using ActionsDAGPtr = std::shared_ptr<ActionsDAG>;
|
|
|
|
|
2020-09-30 13:31:06 +00:00
|
|
|
class IFunctionOverloadResolver;
|
|
|
|
using FunctionOverloadResolverPtr = std::shared_ptr<IFunctionOverloadResolver>;
|
|
|
|
|
2020-09-25 07:31:06 +00:00
|
|
|
/// The case of an explicit enumeration of values.
|
2023-04-25 18:14:08 +00:00
|
|
|
FutureSetPtr makeExplicitSet(
|
|
|
|
const ASTFunction * node, const ActionsDAG & actions, ContextPtr context, PreparedSets & prepared_sets);
|
2018-10-12 15:41:28 +00:00
|
|
|
|
|
|
|
/** For ActionsVisitor
|
|
|
|
* A stack of ExpressionActions corresponding to nested lambda expressions.
|
|
|
|
* The new action should be added to the highest possible level.
|
|
|
|
* For example, in the expression "select arrayMap(x -> x + column1 * column2, array1)"
|
|
|
|
* calculation of the product must be done outside the lambda expression (it does not depend on x),
|
|
|
|
* and the calculation of the sum is inside (depends on x).
|
|
|
|
*/
|
2021-04-10 23:33:54 +00:00
|
|
|
struct ScopeStack : WithContext
|
2018-10-12 15:41:28 +00:00
|
|
|
{
|
2021-03-03 20:01:07 +00:00
|
|
|
class Index;
|
|
|
|
using IndexPtr = std::unique_ptr<Index>;
|
|
|
|
|
2020-09-13 13:51:31 +00:00
|
|
|
struct Level
|
|
|
|
{
|
2020-11-11 16:52:27 +00:00
|
|
|
ActionsDAGPtr actions_dag;
|
2021-03-03 20:01:07 +00:00
|
|
|
IndexPtr index;
|
2020-09-13 13:51:31 +00:00
|
|
|
NameSet inputs;
|
2021-03-03 20:01:07 +00:00
|
|
|
|
|
|
|
Level();
|
2022-02-25 19:04:48 +00:00
|
|
|
Level(Level &&) noexcept;
|
2021-03-03 20:01:07 +00:00
|
|
|
~Level();
|
2020-09-13 13:51:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using Levels = std::vector<Level>;
|
2018-10-12 15:41:28 +00:00
|
|
|
|
|
|
|
Levels stack;
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ScopeStack(ActionsDAGPtr actions_dag, ContextPtr context_);
|
2018-10-12 15:41:28 +00:00
|
|
|
|
|
|
|
void pushLevel(const NamesAndTypesList & input_columns);
|
|
|
|
|
|
|
|
size_t getColumnLevel(const std::string & name);
|
|
|
|
|
2020-09-10 16:01:41 +00:00
|
|
|
void addColumn(ColumnWithTypeAndName column);
|
2020-09-10 07:30:03 +00:00
|
|
|
void addAlias(const std::string & name, std::string alias);
|
2020-11-03 11:28:28 +00:00
|
|
|
void addArrayJoin(const std::string & source_name, std::string result_name);
|
2022-03-18 07:32:42 +00:00
|
|
|
void addFunction(const FunctionOverloadResolverPtr & function, const Names & argument_names, std::string result_name);
|
2018-10-12 15:41:28 +00:00
|
|
|
|
2020-09-10 07:30:03 +00:00
|
|
|
ActionsDAGPtr popLevel();
|
2018-10-12 15:41:28 +00:00
|
|
|
|
2020-09-30 11:45:47 +00:00
|
|
|
const ActionsDAG & getLastActions() const;
|
2021-03-03 20:01:07 +00:00
|
|
|
const Index & getLastActionsIndex() const;
|
2020-09-10 16:01:41 +00:00
|
|
|
std::string dumpNames() const;
|
2018-10-12 15:41:28 +00:00
|
|
|
};
|
|
|
|
|
2019-08-15 13:54:59 +00:00
|
|
|
class ASTIdentifier;
|
|
|
|
class ASTFunction;
|
|
|
|
class ASTLiteral;
|
2018-10-12 15:41:28 +00:00
|
|
|
|
2022-05-16 17:33:38 +00:00
|
|
|
enum class GroupByKind
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
ORDINARY,
|
|
|
|
ROLLUP,
|
|
|
|
CUBE,
|
|
|
|
GROUPING_SETS,
|
|
|
|
};
|
|
|
|
|
2022-05-25 23:22:29 +00:00
|
|
|
/*
|
|
|
|
* This class stores information about aggregation keys used in GROUP BY clause.
|
|
|
|
* It's used for providing information about aggregation to GROUPING function
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
struct AggregationKeysInfo
|
|
|
|
{
|
|
|
|
AggregationKeysInfo(
|
|
|
|
std::reference_wrapper<const NamesAndTypesList> aggregation_keys_,
|
|
|
|
std::reference_wrapper<const ColumnNumbersList> grouping_set_keys_,
|
|
|
|
GroupByKind group_by_kind_)
|
|
|
|
: aggregation_keys(aggregation_keys_)
|
|
|
|
, grouping_set_keys(grouping_set_keys_)
|
|
|
|
, group_by_kind(group_by_kind_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
AggregationKeysInfo(const AggregationKeysInfo &) = default;
|
|
|
|
AggregationKeysInfo(AggregationKeysInfo &&) = default;
|
|
|
|
|
|
|
|
// Names and types of all used keys
|
|
|
|
const NamesAndTypesList & aggregation_keys;
|
|
|
|
// Indexes of aggregation keys used in each grouping set (only for GROUP BY GROUPING SETS)
|
|
|
|
const ColumnNumbersList & grouping_set_keys;
|
|
|
|
|
|
|
|
GroupByKind group_by_kind;
|
|
|
|
};
|
|
|
|
|
2022-07-18 15:53:30 +00:00
|
|
|
/// Collect ExpressionAction from AST. Returns PreparedSets
|
2019-08-15 13:54:59 +00:00
|
|
|
class ActionsMatcher
|
2018-10-12 15:41:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-08-15 13:54:59 +00:00
|
|
|
using Visitor = ConstInDepthNodeVisitor<ActionsMatcher, true>;
|
2018-10-16 12:34:20 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
struct Data : public WithContext
|
2019-08-14 16:53:30 +00:00
|
|
|
{
|
2019-08-15 13:54:59 +00:00
|
|
|
SizeLimits set_size_limit;
|
|
|
|
size_t subquery_depth;
|
|
|
|
const NamesAndTypesList & source_columns;
|
2022-07-18 15:53:30 +00:00
|
|
|
PreparedSetsPtr prepared_sets;
|
2019-08-15 13:54:59 +00:00
|
|
|
bool no_subqueries;
|
2020-01-27 21:57:44 +00:00
|
|
|
bool no_makeset;
|
2019-08-15 13:54:59 +00:00
|
|
|
bool only_consts;
|
|
|
|
size_t visit_depth;
|
|
|
|
ScopeStack actions_stack;
|
2022-05-25 23:22:29 +00:00
|
|
|
AggregationKeysInfo aggregation_keys_info;
|
2022-06-04 02:01:49 +00:00
|
|
|
bool build_expression_with_window_functions;
|
2023-01-03 10:00:39 +00:00
|
|
|
bool is_create_parameterized_view;
|
2019-08-15 13:54:59 +00:00
|
|
|
|
2020-04-01 12:16:39 +00:00
|
|
|
/*
|
|
|
|
* Remember the last unique column suffix to avoid quadratic behavior
|
|
|
|
* when we add lots of column with same prefix. One counter for all
|
|
|
|
* prefixes is good enough.
|
|
|
|
*/
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t next_unique_suffix;
|
2020-04-01 12:16:39 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
Data(
|
|
|
|
ContextPtr context_,
|
|
|
|
SizeLimits set_size_limit_,
|
|
|
|
size_t subquery_depth_,
|
2022-05-25 23:22:29 +00:00
|
|
|
std::reference_wrapper<const NamesAndTypesList> source_columns_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ActionsDAGPtr actions_dag,
|
2022-07-18 15:53:30 +00:00
|
|
|
PreparedSetsPtr prepared_sets_,
|
2021-04-10 23:33:54 +00:00
|
|
|
bool no_subqueries_,
|
|
|
|
bool no_makeset_,
|
|
|
|
bool only_consts_,
|
2022-06-04 02:01:49 +00:00
|
|
|
AggregationKeysInfo aggregation_keys_info_,
|
2023-01-03 10:00:39 +00:00
|
|
|
bool build_expression_with_window_functions_ = false,
|
|
|
|
bool is_create_parameterized_view_ = false);
|
2019-08-15 13:54:59 +00:00
|
|
|
|
2020-09-10 07:30:03 +00:00
|
|
|
/// Does result of the calculation already exists in the block.
|
2020-09-30 12:32:37 +00:00
|
|
|
bool hasColumn(const String & column_name) const;
|
2022-06-13 22:39:18 +00:00
|
|
|
std::vector<std::string_view> getAllColumnNames() const;
|
|
|
|
|
2020-09-10 16:01:41 +00:00
|
|
|
void addColumn(ColumnWithTypeAndName column)
|
|
|
|
{
|
|
|
|
actions_stack.addColumn(std::move(column));
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:30:03 +00:00
|
|
|
void addAlias(const std::string & name, std::string alias)
|
2019-08-15 14:22:33 +00:00
|
|
|
{
|
2020-09-10 07:30:03 +00:00
|
|
|
actions_stack.addAlias(name, std::move(alias));
|
2019-08-15 14:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 07:30:03 +00:00
|
|
|
void addArrayJoin(const std::string & source_name, std::string result_name)
|
2019-08-15 13:54:59 +00:00
|
|
|
{
|
2020-11-03 11:28:28 +00:00
|
|
|
actions_stack.addArrayJoin(source_name, std::move(result_name));
|
2019-08-15 13:54:59 +00:00
|
|
|
}
|
2020-04-14 17:41:06 +00:00
|
|
|
|
2020-09-10 16:01:41 +00:00
|
|
|
void addFunction(const FunctionOverloadResolverPtr & function,
|
|
|
|
const Names & argument_names,
|
|
|
|
std::string result_name)
|
|
|
|
{
|
2020-11-03 11:28:28 +00:00
|
|
|
actions_stack.addFunction(function, argument_names, std::move(result_name));
|
2020-09-10 16:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionsDAGPtr getActions()
|
|
|
|
{
|
|
|
|
return actions_stack.popLevel();
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:41:06 +00:00
|
|
|
/*
|
|
|
|
* Generate a column name that is not present in the sample block, using
|
|
|
|
* the given prefix and an optional numeric suffix.
|
|
|
|
*/
|
|
|
|
String getUniqueName(const String & prefix)
|
|
|
|
{
|
|
|
|
auto result = prefix;
|
|
|
|
|
|
|
|
// First, try the name without any suffix, because it is currently
|
|
|
|
// used both as a display name and a column id.
|
2020-09-10 16:01:41 +00:00
|
|
|
while (hasColumn(result))
|
2020-04-14 17:41:06 +00:00
|
|
|
{
|
|
|
|
result = prefix + "_" + toString(next_unique_suffix);
|
|
|
|
++next_unique_suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-15 13:54:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void visit(const ASTPtr & ast, Data & data);
|
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr & child);
|
2018-10-12 15:41:28 +00:00
|
|
|
|
|
|
|
private:
|
2019-08-15 13:54:59 +00:00
|
|
|
|
|
|
|
static void visit(const ASTIdentifier & identifier, const ASTPtr & ast, Data & data);
|
|
|
|
static void visit(const ASTFunction & node, const ASTPtr & ast, Data & data);
|
|
|
|
static void visit(const ASTLiteral & literal, const ASTPtr & ast, Data & data);
|
2020-10-21 18:17:27 +00:00
|
|
|
static void visit(ASTExpressionList & expression_list, const ASTPtr & ast, Data & data);
|
2019-08-15 13:54:59 +00:00
|
|
|
|
2023-04-25 18:14:08 +00:00
|
|
|
static FutureSetPtr makeSet(const ASTFunction & node, Data & data, bool no_subqueries);
|
2020-10-21 18:17:27 +00:00
|
|
|
static ASTs doUntuple(const ASTFunction * function, ActionsMatcher::Data & data);
|
|
|
|
static std::optional<NameAndTypePair> getNameAndTypeFromAST(const ASTPtr & ast, Data & data);
|
2018-10-12 15:41:28 +00:00
|
|
|
};
|
|
|
|
|
2019-08-15 13:54:59 +00:00
|
|
|
using ActionsVisitor = ActionsMatcher::Visitor;
|
|
|
|
|
2018-10-12 15:41:28 +00:00
|
|
|
}
|