diff --git a/src/Interpreters/ActionsDAG.cpp b/src/Interpreters/ActionsDAG.cpp index becd3f4f4a2..176745c707d 100644 --- a/src/Interpreters/ActionsDAG.cpp +++ b/src/Interpreters/ActionsDAG.cpp @@ -734,6 +734,23 @@ ActionsDAGPtr ActionsDAG::makeConvertingActions( return actions_dag; } +ActionsDAGPtr ActionsDAG::makeAddingColumnActions(ColumnWithTypeAndName column) +{ + auto adding_column_action = std::make_shared(); + FunctionOverloadResolverPtr func_builder_materialize = + std::make_shared( + std::make_unique( + std::make_shared())); + + auto column_name = column.name; + const auto & column_node = adding_column_action->addColumn(std::move(column)); + Inputs inputs = {const_cast(&column_node)}; + auto & function_node = adding_column_action->addFunction(func_builder_materialize, std::move(inputs), {}, true); + adding_column_action->addAlias(function_node, std::move(column_name), true); + + return adding_column_action; +} + ActionsDAGPtr ActionsDAG::merge(ActionsDAG && first, ActionsDAG && second) { /// first: x (1), x (2), y ==> x (2), z, x (3) diff --git a/src/Interpreters/ActionsDAG.h b/src/Interpreters/ActionsDAG.h index fa5ae2ac83f..e13a9bd62b3 100644 --- a/src/Interpreters/ActionsDAG.h +++ b/src/Interpreters/ActionsDAG.h @@ -250,6 +250,9 @@ public: MatchColumnsMode mode, bool ignore_constant_values = false); /// Do not check that constants are same. Use value from result_header. + /// Create expression which add const column and then materialize it. + static ActionsDAGPtr makeAddingColumnActions(ColumnWithTypeAndName column); + /// Create ActionsDAG which represents expression equivalent to applying first and second actions consequently. /// 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`. diff --git a/src/Processors/QueryPlan/AddingConstColumnStep.cpp b/src/Processors/QueryPlan/AddingConstColumnStep.cpp deleted file mode 100644 index 27c7720e58e..00000000000 --- a/src/Processors/QueryPlan/AddingConstColumnStep.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -#include - -namespace DB -{ - -static ITransformingStep::Traits getTraits() -{ - return ITransformingStep::Traits - { - { - .preserves_distinct_columns = true, - .returns_single_stream = false, - .preserves_number_of_streams = true, - .preserves_sorting = true, - }, - { - .preserves_number_of_rows = true, - } - }; -} - -AddingConstColumnStep::AddingConstColumnStep(const DataStream & input_stream_, ColumnWithTypeAndName column_) - : ITransformingStep(input_stream_, - AddingConstColumnTransform::transformHeader(input_stream_.header, column_), - getTraits()) - , column(std::move(column_)) -{ -} - -void AddingConstColumnStep::transformPipeline(QueryPipeline & pipeline) -{ - pipeline.addSimpleTransform([&](const Block & header) - { - return std::make_shared(header, column); - }); -} - -} diff --git a/src/Processors/QueryPlan/AddingConstColumnStep.h b/src/Processors/QueryPlan/AddingConstColumnStep.h deleted file mode 100644 index baa63873f21..00000000000 --- a/src/Processors/QueryPlan/AddingConstColumnStep.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include - -namespace DB -{ - -/// Adds a materialized const column with a specified value. -class AddingConstColumnStep : public ITransformingStep -{ -public: - AddingConstColumnStep(const DataStream & input_stream_, ColumnWithTypeAndName column_); - - String getName() const override { return "AddingConstColumn"; } - - void transformPipeline(QueryPipeline & pipeline) override; - -private: - ColumnWithTypeAndName column; -}; - -} - diff --git a/src/Processors/Transforms/AddingConstColumnTransform.h b/src/Processors/Transforms/AddingConstColumnTransform.h deleted file mode 100644 index 15e9addd924..00000000000 --- a/src/Processors/Transforms/AddingConstColumnTransform.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include - -namespace DB -{ - -namespace ErrorCodes -{ - extern const int LOGICAL_ERROR; -} - -/// Adds a materialized const column to the chunk with a specified value. -class AddingConstColumnTransform : public ISimpleTransform -{ -public: - AddingConstColumnTransform(const Block & header, ColumnWithTypeAndName column_) - : ISimpleTransform(header, transformHeader(header, column_), false) - , column(std::move(column_)) - { - if (!column.column || !isColumnConst(*column.column) || !column.column->empty()) - throw Exception("AddingConstColumnTransform expected empty const column", ErrorCodes::LOGICAL_ERROR); - } - - String getName() const override { return "AddingConstColumnTransform"; } - - static Block transformHeader(Block header, ColumnWithTypeAndName & column_) - { - header.insert(column_); - return header; - } - -protected: - void transform(Chunk & chunk) override - { - auto num_rows = chunk.getNumRows(); - chunk.addColumn(column.column->cloneResized(num_rows)->convertToFullColumnIfConst()); - } - -private: - ColumnWithTypeAndName column; -}; - -} diff --git a/src/Processors/ya.make b/src/Processors/ya.make index d42746791fb..2862873a920 100644 --- a/src/Processors/ya.make +++ b/src/Processors/ya.make @@ -92,7 +92,6 @@ SRCS( Pipe.cpp Port.cpp QueryPipeline.cpp - QueryPlan/AddingConstColumnStep.cpp QueryPlan/AddingDelayedSourceStep.cpp QueryPlan/AddingMissedStep.cpp QueryPlan/AggregatingStep.cpp diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 7b1baf10616..d41faa1ed46 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -28,12 +28,10 @@ #include #include #include -#include #include #include #include #include -#include #include #include @@ -841,7 +839,9 @@ QueryPlanPtr MergeTreeDataSelectExecutor::readFromParts( column.type = std::make_shared(); column.column = column.type->createColumnConst(0, Field(used_sample_factor)); - auto adding_column = std::make_unique(plan->getCurrentDataStream(), std::move(column)); + auto adding_column_action = ActionsDAG::makeAddingColumnActions(std::move(column)); + + auto adding_column = std::make_unique(plan->getCurrentDataStream(), std::move(adding_column_action)); adding_column->setStepDescription("Add _sample_factor column"); plan->addStep(std::move(adding_column)); } diff --git a/src/Storages/StorageMerge.cpp b/src/Storages/StorageMerge.cpp index 74df6dd185b..91ebfaa3a27 100644 --- a/src/Storages/StorageMerge.cpp +++ b/src/Storages/StorageMerge.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include @@ -364,9 +363,13 @@ Pipe StorageMerge::createSources( column.name = "_table"; column.type = std::make_shared(); column.column = column.type->createColumnConst(0, Field(table_name)); + + auto adding_column_dag = ActionsDAG::makeAddingColumnActions(std::move(column)); + auto adding_column_actions = std::make_shared(std::move(adding_column_dag)); + pipe.addSimpleTransform([&](const Block & stream_header) { - return std::make_shared(stream_header, column); + return std::make_shared(stream_header, adding_column_actions); }); }