ClickHouse/src/Interpreters/ExpressionActions.h

477 lines
15 KiB
C++
Raw Normal View History

#pragma once
#include <Core/Block.h>
#include <Core/ColumnWithTypeAndName.h>
#include <Core/Names.h>
#include <Core/Settings.h>
#include <Common/SipHash.h>
#include <Common/UInt128.h>
2014-06-12 21:12:47 +00:00
#include <unordered_map>
#include <unordered_set>
#include <Parsers/ASTTablesInSelectQuery.h>
2020-08-12 08:55:16 +00:00
#include <DataTypes/DataTypeArray.h>
2020-08-13 20:17:18 +00:00
#include <variant>
#if !defined(ARCADIA_BUILD)
# include "config_core.h"
#endif
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
class Context;
class TableJoin;
class IJoin;
using JoinPtr = std::shared_ptr<IJoin>;
2019-12-08 21:06:37 +00:00
class IExecutableFunction;
using ExecutableFunctionPtr = std::shared_ptr<IExecutableFunction>;
2018-02-02 08:33:36 +00:00
class IFunctionBase;
using FunctionBasePtr = std::shared_ptr<IFunctionBase>;
2019-12-08 21:06:37 +00:00
class IFunctionOverloadResolver;
using FunctionOverloadResolverPtr = std::shared_ptr<IFunctionOverloadResolver>;
Squashed commit of the following: commit e712f469a55ff34ad34b482b15cc4153b7ad7233 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:59:13 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a002823084e3a79bffcc17d479620a68eb0644b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:58:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9e06f407c8ee781ed8ddf98bdfcc31846bf2a0fe Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:55:14 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9581620f1e839f456fa7894aa1f996d5162ac6cd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:54:22 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a8564c68cb6cc3649fafaf401256d43c9a2e777 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:47:34 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit cf60632d78ec656be3304ef4565e859bb6ce80ba Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:40:09 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit ee3d1dc6e0c4ca60e3ac1e0c30d4b3ed1e66eca0 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:22:49 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 65592ef7116a90104fcd524b53ef8b7cf22640f2 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:18:17 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 37972c257320d3b7e7b294e0fdeffff218647bfd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:17:06 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit dd909d149974ce5bed2456de1261aa5a368fd3ff Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:16:28 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 3cf43266ca7e30adf01212b1a739ba5fe43639fd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:15:42 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 6731a3df96d1609286e2536b6432916af7743f0f Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:13:35 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 1b5727e0d56415b7add4cb76110105358663602c Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:11:18 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit bbcf726a55685b8e72f5b40ba0bf1904bd1c0407 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:09:04 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit c03b477d5e2e65014e8906ecfa2efb67ee295af1 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:06:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2986e2fb0466bc18d73693dcdded28fccc0dc66b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:05:44 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 5d6cdef13d2e02bd5c4954983334e9162ab2635b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:04:53 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit f2b819b25ce8b2ccdcb201eefb03e1e6f5aab590 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:01:47 2017 +0300 Less dependencies [#CLICKHOUSE-2]
2017-01-14 09:00:19 +00:00
class IDataType;
using DataTypePtr = std::shared_ptr<const IDataType>;
Squashed commit of the following: commit e712f469a55ff34ad34b482b15cc4153b7ad7233 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:59:13 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a002823084e3a79bffcc17d479620a68eb0644b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:58:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9e06f407c8ee781ed8ddf98bdfcc31846bf2a0fe Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:55:14 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9581620f1e839f456fa7894aa1f996d5162ac6cd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:54:22 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a8564c68cb6cc3649fafaf401256d43c9a2e777 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:47:34 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit cf60632d78ec656be3304ef4565e859bb6ce80ba Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:40:09 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit ee3d1dc6e0c4ca60e3ac1e0c30d4b3ed1e66eca0 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:22:49 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 65592ef7116a90104fcd524b53ef8b7cf22640f2 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:18:17 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 37972c257320d3b7e7b294e0fdeffff218647bfd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:17:06 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit dd909d149974ce5bed2456de1261aa5a368fd3ff Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:16:28 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 3cf43266ca7e30adf01212b1a739ba5fe43639fd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:15:42 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 6731a3df96d1609286e2536b6432916af7743f0f Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:13:35 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 1b5727e0d56415b7add4cb76110105358663602c Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:11:18 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit bbcf726a55685b8e72f5b40ba0bf1904bd1c0407 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:09:04 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit c03b477d5e2e65014e8906ecfa2efb67ee295af1 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:06:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2986e2fb0466bc18d73693dcdded28fccc0dc66b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:05:44 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 5d6cdef13d2e02bd5c4954983334e9162ab2635b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:04:53 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit f2b819b25ce8b2ccdcb201eefb03e1e6f5aab590 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:01:47 2017 +0300 Less dependencies [#CLICKHOUSE-2]
2017-01-14 09:00:19 +00:00
class ExpressionActions;
class CompiledExpressionCache;
2020-08-13 20:17:18 +00:00
class ArrayJoinAction;
using ArrayJoinActionPtr = std::shared_ptr<ArrayJoinAction>;
2017-06-02 21:37:28 +00:00
/** Action on the block.
*/
struct ExpressionAction
{
private:
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
public:
enum Type
{
ADD_COLUMN,
REMOVE_COLUMN,
COPY_COLUMN,
APPLY_FUNCTION,
2020-08-13 20:17:18 +00:00
/// Replaces the source column with array into column with elements.
/// Duplicates the values in the remaining columns by the number of elements in the arrays.
/// Source column is removed from block.
ARRAY_JOIN,
2017-06-02 21:37:28 +00:00
/// Reorder and rename the columns, delete the extra ones. The same column names are allowed in the result.
PROJECT,
/// Add columns with alias names. This columns are the same as non-aliased. PROJECT columns if you need to modify them.
ADD_ALIASES,
};
2019-06-16 18:12:14 +00:00
Type type{};
2020-08-13 20:17:18 +00:00
/// For ADD/REMOVE/ARRAY_JOIN/COPY_COLUMN.
std::string source_name;
std::string result_name;
DataTypePtr result_type;
2018-08-09 19:17:55 +00:00
/// If COPY_COLUMN can replace the result column.
bool can_replace = false;
/// For ADD_COLUMN.
ColumnPtr added_column;
/// For APPLY_FUNCTION.
/// OverloadResolver is used before action was added to ExpressionActions (when we don't know types of arguments).
2019-12-08 21:06:37 +00:00
FunctionOverloadResolverPtr function_builder;
2018-09-27 15:16:47 +00:00
/// Can be used after action was added to ExpressionActions if we want to get function signature or properties like monotonicity.
FunctionBasePtr function_base;
2018-09-27 15:16:47 +00:00
/// Prepared function which is used in function execution.
2019-12-08 21:06:37 +00:00
ExecutableFunctionPtr function;
Names argument_names;
bool is_function_compiled = false;
/// For JOIN
std::shared_ptr<const TableJoin> table_join;
JoinPtr join;
/// For PROJECT.
NamesWithAliases projection;
2017-06-02 21:37:28 +00:00
/// If result_name_ == "", as name "function_name(arguments separated by commas) is used".
static ExpressionAction applyFunction(
2019-12-08 21:06:37 +00:00
const FunctionOverloadResolverPtr & function_, const std::vector<std::string> & argument_names_, std::string result_name_ = "");
static ExpressionAction addColumn(const ColumnWithTypeAndName & added_column_);
static ExpressionAction removeColumn(const std::string & removed_name);
2018-08-09 19:17:55 +00:00
static ExpressionAction copyColumn(const std::string & from_name, const std::string & to_name, bool can_replace = false);
static ExpressionAction project(const NamesWithAliases & projected_columns_);
static ExpressionAction project(const Names & projected_columns_);
static ExpressionAction addAliases(const NamesWithAliases & aliased_columns_);
2020-08-13 20:17:18 +00:00
static ExpressionAction arrayJoin(std::string source_name, std::string result_name);
2017-06-02 21:37:28 +00:00
/// Which columns necessary to perform this action.
Names getNeededColumns() const;
std::string toString() const;
2018-08-28 17:06:42 +00:00
bool operator==(const ExpressionAction & other) const;
struct ActionHash
{
UInt128 operator()(const ExpressionAction & action) const;
2018-08-28 17:06:42 +00:00
};
private:
friend class ExpressionActions;
void prepare(Block & sample_block, const Settings & settings, NameSet & names_not_for_constant_folding);
2020-06-25 20:28:41 +00:00
void execute(Block & block, bool dry_run) const;
};
2020-08-12 13:30:02 +00:00
class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
2020-09-10 07:30:03 +00:00
class ActionsDAG
{
public:
enum class Type
{
/// Column which must be in input.
INPUT,
/// Constant column with known value.
COLUMN,
/// Another one name for column.
ALIAS,
/// Function arrayJoin. Specially separated because it changes the number of rows.
ARRAY_JOIN,
2020-09-10 16:01:41 +00:00
FUNCTION,
2020-09-10 07:30:03 +00:00
};
struct Node
{
std::vector<Node *> children;
2020-09-11 12:24:41 +00:00
/// This field is filled if current node is replaced by existing node with the same name.
Node * renaming_parent = nullptr;
2020-09-10 07:30:03 +00:00
Type type;
std::string result_name;
DataTypePtr result_type;
2020-09-13 13:51:31 +00:00
std::string unique_column_name_for_array_join;
2020-09-10 16:01:41 +00:00
FunctionOverloadResolverPtr function_builder;
/// Can be used after action was added to ExpressionActions if we want to get function signature or properties like monotonicity.
FunctionBasePtr function_base;
/// Prepared function which is used in function execution.
ExecutableFunctionPtr function;
2020-09-10 07:30:03 +00:00
/// For COLUMN node and propagated constants.
ColumnPtr column;
2020-09-10 16:01:41 +00:00
/// Some functions like `ignore()` always return constant but can't be replaced by constant it.
/// We calculate such constants in order to avoid unnecessary materialization, but prohibit it's folding.
bool allow_constant_folding = true;
2020-09-10 07:30:03 +00:00
};
using Index = std::unordered_map<std::string_view, Node *>;
private:
std::list<Node> nodes;
Index index;
public:
ActionsDAG() = default;
ActionsDAG(const ActionsDAG &) = delete;
ActionsDAG & operator=(const ActionsDAG &) = delete;
2020-10-07 18:37:27 +00:00
explicit ActionsDAG(const NamesAndTypesList & inputs);
explicit ActionsDAG(const ColumnsWithTypeAndName & inputs);
2020-09-10 07:30:03 +00:00
const Index & getIndex() const { return index; }
2020-09-10 18:36:51 +00:00
ColumnsWithTypeAndName getResultColumns() const;
2020-09-10 16:01:41 +00:00
NamesAndTypesList getNamesAndTypesList() const;
2020-09-11 12:24:41 +00:00
Names getNames() const;
2020-09-10 16:01:41 +00:00
std::string dumpNames() const;
2020-09-10 07:30:03 +00:00
const Node & addInput(std::string name, DataTypePtr type);
2020-09-13 13:51:31 +00:00
const Node & addInput(ColumnWithTypeAndName column);
2020-09-10 16:01:41 +00:00
const Node & addColumn(ColumnWithTypeAndName column);
2020-09-13 13:51:31 +00:00
const Node & addAlias(const std::string & name, std::string alias, bool can_replace = false);
const Node & addArrayJoin(const std::string & source_name, std::string result_name, std::string unique_column_name);
2020-09-10 16:01:41 +00:00
const Node & addFunction(
const FunctionOverloadResolverPtr & function,
const Names & argument_names,
std::string result_name,
bool compile_expressions);
ExpressionActionsPtr buildExpressions(const Context & context);
2020-09-10 07:30:03 +00:00
private:
2020-09-11 12:24:41 +00:00
Node & addNode(Node node, bool can_replace = false);
2020-09-10 07:30:03 +00:00
Node & getNode(const std::string & name);
};
2020-09-11 12:24:41 +00:00
using ActionsDAGPtr = std::shared_ptr<ActionsDAG>;
2017-06-02 21:37:28 +00:00
/** Contains a sequence of actions on the block.
*/
class ExpressionActions
{
2020-10-07 18:37:27 +00:00
private:
using Node = ActionsDAG::Node;
using Index = ActionsDAG::Index;
2020-10-07 18:37:27 +00:00
struct Argument
{
size_t position;
bool can_remove;
};
2020-10-07 18:37:27 +00:00
using Arguments = std::vector<Argument>;
2020-10-07 18:37:27 +00:00
struct Action
{
Node * node;
Arguments arguments;
size_t result_position;
};
2020-10-07 18:37:27 +00:00
using Actions = std::vector<Action>;
struct ExecutionContext
{
ColumnsWithTypeAndName input_columns;
ColumnsWithTypeAndName columns;
size_t num_rows;
};
std::list<Node> nodes;
Index index;
Actions actions;
2020-06-12 14:32:47 +00:00
2020-10-07 18:37:27 +00:00
NamesAndTypesList required_columns;
2020-10-07 18:37:27 +00:00
public:
~ExpressionActions();
2020-10-07 18:37:27 +00:00
ExpressionActions(const ExpressionActions & other) = default;
2017-06-02 21:37:28 +00:00
/// Adds to the beginning the removal of all extra columns.
void prependProjectInput();
2020-08-13 18:40:21 +00:00
/// Splits actions into two parts. Returned half may be swapped with ARRAY JOIN.
2020-08-13 20:17:18 +00:00
/// Returns nullptr if no actions may be moved before ARRAY JOIN.
2020-08-12 13:30:02 +00:00
ExpressionActionsPtr splitActionsBeforeArrayJoin(const NameSet & array_joined_columns);
2017-06-02 21:37:28 +00:00
/// - Adds actions to delete all but the specified columns.
/// - Removes unused input columns.
/// - Can somehow optimize the expression.
/// - Does not reorder the columns.
/// - Does not remove "unexpected" columns (for example, added by functions).
/// - If output_columns is empty, leaves one arbitrary column (so that the number of rows in the block is not lost).
2020-10-07 18:37:27 +00:00
// void finalize(const Names & output_columns);
2017-06-02 21:37:28 +00:00
/// Get a list of input columns.
2020-10-07 18:37:27 +00:00
Names getRequiredColumns() const;
const NamesAndTypesList & getRequiredColumnsWithTypes() const;
2017-06-02 21:37:28 +00:00
/// Execute the expression on the block. The block must contain all the columns returned by getRequiredColumns.
void execute(Block & block, bool dry_run = false) const;
2020-09-08 10:40:53 +00:00
bool hasArrayJoin() const;
2017-06-02 21:37:28 +00:00
/// Obtain a sample block that contains the names and types of result columns.
2020-10-07 18:37:27 +00:00
const Block & getSampleBlock() const;
std::string dumpActions() const;
static std::string getSmallestColumn(const NamesAndTypesList & columns);
const Settings & getSettings() const { return settings; }
2018-08-28 17:06:42 +00:00
2019-10-11 17:27:54 +00:00
/// Check if column is always zero. True if it's definite, false if we can't say for sure.
/// Call it only after subqueries for sets were executed.
bool checkColumnIsAlwaysFalse(const String & column_name) const;
private:
2019-07-31 10:22:56 +00:00
Settings settings;
#if USE_EMBEDDED_COMPILER
std::shared_ptr<CompiledExpressionCache> compilation_cache;
#endif
void checkLimits(Block & block) const;
2020-10-07 18:37:27 +00:00
void executeAction(const Action & action, ExecutionContext & execution_context, bool dry_run);
};
2017-06-02 21:37:28 +00:00
/** The sequence of transformations over the block.
* It is assumed that the result of each step is fed to the input of the next step.
* Used to execute parts of the query individually.
*
2017-06-02 21:37:28 +00:00
* For example, you can create a chain of two steps:
* 1) evaluate the expression in the WHERE clause,
* 2) calculate the expression in the SELECT section,
* and between the two steps do the filtering by value in the WHERE clause.
*/
struct ExpressionActionsChain
{
2020-08-12 08:55:16 +00:00
explicit ExpressionActionsChain(const Context & context_) : context(context_) {}
2020-08-13 20:17:18 +00:00
struct Step
{
2020-08-19 19:33:49 +00:00
virtual ~Step() = default;
explicit Step(Names required_output_) : required_output(std::move(required_output_)) {}
2020-08-12 08:55:16 +00:00
2018-04-23 19:05:46 +00:00
/// Columns were added to the block before current step in addition to prev step output.
2018-04-12 09:45:24 +00:00
NameSet additional_input;
2018-04-23 19:05:46 +00:00
/// Columns which are required in the result of current step.
Names required_output;
2018-04-23 19:05:46 +00:00
/// True if column from required_output is needed only for current step and not used in next actions
/// (and can be removed from block). Example: filter column for where actions.
2018-04-20 19:38:34 +00:00
/// If not empty, has the same size with required_output; is filled in finalize().
2018-06-29 11:42:44 +00:00
std::vector<bool> can_remove_required_output;
2020-08-19 19:33:49 +00:00
virtual const NamesAndTypesList & getRequiredColumns() const = 0;
virtual const ColumnsWithTypeAndName & getResultColumns() const = 0;
/// Remove unused result and update required columns
virtual void finalize(const Names & required_output_) = 0;
/// Add projections to expression
virtual void prependProjectInput() const = 0;
virtual std::string dump() const = 0;
/// Only for ExpressionActionsStep
2020-09-11 12:24:41 +00:00
ActionsDAGPtr & actions();
const ActionsDAGPtr & actions() const;
ExpressionActionsPtr getExpression() const;
2020-08-19 19:33:49 +00:00
};
struct ExpressionActionsStep : public Step
{
2020-09-11 12:24:41 +00:00
ActionsDAGPtr actions_dag;
2020-08-19 19:33:49 +00:00
ExpressionActionsPtr actions;
2020-09-11 12:24:41 +00:00
explicit ExpressionActionsStep(ActionsDAGPtr actions_, Names required_output_ = Names())
2020-08-19 19:33:49 +00:00
: Step(std::move(required_output_))
2020-09-11 12:24:41 +00:00
, actions_dag(std::move(actions_))
2020-08-19 19:33:49 +00:00
{
}
const NamesAndTypesList & getRequiredColumns() const override
2020-08-12 08:55:16 +00:00
{
2020-08-19 19:33:49 +00:00
return actions->getRequiredColumnsWithTypes();
2020-08-12 08:55:16 +00:00
}
2020-08-19 19:33:49 +00:00
const ColumnsWithTypeAndName & getResultColumns() const override
{
return actions->getSampleBlock().getColumnsWithTypeAndName();
}
2020-08-12 08:55:16 +00:00
2020-08-19 19:33:49 +00:00
void finalize(const Names & required_output_) override
{
actions->finalize(required_output_);
}
void prependProjectInput() const override
{
actions->prependProjectInput();
}
std::string dump() const override
{
return actions->dumpActions();
}
};
struct ArrayJoinStep : public Step
{
ArrayJoinActionPtr array_join;
NamesAndTypesList required_columns;
ColumnsWithTypeAndName result_columns;
2020-09-08 10:40:53 +00:00
ArrayJoinStep(ArrayJoinActionPtr array_join_, ColumnsWithTypeAndName required_columns_);
2020-08-12 08:55:16 +00:00
2020-08-19 19:33:49 +00:00
const NamesAndTypesList & getRequiredColumns() const override { return required_columns; }
const ColumnsWithTypeAndName & getResultColumns() const override { return result_columns; }
void finalize(const Names & required_output_) override;
void prependProjectInput() const override {} /// TODO: remove unused columns before ARRAY JOIN ?
std::string dump() const override { return "ARRAY JOIN"; }
};
2020-09-08 10:40:53 +00:00
struct JoinStep : public Step
{
std::shared_ptr<TableJoin> analyzed_join;
JoinPtr join;
NamesAndTypesList required_columns;
ColumnsWithTypeAndName result_columns;
JoinStep(std::shared_ptr<TableJoin> analyzed_join_, JoinPtr join_, ColumnsWithTypeAndName required_columns_);
const NamesAndTypesList & getRequiredColumns() const override { return required_columns; }
const ColumnsWithTypeAndName & getResultColumns() const override { return result_columns; }
void finalize(const Names & required_output_) override;
void prependProjectInput() const override {} /// TODO: remove unused columns before JOIN ?
std::string dump() const override { return "JOIN"; }
};
2020-08-19 19:33:49 +00:00
using StepPtr = std::unique_ptr<Step>;
using Steps = std::vector<StepPtr>;
const Context & context;
Steps steps;
void addStep();
void finalize();
void clear()
{
steps.clear();
}
2020-06-25 17:40:25 +00:00
ExpressionActionsPtr getLastActions(bool allow_empty = false)
{
2020-06-25 18:44:05 +00:00
if (steps.empty())
{
if (allow_empty)
return {};
throw Exception("Empty ExpressionActionsChain", ErrorCodes::LOGICAL_ERROR);
2020-06-25 18:44:05 +00:00
}
2020-09-13 13:51:31 +00:00
auto * step = typeid_cast<ExpressionActionsStep *>(steps.back().get());
2020-09-11 12:24:41 +00:00
step->actions = step->actions_dag->buildExpressions(context);
return step->actions;
}
Step & getLastStep()
{
if (steps.empty())
throw Exception("Empty ExpressionActionsChain", ErrorCodes::LOGICAL_ERROR);
2020-08-19 19:58:23 +00:00
return *steps.back();
}
Step & lastStep(const NamesAndTypesList & columns)
{
if (steps.empty())
2020-09-11 12:24:41 +00:00
steps.emplace_back(std::make_unique<ExpressionActionsStep>(std::make_shared<ActionsDAG>(columns)));
2020-08-19 19:58:23 +00:00
return *steps.back();
}
2020-07-26 14:21:57 +00:00
std::string dumpChain() const;
};
}