diff --git a/dbms/src/DataStreams/FilterColumnsBlockInputStream.h b/dbms/src/DataStreams/FilterColumnsBlockInputStream.h new file mode 100644 index 00000000000..784a9180289 --- /dev/null +++ b/dbms/src/DataStreams/FilterColumnsBlockInputStream.h @@ -0,0 +1,58 @@ +#pragma once + +#include +#include +#include + +namespace DB +{ + +/// Removes columns other than columns_to_save_ from block +class FilterColumnsBlockInputStream : public IProfilingBlockInputStream +{ +public: + FilterColumnsBlockInputStream( + BlockInputStreamPtr input_, + const Names & columns_to_save_) + : columns_to_save(columns_to_save_) + { + children.push_back(input_); + } + + String getName() const override { + return "FilterColumnsBlockInputStream"; + } + + String getID() const override + { + std::stringstream res; + res << "FilterColumnsBlockInputStream(" << children.back()->getID(); + + for (const auto & it : columns_to_save) + res << ", " << it; + + res << ")"; + return res.str(); + } + +protected: + Block readImpl() override + { + Block block = children.back()->read(); + + if (!block) + return block; + Block filtered; + + for (const auto & it : columns_to_save) + filtered.insert(std::move(block.getByName(it))); + + + return filtered; + } + +private: + Names columns_to_save; +}; + +} diff --git a/dbms/src/Storages/StorageMerge.cpp b/dbms/src/Storages/StorageMerge.cpp index 8b2f0927388..57a532489d2 100644 --- a/dbms/src/Storages/StorageMerge.cpp +++ b/dbms/src/Storages/StorageMerge.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace DB @@ -225,7 +226,13 @@ BlockInputStreams StorageMerge::read( if (processed_stage_in_source_tables) processed_stage = processed_stage_in_source_tables.value(); - return narrowBlockInputStreams(res, num_streams); + res = narrowBlockInputStreams(res, num_streams); + + /// Added to avoid different block structure from different sources + for (auto & stream : res) + stream = std::make_shared(stream, column_names); + + return res; } /// Construct a block consisting only of possible values of virtual columns