2020-11-16 14:57:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-12-29 15:49:51 +00:00
|
|
|
#include <utility>
|
2020-11-16 14:57:56 +00:00
|
|
|
#include <Core/ColumnsWithTypeAndName.h>
|
|
|
|
#include <Core/NamesAndTypes.h>
|
|
|
|
#include <Core/Names.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2022-09-28 13:29:29 +00:00
|
|
|
#include "config.h"
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ActionsDAG;
|
|
|
|
using ActionsDAGPtr = std::shared_ptr<ActionsDAG>;
|
|
|
|
|
|
|
|
class IExecutableFunction;
|
|
|
|
using ExecutableFunctionPtr = std::shared_ptr<IExecutableFunction>;
|
|
|
|
|
|
|
|
class IFunctionBase;
|
2022-11-30 18:48:09 +00:00
|
|
|
using FunctionBasePtr = std::shared_ptr<const IFunctionBase>;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
class IFunctionOverloadResolver;
|
|
|
|
using FunctionOverloadResolverPtr = std::shared_ptr<IFunctionOverloadResolver>;
|
|
|
|
|
2023-02-01 13:33:32 +00:00
|
|
|
class FunctionNode;
|
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
class IDataType;
|
|
|
|
using DataTypePtr = std::shared_ptr<const IDataType>;
|
|
|
|
|
2021-04-14 14:07:56 +00:00
|
|
|
namespace JSONBuilder
|
|
|
|
{
|
|
|
|
class JSONMap;
|
|
|
|
|
|
|
|
class IItem;
|
|
|
|
using ItemPtr = std::unique_ptr<IItem>;
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:30:25 +00:00
|
|
|
class SortDescription;
|
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
/// Directed acyclic graph of expressions.
|
|
|
|
/// This is an intermediate representation of actions which is usually built from expression list AST.
|
|
|
|
/// Node of DAG describe calculation of a single column with known type, name, and constant value (if applicable).
|
|
|
|
///
|
|
|
|
/// DAG representation is useful in case we need to know explicit dependencies between actions.
|
|
|
|
/// It is helpful when it is needed to optimize actions, remove unused expressions, compile subexpressions,
|
|
|
|
/// split or merge parts of graph, calculate expressions on partial input.
|
|
|
|
///
|
|
|
|
/// Built DAG is used by ExpressionActions, which calculates expressions on block.
|
|
|
|
class ActionsDAG
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum class ActionType
|
|
|
|
{
|
|
|
|
/// 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,
|
|
|
|
FUNCTION,
|
|
|
|
};
|
|
|
|
|
2021-03-02 17:08:59 +00:00
|
|
|
struct Node;
|
|
|
|
using NodeRawPtrs = std::vector<Node *>;
|
|
|
|
using NodeRawConstPtrs = std::vector<const Node *>;
|
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
struct Node
|
|
|
|
{
|
2021-03-02 17:08:59 +00:00
|
|
|
NodeRawConstPtrs children;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2021-05-08 15:20:40 +00:00
|
|
|
ActionType type{};
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
std::string result_name;
|
|
|
|
DataTypePtr result_type;
|
|
|
|
|
2022-08-04 19:30:25 +00:00
|
|
|
/// Can be used to get function signature or properties like monotonicity.
|
2020-11-16 14:57:56 +00:00
|
|
|
FunctionBasePtr function_base;
|
|
|
|
/// Prepared function which is used in function execution.
|
|
|
|
ExecutableFunctionPtr function;
|
|
|
|
/// If function is a compiled statement.
|
|
|
|
bool is_function_compiled = false;
|
2021-07-12 06:36:46 +00:00
|
|
|
/// It is deterministic (See IFunction::isDeterministic).
|
|
|
|
/// This property is kept after constant folding of non-deterministic functions like 'now', 'today'.
|
|
|
|
bool is_deterministic = true;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
/// For COLUMN node and propagated constants.
|
|
|
|
ColumnPtr column;
|
2021-04-09 16:18:45 +00:00
|
|
|
|
2021-04-14 14:07:56 +00:00
|
|
|
void toTree(JSONBuilder::JSONMap & map) const;
|
2020-11-16 14:57:56 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 16:29:56 +00:00
|
|
|
/// NOTE: std::list is an implementation detail.
|
|
|
|
/// It allows to add and remove new nodes inplace without reallocation.
|
|
|
|
/// Raw pointers to nodes remain valid.
|
2020-11-16 14:57:56 +00:00
|
|
|
using Nodes = std::list<Node>;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Nodes nodes;
|
2021-03-02 17:08:59 +00:00
|
|
|
NodeRawConstPtrs inputs;
|
2022-08-08 15:54:51 +00:00
|
|
|
NodeRawConstPtrs outputs;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2021-03-02 17:08:59 +00:00
|
|
|
bool project_input = false;
|
|
|
|
bool projected_output = false;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ActionsDAG() = default;
|
2020-11-26 16:16:44 +00:00
|
|
|
ActionsDAG(ActionsDAG &&) = default;
|
2020-11-16 14:57:56 +00:00
|
|
|
ActionsDAG(const ActionsDAG &) = delete;
|
|
|
|
ActionsDAG & operator=(const ActionsDAG &) = delete;
|
2020-11-17 12:39:41 +00:00
|
|
|
explicit ActionsDAG(const NamesAndTypesList & inputs_);
|
|
|
|
explicit ActionsDAG(const ColumnsWithTypeAndName & inputs_);
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
const Nodes & getNodes() const { return nodes; }
|
2023-12-28 17:34:28 +00:00
|
|
|
static Nodes detachNodes(ActionsDAG && dag) { return std::move(dag.nodes); }
|
2022-08-08 15:54:51 +00:00
|
|
|
const NodeRawConstPtrs & getOutputs() const { return outputs; }
|
|
|
|
/** Output nodes can contain any column returned from DAG.
|
|
|
|
* You may manually change it if needed.
|
|
|
|
*/
|
|
|
|
NodeRawConstPtrs & getOutputs() { return outputs; }
|
|
|
|
|
2021-03-02 17:08:59 +00:00
|
|
|
const NodeRawConstPtrs & getInputs() const { return inputs; }
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
NamesAndTypesList getRequiredColumns() const;
|
2021-05-19 08:43:16 +00:00
|
|
|
Names getRequiredColumnsNames() const;
|
2020-11-16 14:57:56 +00:00
|
|
|
ColumnsWithTypeAndName getResultColumns() const;
|
|
|
|
NamesAndTypesList getNamesAndTypesList() const;
|
|
|
|
|
|
|
|
Names getNames() const;
|
|
|
|
std::string dumpNames() const;
|
|
|
|
std::string dumpDAG() const;
|
|
|
|
|
2021-03-02 17:08:59 +00:00
|
|
|
const Node & addInput(std::string name, DataTypePtr type);
|
|
|
|
const Node & addInput(ColumnWithTypeAndName column);
|
2021-03-18 11:37:45 +00:00
|
|
|
const Node & addColumn(ColumnWithTypeAndName column);
|
2021-03-02 17:08:59 +00:00
|
|
|
const Node & addAlias(const Node & child, std::string alias);
|
|
|
|
const Node & addArrayJoin(const Node & child, std::string result_name);
|
2020-11-16 14:57:56 +00:00
|
|
|
const Node & addFunction(
|
|
|
|
const FunctionOverloadResolverPtr & function,
|
2021-03-02 17:08:59 +00:00
|
|
|
NodeRawConstPtrs children,
|
|
|
|
std::string result_name);
|
2023-02-01 13:33:32 +00:00
|
|
|
const Node & addFunction(
|
|
|
|
const FunctionNode & function,
|
|
|
|
NodeRawConstPtrs children,
|
|
|
|
std::string result_name);
|
2022-11-28 15:02:59 +00:00
|
|
|
const Node & addFunction(
|
|
|
|
const FunctionBasePtr & function_base,
|
|
|
|
NodeRawConstPtrs children,
|
|
|
|
std::string result_name);
|
2023-01-31 13:54:04 +00:00
|
|
|
const Node & addCast(const Node & node_to_cast, const DataTypePtr & cast_type, std::string result_name);
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2022-08-08 15:54:51 +00:00
|
|
|
/// Find first column by name in output nodes. This search is linear.
|
|
|
|
const Node & findInOutputs(const std::string & name) const;
|
|
|
|
|
2021-03-11 17:03:39 +00:00
|
|
|
/// Same, but return nullptr if node not found.
|
2022-08-08 15:54:51 +00:00
|
|
|
const Node * tryFindInOutputs(const std::string & name) const;
|
|
|
|
|
2023-11-02 11:19:37 +00:00
|
|
|
/// Same, but for the list of names.
|
|
|
|
NodeRawConstPtrs findInOutpus(const Names & names) const;
|
|
|
|
|
2022-08-08 15:54:51 +00:00
|
|
|
/// Find first node with the same name in output nodes and replace it.
|
|
|
|
/// If was not found, add node to outputs end.
|
|
|
|
void addOrReplaceInOutputs(const Node & node);
|
2021-03-03 20:01:07 +00:00
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
/// Call addAlias several times.
|
|
|
|
void addAliases(const NamesWithAliases & aliases);
|
2022-08-08 15:54:51 +00:00
|
|
|
|
|
|
|
/// Add alias actions and remove unused columns from outputs. Also specify result columns order in outputs.
|
2020-11-16 14:57:56 +00:00
|
|
|
void project(const NamesWithAliases & projection);
|
|
|
|
|
2022-08-08 15:54:51 +00:00
|
|
|
/// If column is not in outputs, try to find it in nodes and insert back into outputs.
|
2020-11-16 14:57:56 +00:00
|
|
|
bool tryRestoreColumn(const std::string & column_name);
|
2022-08-08 15:54:51 +00:00
|
|
|
|
|
|
|
/// Find column in result. Remove it from outputs.
|
2021-02-04 11:44:00 +00:00
|
|
|
/// If columns is in inputs and has no dependent nodes, remove it from inputs too.
|
|
|
|
/// Return true if column was removed from inputs.
|
|
|
|
bool removeUnusedResult(const std::string & column_name);
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2021-03-05 15:21:14 +00:00
|
|
|
void projectInput(bool project = true) { project_input = project; }
|
2021-03-18 11:33:07 +00:00
|
|
|
bool isInputProjected() const { return project_input; }
|
|
|
|
bool isOutputProjected() const { return projected_output; }
|
2021-03-05 15:21:14 +00:00
|
|
|
|
2022-08-08 15:54:51 +00:00
|
|
|
/// Remove actions that are not needed to compute output nodes
|
|
|
|
void removeUnusedActions(bool allow_remove_inputs = true, bool allow_constant_folding = true);
|
|
|
|
|
2023-11-06 16:11:27 +00:00
|
|
|
/// Remove actions that are not needed to compute output nodes. Keep inputs from used_inputs.
|
|
|
|
void removeUnusedActions(const std::unordered_set<const Node *> & used_inputs, bool allow_constant_folding = true);
|
|
|
|
|
2022-08-08 15:54:51 +00:00
|
|
|
/// Remove actions that are not needed to compute output nodes with required names
|
2021-10-26 19:56:12 +00:00
|
|
|
void removeUnusedActions(const Names & required_names, bool allow_remove_inputs = true, bool allow_constant_folding = true);
|
2022-08-08 15:54:51 +00:00
|
|
|
|
|
|
|
/// Remove actions that are not needed to compute output nodes with required names
|
2021-10-26 19:56:12 +00:00
|
|
|
void removeUnusedActions(const NameSet & required_names, bool allow_remove_inputs = true, bool allow_constant_folding = true);
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2022-02-06 08:45:49 +00:00
|
|
|
/// Transform the current DAG in a way that leaf nodes get folded into their parents. It's done
|
|
|
|
/// because each projection can provide some columns as inputs to substitute certain sub-DAGs
|
|
|
|
/// (expressions). Consider the following example:
|
|
|
|
/// CREATE TABLE tbl (dt DateTime, val UInt64,
|
2022-09-19 14:03:22 +00:00
|
|
|
/// PROJECTION p_hour (SELECT sum(val) GROUP BY toStartOfHour(dt)));
|
2022-02-06 08:45:49 +00:00
|
|
|
///
|
2022-09-19 14:03:22 +00:00
|
|
|
/// Query: SELECT toStartOfHour(dt), sum(val) FROM tbl GROUP BY toStartOfHour(dt);
|
2022-02-06 08:45:49 +00:00
|
|
|
///
|
|
|
|
/// We will have an ActionsDAG like this:
|
2022-09-19 14:03:22 +00:00
|
|
|
/// FUNCTION: toStartOfHour(dt) sum(val)
|
2022-02-06 08:45:49 +00:00
|
|
|
/// ^ ^
|
|
|
|
/// | |
|
|
|
|
/// INPUT: dt val
|
|
|
|
///
|
|
|
|
/// Now we traverse the DAG and see if any FUNCTION node can be replaced by projection's INPUT node.
|
|
|
|
/// The result DAG will be:
|
2022-09-19 14:03:22 +00:00
|
|
|
/// INPUT: toStartOfHour(dt) sum(val)
|
2022-02-06 08:45:49 +00:00
|
|
|
///
|
|
|
|
/// We don't need aggregate columns from projection because they are matched after DAG.
|
|
|
|
/// Currently we use canonical names of each node to find matches. It can be improved after we
|
|
|
|
/// have a full-featured name binding system.
|
|
|
|
///
|
|
|
|
/// @param required_columns should contain columns which this DAG is required to produce after folding. It used for result actions.
|
|
|
|
/// @param projection_block_for_keys contains all key columns of given projection.
|
|
|
|
/// @param predicate_column_name means we need to produce the predicate column after folding.
|
|
|
|
/// @param add_missing_keys means whether to add additional missing columns to input nodes from projection key columns directly.
|
|
|
|
/// @return required columns for this folded DAG. It's expected to be fewer than the original ones if some projection is used.
|
2021-05-03 16:41:48 +00:00
|
|
|
NameSet foldActionsByProjection(
|
2021-05-04 12:40:34 +00:00
|
|
|
const NameSet & required_columns,
|
|
|
|
const Block & projection_block_for_keys,
|
|
|
|
const String & predicate_column_name = {},
|
|
|
|
bool add_missing_keys = true);
|
2022-02-06 08:45:49 +00:00
|
|
|
|
2023-04-03 18:18:39 +00:00
|
|
|
/// Get an ActionsDAG in a following way:
|
|
|
|
/// * Traverse a tree starting from required_outputs
|
|
|
|
/// * If there is a node from new_inputs keys, replace it to INPUT
|
|
|
|
/// * INPUT name should be taken from new_inputs mapped node name
|
|
|
|
/// * Mapped nodes may be the same nodes, and in this case there would be a single INPUT
|
2023-02-21 19:50:17 +00:00
|
|
|
/// Here want to substitute some expressions to columns from projection.
|
|
|
|
/// This function expects that all required_outputs can be calculated from nodes in new_inputs.
|
2023-02-21 20:01:59 +00:00
|
|
|
/// If not, exception will happen.
|
2023-02-21 19:50:17 +00:00
|
|
|
/// This function also expects that new_inputs and required_outputs are valid nodes from the same DAG.
|
|
|
|
/// Example:
|
|
|
|
/// DAG: new_inputs: Result DAG
|
|
|
|
/// a b c * d -> "(a + b) * d"
|
|
|
|
/// \ / e -> ""
|
|
|
|
/// a + b
|
|
|
|
/// \ required_outputs: => "(a + b) * d" e
|
|
|
|
/// c (alias) d c * d - e \ /
|
|
|
|
/// \ / c * d - e
|
|
|
|
/// c * d e
|
|
|
|
/// \ /
|
|
|
|
/// c * d - e
|
|
|
|
static ActionsDAGPtr foldActionsByProjection(
|
2023-04-03 16:44:31 +00:00
|
|
|
const std::unordered_map<const Node *, const Node *> & new_inputs,
|
2023-02-21 19:50:17 +00:00
|
|
|
const NodeRawConstPtrs & required_outputs);
|
2023-01-27 18:38:14 +00:00
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
bool hasArrayJoin() const;
|
2020-11-27 09:50:56 +00:00
|
|
|
bool hasStatefulFunctions() const;
|
2021-01-28 11:00:24 +00:00
|
|
|
bool trivial() const; /// If actions has no functions or array join.
|
2021-07-12 05:58:19 +00:00
|
|
|
void assertDeterministic() const; /// Throw if not isDeterministic.
|
2023-01-22 20:30:20 +00:00
|
|
|
bool hasNonDeterministic() const;
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2021-03-05 13:56:44 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
2021-08-10 11:31:15 +00:00
|
|
|
void compileExpressions(size_t min_count_to_compile_expression, const std::unordered_set<const Node *> & lazy_executed_nodes = {});
|
2021-03-05 13:56:44 +00:00
|
|
|
#endif
|
2020-11-16 14:57:56 +00:00
|
|
|
|
|
|
|
ActionsDAGPtr clone() const;
|
|
|
|
|
2023-11-02 11:19:37 +00:00
|
|
|
static ActionsDAGPtr cloneSubDAG(const NodeRawConstPtrs & outputs, bool remove_aliases);
|
|
|
|
|
2021-05-13 13:38:18 +00:00
|
|
|
/// Execute actions for header. Input block must have empty columns.
|
2021-07-28 07:02:10 +00:00
|
|
|
/// Result should be equal to the execution of ExpressionActions built from this DAG.
|
2021-05-13 13:38:18 +00:00
|
|
|
/// Actions are not changed, no expressions are compiled.
|
|
|
|
///
|
|
|
|
/// In addition, check that result constants are constants according to DAG.
|
|
|
|
/// In case if function return constant, but arguments are not constant, materialize it.
|
|
|
|
Block updateHeader(Block header) const;
|
|
|
|
|
2023-08-10 18:41:57 +00:00
|
|
|
using IntermediateExecutionResult = std::unordered_map<const Node *, ColumnWithTypeAndName>;
|
|
|
|
static ColumnsWithTypeAndName evaluatePartialResult(
|
|
|
|
IntermediateExecutionResult & node_to_column,
|
|
|
|
const NodeRawConstPtrs & outputs,
|
2024-02-07 23:44:21 +00:00
|
|
|
size_t input_rows_count,
|
2023-08-10 18:41:57 +00:00
|
|
|
bool throw_on_error);
|
|
|
|
|
2021-02-04 20:36:50 +00:00
|
|
|
/// For apply materialize() function for every output.
|
|
|
|
/// Also add aliases so the result names remain unchanged.
|
|
|
|
void addMaterializingOutputActions();
|
2020-11-17 14:51:05 +00:00
|
|
|
|
2021-03-18 11:33:07 +00:00
|
|
|
/// Apply materialize() function to node. Result node has the same name.
|
2021-03-09 16:54:06 +00:00
|
|
|
const Node & materializeNode(const Node & node);
|
|
|
|
|
2020-11-17 14:51:05 +00:00
|
|
|
enum class MatchColumnsMode
|
|
|
|
{
|
|
|
|
/// Require same number of columns in source and result. Match columns by corresponding positions, regardless to names.
|
|
|
|
Position,
|
|
|
|
/// Find columns in source by their names. Allow excessive columns in source.
|
|
|
|
Name,
|
|
|
|
};
|
|
|
|
|
2020-11-20 16:52:50 +00:00
|
|
|
/// Create ActionsDAG which converts block structure from source to result.
|
|
|
|
/// It is needed to convert result from different sources to the same structure, e.g. for UNION query.
|
|
|
|
/// Conversion should be possible with only usage of CAST function and renames.
|
2021-02-18 11:49:32 +00:00
|
|
|
/// @param ignore_constant_values - Do not check that constants are same. Use value from result_header.
|
|
|
|
/// @param add_casted_columns - Create new columns with converted values instead of replacing original.
|
2021-05-13 14:22:53 +00:00
|
|
|
/// @param new_names - Output parameter for new column names when add_casted_columns is used.
|
2020-11-17 14:51:05 +00:00
|
|
|
static ActionsDAGPtr makeConvertingActions(
|
|
|
|
const ColumnsWithTypeAndName & source,
|
|
|
|
const ColumnsWithTypeAndName & result,
|
|
|
|
MatchColumnsMode mode,
|
2021-02-18 11:49:32 +00:00
|
|
|
bool ignore_constant_values = false,
|
|
|
|
bool add_casted_columns = false,
|
|
|
|
NameToNameMap * new_names = nullptr);
|
2020-11-17 14:51:05 +00:00
|
|
|
|
2021-02-04 14:25:11 +00:00
|
|
|
/// Create expression which add const column and then materialize it.
|
|
|
|
static ActionsDAGPtr makeAddingColumnActions(ColumnWithTypeAndName column);
|
|
|
|
|
2021-01-19 10:03:25 +00:00
|
|
|
/// Create ActionsDAG which represents expression equivalent to applying first and second actions consequently.
|
2020-12-01 11:19:03 +00:00
|
|
|
/// Is used to replace `(first -> second)` expression chain to single `merge(first, second)` expression.
|
|
|
|
/// If first.settings.project_input is set, then outputs of `first` must include inputs of `second`.
|
|
|
|
/// Otherwise, any two actions may be combined.
|
|
|
|
static ActionsDAGPtr merge(ActionsDAG && first, ActionsDAG && second);
|
2020-11-26 16:16:44 +00:00
|
|
|
|
2022-08-19 17:56:02 +00:00
|
|
|
/// The result is similar to merge(*this, second);
|
|
|
|
/// Invariant : no nodes are removed from the first (this) DAG.
|
|
|
|
/// So that pointers to nodes are kept valid.
|
|
|
|
void mergeInplace(ActionsDAG && second);
|
|
|
|
|
2023-03-20 10:29:27 +00:00
|
|
|
/// Merge current nodes with specified dag nodes
|
|
|
|
void mergeNodes(ActionsDAG && second);
|
|
|
|
|
2024-01-05 16:31:10 +00:00
|
|
|
struct SplitResult
|
|
|
|
{
|
|
|
|
ActionsDAGPtr first;
|
|
|
|
ActionsDAGPtr second;
|
|
|
|
std::unordered_map<const Node *, const Node *> split_nodes_mapping;
|
|
|
|
};
|
2021-02-04 11:44:00 +00:00
|
|
|
|
2021-01-19 10:03:25 +00:00
|
|
|
/// Split ActionsDAG into two DAGs, where first part contains all nodes from split_nodes and their children.
|
|
|
|
/// Execution of first then second parts on block is equivalent to execution of initial DAG.
|
2024-03-19 12:37:21 +00:00
|
|
|
/// Inputs and outputs of original DAG are split between the first and the second DAGs.
|
2024-03-19 11:40:03 +00:00
|
|
|
/// Intermediate result can apper in first outputs and second inputs.
|
|
|
|
/// Example:
|
|
|
|
/// initial DAG : (a, b, c, d, e) -> (w, x, y, z) | 1 a 2 b 3 c 4 d 5 e 6 -> 1 2 3 4 5 6 w x y z
|
|
|
|
/// split (first) : (a, c, d) -> (i, j, k, w, y) | 1 a 2 b 3 c 4 d 5 e 6 -> 1 2 b 3 4 5 e 6 i j k w y
|
|
|
|
/// split (second) : (i, j, k, y, b, e) -> (x, y, z) | 1 2 b 3 4 5 e 6 i j k w y -> 1 2 3 4 5 6 w x y z
|
2024-01-05 16:31:10 +00:00
|
|
|
SplitResult split(std::unordered_set<const Node *> split_nodes, bool create_split_nodes_mapping = false) const;
|
2021-01-18 14:59:59 +00:00
|
|
|
|
2021-01-19 10:03:25 +00:00
|
|
|
/// Splits actions into two parts. Returned first half may be swapped with ARRAY JOIN.
|
2021-02-04 11:44:00 +00:00
|
|
|
SplitResult splitActionsBeforeArrayJoin(const NameSet & array_joined_columns) const;
|
2021-01-19 10:03:25 +00:00
|
|
|
|
|
|
|
/// Splits actions into two parts. First part has minimal size sufficient for calculation of column_name.
|
2022-08-08 15:54:51 +00:00
|
|
|
/// Outputs of initial actions must contain column_name.
|
2021-02-04 11:44:00 +00:00
|
|
|
SplitResult splitActionsForFilter(const std::string & column_name) const;
|
2021-01-19 10:03:25 +00:00
|
|
|
|
2022-03-25 16:20:29 +00:00
|
|
|
/// Splits actions into two parts. The first part contains all the calculations required to calculate sort_columns.
|
|
|
|
/// The second contains the rest.
|
|
|
|
SplitResult splitActionsBySortingDescription(const NameSet & sort_columns) const;
|
2022-03-25 15:18:24 +00:00
|
|
|
|
2021-02-10 16:26:49 +00:00
|
|
|
/// Create actions which may calculate part of filter using only available_inputs.
|
|
|
|
/// If nothing may be calculated, returns nullptr.
|
|
|
|
/// Otherwise, return actions which inputs are from available_inputs.
|
2021-04-08 09:54:39 +00:00
|
|
|
/// Returned actions add single column which may be used for filter. Added column will be the first one.
|
2021-02-10 16:26:49 +00:00
|
|
|
/// Also, replace some nodes of current inputs to constant 1 in case they are filtered.
|
2021-03-17 16:08:46 +00:00
|
|
|
///
|
2021-03-17 18:42:42 +00:00
|
|
|
/// @param all_inputs should contain inputs from previous step, which will be used for result actions.
|
2021-03-17 16:08:46 +00:00
|
|
|
/// It is expected that all_inputs contain columns from available_inputs.
|
|
|
|
/// This parameter is needed to enforce result actions save columns order in block.
|
2021-03-17 18:42:42 +00:00
|
|
|
/// Otherwise for some queries, e.g. with GROUP BY, columns will be mixed.
|
2021-03-17 16:08:46 +00:00
|
|
|
/// Example: SELECT sum(x), y, z FROM tab WHERE z > 0 and sum(x) > 0
|
|
|
|
/// Pushed condition: z > 0
|
|
|
|
/// GROUP BY step will transform columns `x, y, z` -> `sum(x), y, z`
|
|
|
|
/// If we just add filter step with actions `z -> z > 0` before GROUP BY,
|
2021-04-08 09:54:39 +00:00
|
|
|
/// columns will be transformed like `x, y, z` -> `z > 0, z, x, y` -(remove filter)-> `z, x, y`.
|
2021-03-17 16:08:46 +00:00
|
|
|
/// To avoid it, add inputs from `all_inputs` list,
|
2021-04-08 09:54:39 +00:00
|
|
|
/// so actions `x, y, z -> z > 0, x, y, z` -(remove filter)-> `x, y, z` will not change columns order.
|
2021-03-17 16:08:46 +00:00
|
|
|
ActionsDAGPtr cloneActionsForFilterPushDown(
|
|
|
|
const std::string & filter_name,
|
|
|
|
bool can_remove_filter,
|
|
|
|
const Names & available_inputs,
|
|
|
|
const ColumnsWithTypeAndName & all_inputs);
|
2021-02-10 16:26:49 +00:00
|
|
|
|
2022-08-12 23:42:37 +00:00
|
|
|
bool
|
|
|
|
isSortingPreserved(const Block & input_header, const SortDescription & sort_description, const String & ignore_output_column = "") const;
|
2022-08-08 12:42:10 +00:00
|
|
|
|
2022-11-17 18:44:26 +00:00
|
|
|
/** Build filter dag from multiple filter dags.
|
|
|
|
*
|
|
|
|
* If filter nodes are empty, result is nullptr.
|
|
|
|
*
|
|
|
|
* If filter nodes are not empty, nodes and their children are merged into single dag.
|
|
|
|
*
|
|
|
|
* Additionally during dag construction if node has name that exists in node_name_to_input_column map argument
|
|
|
|
* in final dag this node is represented as INPUT node with specified column.
|
|
|
|
*
|
2023-03-20 10:29:27 +00:00
|
|
|
* If single_output_condition_node = true, result dag has single output node:
|
2022-11-17 18:44:26 +00:00
|
|
|
* 1. If there is single filter node, result dag output will contain this node.
|
|
|
|
* 2. If there are multiple filter nodes, result dag output will contain single `and` function node
|
|
|
|
* and children of this node will be filter nodes.
|
2023-03-20 10:29:27 +00:00
|
|
|
*
|
|
|
|
* If single_output_condition_node = false, result dag has multiple output nodes.
|
2022-11-17 18:44:26 +00:00
|
|
|
*/
|
|
|
|
static ActionsDAGPtr buildFilterActionsDAG(
|
|
|
|
const NodeRawConstPtrs & filter_nodes,
|
2024-01-25 15:22:49 +00:00
|
|
|
const std::unordered_map<std::string, ColumnWithTypeAndName> & node_name_to_input_node_column = {},
|
2023-03-20 10:29:27 +00:00
|
|
|
bool single_output_condition_node = true);
|
2022-11-17 18:44:26 +00:00
|
|
|
|
2023-09-01 15:45:08 +00:00
|
|
|
/// Check if `predicate` is a combination of AND functions.
|
|
|
|
/// Returns a list of nodes representing atomic predicates.
|
|
|
|
static NodeRawConstPtrs extractConjunctionAtoms(const Node * predicate);
|
|
|
|
|
|
|
|
/// Get a list of nodes. For every node, check if it can be compused using allowed subset of inputs.
|
|
|
|
/// Returns only those nodes from the list which can be computed.
|
|
|
|
static NodeRawConstPtrs filterNodesByAllowedInputs(
|
|
|
|
NodeRawConstPtrs nodes,
|
|
|
|
const std::unordered_set<const Node *> & allowed_inputs);
|
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
private:
|
2022-11-24 17:26:09 +00:00
|
|
|
NodeRawConstPtrs getParents(const Node * target) const;
|
|
|
|
|
2021-03-02 17:08:59 +00:00
|
|
|
Node & addNode(Node node);
|
2020-11-16 14:57:56 +00:00
|
|
|
|
2022-12-29 15:49:51 +00:00
|
|
|
const Node & addFunctionImpl(
|
|
|
|
const FunctionBasePtr & function_base,
|
|
|
|
NodeRawConstPtrs children,
|
|
|
|
ColumnsWithTypeAndName arguments,
|
|
|
|
std::string result_name,
|
2023-02-01 13:33:32 +00:00
|
|
|
DataTypePtr result_type,
|
2022-12-29 15:49:51 +00:00
|
|
|
bool all_const);
|
|
|
|
|
2021-03-05 13:56:44 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
2021-08-10 11:31:15 +00:00
|
|
|
void compileFunctions(size_t min_count_to_compile_expression, const std::unordered_set<const Node *> & lazy_executed_nodes = {});
|
2021-03-05 13:56:44 +00:00
|
|
|
#endif
|
2021-02-20 16:13:36 +00:00
|
|
|
|
2021-03-11 17:03:39 +00:00
|
|
|
static ActionsDAGPtr cloneActionsForConjunction(NodeRawConstPtrs conjunction, const ColumnsWithTypeAndName & all_inputs);
|
2020-11-16 14:57:56 +00:00
|
|
|
};
|
|
|
|
|
2023-03-21 16:30:13 +00:00
|
|
|
class FindOriginalNodeForOutputName
|
|
|
|
{
|
|
|
|
using NameToNodeIndex = std::unordered_map<std::string_view, const ActionsDAG::Node *>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FindOriginalNodeForOutputName(const ActionsDAGPtr & actions);
|
2023-04-05 15:56:06 +00:00
|
|
|
const ActionsDAG::Node * find(const String & output_name);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActionsDAGPtr actions;
|
|
|
|
NameToNodeIndex index;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FindAliasForInputName
|
|
|
|
{
|
|
|
|
using NameToNodeIndex = std::unordered_map<std::string_view, const ActionsDAG::Node *>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FindAliasForInputName(const ActionsDAGPtr & actions);
|
|
|
|
const ActionsDAG::Node * find(const String & name);
|
2023-03-21 16:30:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActionsDAGPtr actions;
|
|
|
|
NameToNodeIndex index;
|
|
|
|
};
|
2023-03-07 22:16:22 +00:00
|
|
|
|
2021-06-22 10:28:56 +00:00
|
|
|
/// This is an ugly way to bypass impossibility to forward declare ActionDAG::Node.
|
|
|
|
struct ActionDAGNodes
|
|
|
|
{
|
|
|
|
ActionsDAG::NodeRawConstPtrs nodes;
|
|
|
|
};
|
|
|
|
|
2020-11-16 14:57:56 +00:00
|
|
|
}
|