1#pragma once
2
3#include <Core/QueryProcessingStage.h>
4#include <Storages/SelectQueryInfo.h>
5#include <Storages/MergeTree/MergeTreeData.h>
6#include <Storages/MergeTree/RangesInDataPart.h>
7
8
9namespace DB
10{
11
12class KeyCondition;
13
14
15/** Executes SELECT queries on data from the merge tree.
16 */
17class MergeTreeDataSelectExecutor
18{
19public:
20 explicit MergeTreeDataSelectExecutor(const MergeTreeData & data_);
21
22 /** When reading, selects a set of parts that covers the desired range of the index.
23 * max_blocks_number_to_read - if not nullptr, do not read all the parts whose right border is greater than max_block in partition.
24 */
25 using PartitionIdToMaxBlock = std::unordered_map<String, Int64>;
26
27 Pipes read(
28 const Names & column_names,
29 const SelectQueryInfo & query_info,
30 const Context & context,
31 UInt64 max_block_size,
32 unsigned num_streams,
33 const PartitionIdToMaxBlock * max_block_numbers_to_read = nullptr) const;
34
35 Pipes readFromParts(
36 MergeTreeData::DataPartsVector parts,
37 const Names & column_names,
38 const SelectQueryInfo & query_info,
39 const Context & context,
40 UInt64 max_block_size,
41 unsigned num_streams,
42 const PartitionIdToMaxBlock * max_block_numbers_to_read = nullptr) const;
43
44private:
45 const MergeTreeData & data;
46
47 Logger * log;
48
49 Pipes spreadMarkRangesAmongStreams(
50 RangesInDataParts && parts,
51 size_t num_streams,
52 const Names & column_names,
53 UInt64 max_block_size,
54 bool use_uncompressed_cache,
55 const SelectQueryInfo & query_info,
56 const Names & virt_columns,
57 const Settings & settings) const;
58
59 Pipes spreadMarkRangesAmongStreamsWithOrder(
60 RangesInDataParts && parts,
61 size_t num_streams,
62 const Names & column_names,
63 UInt64 max_block_size,
64 bool use_uncompressed_cache,
65 const SelectQueryInfo & query_info,
66 const ExpressionActionsPtr & sorting_key_prefix_expr,
67 const Names & virt_columns,
68 const Settings & settings) const;
69
70 Pipes spreadMarkRangesAmongStreamsFinal(
71 RangesInDataParts && parts,
72 const Names & column_names,
73 UInt64 max_block_size,
74 bool use_uncompressed_cache,
75 const SelectQueryInfo & query_info,
76 const Names & virt_columns,
77 const Settings & settings) const;
78
79 /// Get the approximate value (bottom estimate - only by full marks) of the number of rows falling under the index.
80 size_t getApproximateTotalRowsToRead(
81 const MergeTreeData::DataPartsVector & parts,
82 const KeyCondition & key_condition,
83 const Settings & settings) const;
84
85 /// Create the expression "Sign == 1".
86 void createPositiveSignCondition(
87 ExpressionActionsPtr & out_expression,
88 String & out_column,
89 const Context & context) const;
90
91 MarkRanges markRangesFromPKRange(
92 const MergeTreeData::DataPartPtr & part,
93 const KeyCondition & key_condition,
94 const Settings & settings) const;
95
96 MarkRanges filterMarksUsingIndex(
97 MergeTreeIndexPtr index,
98 MergeTreeIndexConditionPtr condition,
99 MergeTreeData::DataPartPtr part,
100 const MarkRanges & ranges,
101 const Settings & settings) const;
102};
103
104}
105