1#pragma once
2
3#include <unordered_map>
4
5#include <Core/Types.h>
6
7
8namespace DB
9{
10
11class ReadBuffer;
12class WriteBuffer;
13
14/** More information about the block.
15 */
16struct BlockInfo
17{
18 /** is_overflows:
19 * After running GROUP BY ... WITH TOTALS with the max_rows_to_group_by and group_by_overflow_mode = 'any' settings,
20 * a row is inserted in the separate block with aggregated values that have not passed max_rows_to_group_by.
21 * If it is such a block, then is_overflows is set to true for it.
22 */
23
24 /** bucket_num:
25 * When using the two-level aggregation method, data with different key groups are scattered across different buckets.
26 * In this case, the bucket number is indicated here. It is used to optimize the merge for distributed aggregation.
27 * Otherwise -1.
28 */
29
30#define APPLY_FOR_BLOCK_INFO_FIELDS(M) \
31 M(bool, is_overflows, false, 1) \
32 M(Int32, bucket_num, -1, 2)
33
34#define DECLARE_FIELD(TYPE, NAME, DEFAULT, FIELD_NUM) \
35 TYPE NAME = DEFAULT;
36
37 APPLY_FOR_BLOCK_INFO_FIELDS(DECLARE_FIELD)
38
39#undef DECLARE_FIELD
40
41 /// Write the values in binary form. NOTE: You could use protobuf, but it would be overkill for this case.
42 void write(WriteBuffer & out) const;
43
44 /// Read the values in binary form.
45 void read(ReadBuffer & in);
46};
47
48/// Block extention to support delayed defaults. AddingDefaultsBlockInputStream uses it to replace missing values with column defaults.
49class BlockMissingValues
50{
51public:
52 using RowsBitMask = std::vector<bool>; /// a bit per row for a column
53
54 const RowsBitMask & getDefaultsBitmask(size_t column_idx) const;
55 void setBit(size_t column_idx, size_t row_idx);
56 bool empty() const { return rows_mask_by_column_id.empty(); }
57 size_t size() const { return rows_mask_by_column_id.size(); }
58 void clear() { rows_mask_by_column_id.clear(); }
59
60private:
61 using RowsMaskByColumnId = std::unordered_map<size_t, RowsBitMask>;
62
63 /// If rows_mask_by_column_id[column_id][row_id] is true related value in Block should be replaced with column default.
64 /// It could contain less columns and rows then related block.
65 RowsMaskByColumnId rows_mask_by_column_id;
66};
67
68}
69