1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/row_operations/row_operations.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/enums/expression_type.hpp"
12#include "duckdb/common/types.hpp"
13
14namespace duckdb {
15
16class Allocator;
17struct AggregateObject;
18struct AggregateFilterData;
19class DataChunk;
20class RowLayout;
21class TupleDataLayout;
22class RowDataCollection;
23struct SelectionVector;
24class StringHeap;
25class Vector;
26struct UnifiedVectorFormat;
27
28struct RowOperationsState {
29 RowOperationsState(Allocator &allocator) : allocator(allocator) {
30 }
31
32 Allocator &allocator;
33};
34
35// RowOperations contains a set of operations that operate on data using a RowLayout
36struct RowOperations {
37 //===--------------------------------------------------------------------===//
38 // Aggregation Operators
39 //===--------------------------------------------------------------------===//
40 //! initialize - unaligned addresses
41 static void InitializeStates(TupleDataLayout &layout, Vector &addresses, const SelectionVector &sel, idx_t count);
42 //! destructor - unaligned addresses, updated
43 static void DestroyStates(RowOperationsState &state, TupleDataLayout &layout, Vector &addresses, idx_t count);
44 //! update - aligned addresses
45 static void UpdateStates(RowOperationsState &state, AggregateObject &aggr, Vector &addresses, DataChunk &payload,
46 idx_t arg_idx, idx_t count);
47 //! filtered update - aligned addresses
48 static void UpdateFilteredStates(RowOperationsState &state, AggregateFilterData &filter_data, AggregateObject &aggr,
49 Vector &addresses, DataChunk &payload, idx_t arg_idx);
50 //! combine - unaligned addresses, updated
51 static void CombineStates(RowOperationsState &state, TupleDataLayout &layout, Vector &sources, Vector &targets,
52 idx_t count);
53 //! finalize - unaligned addresses, updated
54 static void FinalizeStates(RowOperationsState &state, TupleDataLayout &layout, Vector &addresses, DataChunk &result,
55 idx_t aggr_idx);
56
57 //===--------------------------------------------------------------------===//
58 // Read/Write Operators
59 //===--------------------------------------------------------------------===//
60 //! Scatter group data to the rows. Initialises the ValidityMask.
61 static void Scatter(DataChunk &columns, UnifiedVectorFormat col_data[], const RowLayout &layout, Vector &rows,
62 RowDataCollection &string_heap, const SelectionVector &sel, idx_t count);
63 //! Gather a single column.
64 //! If heap_ptr is not null, then the data is assumed to contain swizzled pointers,
65 //! which will be unswizzled in memory.
66 static void Gather(Vector &rows, const SelectionVector &row_sel, Vector &col, const SelectionVector &col_sel,
67 const idx_t count, const RowLayout &layout, const idx_t col_no, const idx_t build_size = 0,
68 data_ptr_t heap_ptr = nullptr);
69 //! Full Scan an entire columns
70 static void FullScanColumn(const TupleDataLayout &layout, Vector &rows, Vector &col, idx_t count, idx_t col_idx);
71
72 //===--------------------------------------------------------------------===//
73 // Comparison Operators
74 //===--------------------------------------------------------------------===//
75 //! Compare a block of key data against the row values to produce an updated selection that matches
76 //! and a second (optional) selection of non-matching values.
77 //! Returns the number of matches remaining in the selection.
78 using Predicates = vector<ExpressionType>;
79
80 static idx_t Match(DataChunk &columns, UnifiedVectorFormat col_data[], const TupleDataLayout &layout, Vector &rows,
81 const Predicates &predicates, SelectionVector &sel, idx_t count, SelectionVector *no_match,
82 idx_t &no_match_count);
83
84 //===--------------------------------------------------------------------===//
85 // Heap Operators
86 //===--------------------------------------------------------------------===//
87 //! Compute the entry sizes of a vector with variable size type (used before building heap buffer space).
88 static void ComputeEntrySizes(Vector &v, idx_t entry_sizes[], idx_t vcount, idx_t ser_count,
89 const SelectionVector &sel, idx_t offset = 0);
90 //! Compute the entry sizes of vector data with variable size type (used before building heap buffer space).
91 static void ComputeEntrySizes(Vector &v, UnifiedVectorFormat &vdata, idx_t entry_sizes[], idx_t vcount,
92 idx_t ser_count, const SelectionVector &sel, idx_t offset = 0);
93 //! Scatter vector with variable size type to the heap.
94 static void HeapScatter(Vector &v, idx_t vcount, const SelectionVector &sel, idx_t ser_count, idx_t col_idx,
95 data_ptr_t *key_locations, data_ptr_t *validitymask_locations, idx_t offset = 0);
96 //! Scatter vector data with variable size type to the heap.
97 static void HeapScatterVData(UnifiedVectorFormat &vdata, PhysicalType type, const SelectionVector &sel,
98 idx_t ser_count, idx_t col_idx, data_ptr_t *key_locations,
99 data_ptr_t *validitymask_locations, idx_t offset = 0);
100 //! Gather a single column with variable size type from the heap.
101 static void HeapGather(Vector &v, const idx_t &vcount, const SelectionVector &sel, const idx_t &col_idx,
102 data_ptr_t key_locations[], data_ptr_t validitymask_locations[]);
103
104 //===--------------------------------------------------------------------===//
105 // Sorting Operators
106 //===--------------------------------------------------------------------===//
107 //! Scatter vector data to the rows in radix-sortable format.
108 static void RadixScatter(Vector &v, idx_t vcount, const SelectionVector &sel, idx_t ser_count,
109 data_ptr_t key_locations[], bool desc, bool has_null, bool nulls_first, idx_t prefix_len,
110 idx_t width, idx_t offset = 0);
111
112 //===--------------------------------------------------------------------===//
113 // Out-of-Core Operators
114 //===--------------------------------------------------------------------===//
115 //! Swizzles blob pointers to offset within heap row
116 static void SwizzleColumns(const RowLayout &layout, const data_ptr_t base_row_ptr, const idx_t count);
117 //! Swizzles the base pointer of each row to offset within heap block
118 static void SwizzleHeapPointer(const RowLayout &layout, data_ptr_t row_ptr, const data_ptr_t heap_base_ptr,
119 const idx_t count, const idx_t base_offset = 0);
120 //! Copies 'count' heap rows that are pointed to by the rows at 'row_ptr' to 'heap_ptr' and swizzles the pointers
121 static void CopyHeapAndSwizzle(const RowLayout &layout, data_ptr_t row_ptr, const data_ptr_t heap_base_ptr,
122 data_ptr_t heap_ptr, const idx_t count);
123
124 //! Unswizzles the base offset within heap block the rows to pointers
125 static void UnswizzleHeapPointer(const RowLayout &layout, const data_ptr_t base_row_ptr,
126 const data_ptr_t base_heap_ptr, const idx_t count);
127 //! Unswizzles all offsets back to pointers
128 static void UnswizzlePointers(const RowLayout &layout, const data_ptr_t base_row_ptr,
129 const data_ptr_t base_heap_ptr, const idx_t count);
130};
131
132} // namespace duckdb
133