-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgeneric_row.h
More file actions
233 lines (198 loc) · 8.69 KB
/
generic_row.h
File metadata and controls
233 lines (198 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2024-present Alibaba Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include "paimon/common/data/binary_string.h"
#include "paimon/common/data/data_define.h"
#include "paimon/common/data/internal_row.h"
#include "paimon/common/types/row_kind.h"
#include "paimon/data/decimal.h"
#include "paimon/data/timestamp.h"
#include "paimon/result.h"
namespace paimon {
class Bytes;
class InternalArray;
class InternalMap;
/// An internal data structure representing data of row.
/// GenericRow is a generic implementation of `InternalRow` which is backed by an array of
/// VariantType. A GenericRow can have an arbitrary number of fields of different types. The fields
/// in a row can be accessed by position (0-based) using either the generic `GetField()` or
/// type-specific getters (such as `GetInt()`). A field can be updated by the generic `SetField()`.
/// @note All fields of this data structure must be internal data structures. See `InternalRow`
/// for more information about internal data structures. The fields in GenericRow can be null for
/// representing nullability. Noted that GenericRow does not support nested type (i.e., Map, Array,
/// Struct).
class GenericRow : public InternalRow {
public:
/// Creates an instance of GenericRow with given number of fields.
/// Initially, all fields are set to null. By default, the row describes a
/// `RowKind::Insert()` in a changelog.
/// @note All fields of the row must be internal data structures.
/// @param arity number of fields
explicit GenericRow(int32_t arity) : GenericRow(RowKind::Insert(), arity) {}
/// Creates an instance of GenericRow with given kind and number of fields.
/// Initially, all fields are set to null.
/// @note All fields of the row must be internal data structures.
/// @param kind kind of change that this row describes in a changelog
/// @param arity number of fields
GenericRow(const RowKind* kind, int32_t arity) : kind_(kind) {
fields_.resize(arity);
}
/// Sets the field value at the given position.
/// @note The given field value must be an internal data structures. Otherwise the
/// GenericRow is corrupted. See `InternalRow` for more information about internal data
/// structures. The field value can be null for representing nullability.
void SetField(int32_t pos, const VariantType& value) {
assert(static_cast<size_t>(pos) < fields_.size());
fields_[pos] = value;
}
/// @return the field value at the given position.
/// @note The returned value is in internal data structure. See `InternalRow`
/// for more information about internal data structures.
/// The returned field value can be null for representing nullability.
const VariantType& GetField(int32_t pos) const {
assert(static_cast<size_t>(pos) < fields_.size());
return fields_[pos];
}
int32_t GetFieldCount() const override {
return fields_.size();
}
Result<const RowKind*> GetRowKind() const override {
return kind_;
}
void SetRowKind(const RowKind* kind) override {
kind_ = kind;
}
bool IsNullAt(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::IsVariantNull(fields_[pos]);
}
void AddDataHolder(std::unique_ptr<InternalRow>&& holder) {
holders_.push_back(std::move(holder));
}
bool GetBoolean(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<bool>(fields_[pos]);
}
char GetByte(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<char>(fields_[pos]);
}
int16_t GetShort(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<int16_t>(fields_[pos]);
}
int32_t GetInt(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<int32_t>(fields_[pos]);
}
int32_t GetDate(int32_t pos) const override {
return GetInt(pos);
}
int64_t GetLong(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<int64_t>(fields_[pos]);
}
float GetFloat(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<float>(fields_[pos]);
}
double GetDouble(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<double>(fields_[pos]);
}
BinaryString GetString(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
auto* value_ptr = DataDefine::GetVariantPtr<std::string_view>(fields_[pos]);
if (value_ptr) {
auto bytes = std::make_shared<Bytes>(value_ptr->size(), GetDefaultPool().get());
memcpy(bytes->data(), value_ptr->data(), value_ptr->size());
return BinaryString::FromBytes(bytes);
}
return DataDefine::GetVariantValue<BinaryString>(fields_[pos]);
}
std::string_view GetStringView(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<std::string_view>(fields_[pos]);
}
Decimal GetDecimal(int32_t pos, int32_t precision, int32_t scale) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<Decimal>(fields_[pos]);
}
Timestamp GetTimestamp(int32_t pos, int32_t precision) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<Timestamp>(fields_[pos]);
}
std::shared_ptr<Bytes> GetBinary(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<std::shared_ptr<Bytes>>(fields_[pos]);
}
std::shared_ptr<InternalArray> GetArray(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<std::shared_ptr<InternalArray>>(fields_[pos]);
}
std::shared_ptr<InternalMap> GetMap(int32_t pos) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<std::shared_ptr<InternalMap>>(fields_[pos]);
}
std::shared_ptr<InternalRow> GetRow(int32_t pos, int32_t num_fields) const override {
assert(static_cast<size_t>(pos) < fields_.size());
return DataDefine::GetVariantValue<std::shared_ptr<InternalRow>>(fields_[pos]);
}
std::string ToString() const override {
std::string ret = "(";
for (size_t i = 0; i < fields_.size(); i++) {
if (i != 0) {
ret.append(",");
}
ret.append(DataDefine::VariantValueToString(fields_[i]));
}
ret.append(")");
return ret;
}
bool operator==(const GenericRow& other) const {
if (this == &other) {
return true;
}
return *kind_ == *other.kind_ && fields_ == other.fields_;
}
/// Creates an instance of GenericRow with given field values.
static std::unique_ptr<GenericRow> Of(const std::vector<VariantType>& values) {
auto row = std::make_unique<GenericRow>(values.size());
for (size_t i = 0; i < values.size(); i++) {
row->SetField(i, values[i]);
}
return row;
}
private:
/// The array to store the actual internal format values.
std::vector<VariantType> fields_;
/// As GenericRow only holds string view for string data to avoid deep copy, original data must
/// be held in holders_
std::vector<std::unique_ptr<InternalRow>> holders_;
/// The kind of change that a row describes in a changelog.
const RowKind* kind_;
};
} // namespace paimon