1#pragma once
2
3#include <Storages/IStorage.h>
4#include <Storages/MergeTree/MergeTreeDataPart.h>
5#include <Storages/MergeTree/MergeTreeDataSelectExecutor.h>
6#include <Core/Defines.h>
7
8#include <ext/shared_ptr_helper.h>
9#include <Processors/Executors/TreeExecutorBlockInputStream.h>
10
11
12namespace DB
13{
14
15/// A Storage that allows reading from a single MergeTree data part.
16class StorageFromMergeTreeDataPart : public ext::shared_ptr_helper<StorageFromMergeTreeDataPart>, public IStorage
17{
18 friend struct ext::shared_ptr_helper<StorageFromMergeTreeDataPart>;
19public:
20 String getName() const override { return "FromMergeTreeDataPart"; }
21 String getTableName() const override { return part->storage.getTableName() + " (part " + part->name + ")"; }
22 String getDatabaseName() const override { return part->storage.getDatabaseName(); }
23
24 BlockInputStreams read(
25 const Names & column_names,
26 const SelectQueryInfo & query_info,
27 const Context & context,
28 QueryProcessingStage::Enum /*processed_stage*/,
29 size_t max_block_size,
30 unsigned num_streams) override
31 {
32 auto pipes = MergeTreeDataSelectExecutor(part->storage).readFromParts(
33 {part}, column_names, query_info, context, max_block_size, num_streams);
34
35 /// Wrap processors to BlockInputStreams. It is temporary. Will be changed to processors interface later.
36 BlockInputStreams streams;
37 streams.reserve(pipes.size());
38
39 for (auto & pipe : pipes)
40 streams.emplace_back(std::make_shared<TreeExecutorBlockInputStream>(std::move(pipe)));
41
42 return streams;
43 }
44
45 bool supportsIndexForIn() const override { return true; }
46
47 bool mayBenefitFromIndexForIn(const ASTPtr & left_in_operand, const Context & query_context) const override
48 {
49 return part->storage.mayBenefitFromIndexForIn(left_in_operand, query_context);
50 }
51
52protected:
53 StorageFromMergeTreeDataPart(const MergeTreeData::DataPartPtr & part_)
54 : IStorage(part_->storage.getVirtuals()), part(part_)
55 {
56 setColumns(part_->storage.getColumns());
57 setIndices(part_->storage.getIndices());
58 }
59
60private:
61 MergeTreeData::DataPartPtr part;
62};
63
64}
65