1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/storage/table/chunk_info.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/vector_size.hpp"
13#include "duckdb/common/atomic.hpp"
14
15namespace duckdb {
16class RowGroup;
17struct SelectionVector;
18class Transaction;
19struct TransactionData;
20
21enum class ChunkInfoType : uint8_t { CONSTANT_INFO, VECTOR_INFO, EMPTY_INFO };
22
23class ChunkInfo {
24public:
25 ChunkInfo(idx_t start, ChunkInfoType type) : start(start), type(type) {
26 }
27 virtual ~ChunkInfo() {
28 }
29
30 //! The row index of the first row
31 idx_t start;
32 //! The ChunkInfo type
33 ChunkInfoType type;
34
35public:
36 //! Gets up to max_count entries from the chunk info. If the ret is 0>ret>max_count, the selection vector is filled
37 //! with the tuples
38 virtual idx_t GetSelVector(TransactionData transaction, SelectionVector &sel_vector, idx_t max_count) = 0;
39 virtual idx_t GetCommittedSelVector(transaction_t min_start_id, transaction_t min_transaction_id,
40 SelectionVector &sel_vector, idx_t max_count) = 0;
41 //! Returns whether or not a single row in the ChunkInfo should be used or not for the given transaction
42 virtual bool Fetch(TransactionData transaction, row_t row) = 0;
43 virtual void CommitAppend(transaction_t commit_id, idx_t start, idx_t end) = 0;
44
45 virtual void Serialize(Serializer &serialize) = 0;
46 static unique_ptr<ChunkInfo> Deserialize(Deserializer &source);
47
48public:
49 template <class TARGET>
50 TARGET &Cast() {
51 if (type != TARGET::TYPE) {
52 throw InternalException("Failed to cast chunk info to type - query result type mismatch");
53 }
54 return reinterpret_cast<TARGET &>(*this);
55 }
56
57 template <class TARGET>
58 const TARGET &Cast() const {
59 if (type != TARGET::TYPE) {
60 throw InternalException("Failed to cast chunk info to type - query result type mismatch");
61 }
62 return reinterpret_cast<const TARGET &>(*this);
63 }
64};
65
66class ChunkConstantInfo : public ChunkInfo {
67public:
68 static constexpr const ChunkInfoType TYPE = ChunkInfoType::CONSTANT_INFO;
69
70public:
71 explicit ChunkConstantInfo(idx_t start);
72
73 atomic<transaction_t> insert_id;
74 atomic<transaction_t> delete_id;
75
76public:
77 idx_t GetSelVector(TransactionData transaction, SelectionVector &sel_vector, idx_t max_count) override;
78 idx_t GetCommittedSelVector(transaction_t min_start_id, transaction_t min_transaction_id,
79 SelectionVector &sel_vector, idx_t max_count) override;
80 bool Fetch(TransactionData transaction, row_t row) override;
81 void CommitAppend(transaction_t commit_id, idx_t start, idx_t end) override;
82
83 void Serialize(Serializer &serialize) override;
84 static unique_ptr<ChunkInfo> Deserialize(Deserializer &source);
85
86private:
87 template <class OP>
88 idx_t TemplatedGetSelVector(transaction_t start_time, transaction_t transaction_id, SelectionVector &sel_vector,
89 idx_t max_count);
90};
91
92class ChunkVectorInfo : public ChunkInfo {
93public:
94 static constexpr const ChunkInfoType TYPE = ChunkInfoType::VECTOR_INFO;
95
96public:
97 explicit ChunkVectorInfo(idx_t start);
98
99 //! The transaction ids of the transactions that inserted the tuples (if any)
100 atomic<transaction_t> inserted[STANDARD_VECTOR_SIZE];
101 atomic<transaction_t> insert_id;
102 atomic<bool> same_inserted_id;
103
104 //! The transaction ids of the transactions that deleted the tuples (if any)
105 atomic<transaction_t> deleted[STANDARD_VECTOR_SIZE];
106 atomic<bool> any_deleted;
107
108public:
109 idx_t GetSelVector(transaction_t start_time, transaction_t transaction_id, SelectionVector &sel_vector,
110 idx_t max_count);
111 idx_t GetSelVector(TransactionData transaction, SelectionVector &sel_vector, idx_t max_count) override;
112 idx_t GetCommittedSelVector(transaction_t min_start_id, transaction_t min_transaction_id,
113 SelectionVector &sel_vector, idx_t max_count) override;
114 bool Fetch(TransactionData transaction, row_t row) override;
115 void CommitAppend(transaction_t commit_id, idx_t start, idx_t end) override;
116
117 void Append(idx_t start, idx_t end, transaction_t commit_id);
118
119 //! Performs a delete in the ChunkVectorInfo - returns how many tuples were actually deleted
120 //! The number of rows that were actually deleted might be lower than the input count
121 //! In case we delete rows that were already deleted
122 //! Note that "rows" is written to to reflect the row ids that were actually deleted
123 //! i.e. after calling this function, rows will hold [0..actual_delete_count] row ids of the actually deleted tuples
124 idx_t Delete(transaction_t transaction_id, row_t rows[], idx_t count);
125 void CommitDelete(transaction_t commit_id, row_t rows[], idx_t count);
126
127 void Serialize(Serializer &serialize) override;
128 static unique_ptr<ChunkInfo> Deserialize(Deserializer &source);
129
130private:
131 template <class OP>
132 idx_t TemplatedGetSelVector(transaction_t start_time, transaction_t transaction_id, SelectionVector &sel_vector,
133 idx_t max_count);
134};
135
136} // namespace duckdb
137