forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumnar_row_test.cpp
More file actions
389 lines (353 loc) · 19 KB
/
Copy pathcolumnar_row_test.cpp
File metadata and controls
389 lines (353 loc) · 19 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* 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.
*/
#include "paimon/common/data/columnar/columnar_row.h"
#include <utility>
#include "arrow/api.h"
#include "arrow/array/array_base.h"
#include "arrow/array/array_dict.h"
#include "arrow/array/array_nested.h"
#include "arrow/ipc/json_simple.h"
#include "gtest/gtest.h"
#include "paimon/common/data/columnar/columnar_row_ref.h"
#include "paimon/common/utils/date_time_utils.h"
#include "paimon/memory/bytes.h"
#include "paimon/memory/memory_pool.h"
namespace paimon::test {
TEST(ColumnarRowTest, TestSimple) {
auto pool = GetDefaultPool();
std::shared_ptr<arrow::DataType> target_type =
arrow::struct_({arrow::field("f1", arrow::boolean()), arrow::field("f2", arrow::int8()),
arrow::field("f3", arrow::int16()), arrow::field("f4", arrow::int32()),
arrow::field("f5", arrow::int64()), arrow::field("f6", arrow::float32()),
arrow::field("f7", arrow::float64()), arrow::field("f8", arrow::utf8())});
auto f1 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::boolean(), R"([true, false, false, true])")
.ValueOrDie();
auto f2 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int8(), R"([0, 1, 2, 3])").ValueOrDie();
auto f3 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int16(), R"([4, 5, 6, 7])").ValueOrDie();
auto f4 = arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), R"([10, 11, 12, 13])")
.ValueOrDie();
auto f5 = arrow::ipc::internal::json::ArrayFromJSON(arrow::int64(), R"([15, 16, 17, 18])")
.ValueOrDie();
auto f6 = arrow::ipc::internal::json::ArrayFromJSON(arrow::float32(), R"([0.0, 1.1, 2.2, 3.3])")
.ValueOrDie();
auto f7 = arrow::ipc::internal::json::ArrayFromJSON(arrow::float64(), R"([5.5, 6.6, 7.7, 8.8])")
.ValueOrDie();
auto f8 = arrow::ipc::internal::json::ArrayFromJSON(arrow::utf8(),
R"(["Hello", "World", "HELLO", "WORLD"])")
.ValueOrDie();
auto data = arrow::StructArray::Make({f1, f2, f3, f4, f5, f6, f7, f8}, target_type->fields())
.ValueOrDie();
auto row = ColumnarRow(data->fields(), pool, 0);
ASSERT_EQ(row.GetFieldCount(), 8);
ASSERT_EQ(row.GetBoolean(0), true);
ASSERT_EQ(row.GetByte(1), 0);
ASSERT_EQ(row.GetShort(2), 4);
ASSERT_EQ(row.GetInt(3), 10);
ASSERT_EQ(row.GetLong(4), 15);
ASSERT_EQ(row.GetFloat(5), 0.0);
ASSERT_EQ(row.GetDouble(6), 5.5);
ASSERT_EQ(row.GetString(7).ToString(), "Hello");
ASSERT_EQ(std::string(row.GetStringView(7)), "Hello");
}
TEST(ColumnarRowRefTest, TestSimple) {
auto pool = GetDefaultPool();
std::shared_ptr<arrow::DataType> target_type =
arrow::struct_({arrow::field("f1", arrow::int32()), arrow::field("f2", arrow::utf8())});
auto f1 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), R"([1, 2, 3])").ValueOrDie();
auto f2 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::utf8(), R"(["alpha", "beta", "gamma"])")
.ValueOrDie();
auto data = arrow::StructArray::Make({f1, f2}, target_type->fields()).ValueOrDie();
auto ctx = std::make_shared<ColumnarBatchContext>(data, data->fields(), pool);
ColumnarRowRef row(ctx, 1);
ASSERT_EQ(row.GetFieldCount(), 2);
ASSERT_EQ(row.GetInt(0), 2);
ASSERT_EQ(std::string(row.GetStringView(1)), "beta");
auto row_kind = row.GetRowKind();
ASSERT_TRUE(row_kind.ok());
ASSERT_EQ(row_kind.value(), RowKind::Insert());
row.SetRowKind(RowKind::Delete());
auto updated_kind = row.GetRowKind();
ASSERT_TRUE(updated_kind.ok());
ASSERT_EQ(updated_kind.value(), RowKind::Delete());
}
TEST(ColumnarRowTest, TestComplexAndNestedType) {
auto pool = GetDefaultPool();
std::shared_ptr<arrow::DataType> target_type = arrow::struct_({
arrow::field("f0", arrow::date32()),
arrow::field("f1", arrow::decimal128(10, 3)),
arrow::field("f2", arrow::timestamp(arrow::TimeUnit::NANO)),
arrow::field("f3", arrow::binary()),
arrow::field(
"f4", arrow::struct_({field("sub1", arrow::int64()), field("sub2", arrow::int64()),
field("sub3", arrow::int64()), field("sub4", arrow::int64())})),
arrow::field("f5", arrow::list(arrow::int64())),
arrow::field("f6", arrow::map(arrow::int32(), arrow::int64())),
arrow::field("f7", arrow::map(arrow::int32(), arrow::list(arrow::int64()))),
arrow::field("f8", arrow::timestamp(arrow::TimeUnit::SECOND)),
arrow::field("f9", arrow::timestamp(arrow::TimeUnit::MILLI)),
arrow::field("f10", arrow::timestamp(arrow::TimeUnit::MICRO)),
});
auto f0 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::date32(), R"([109, 1000, -1000, 555])")
.ValueOrDie();
auto f1 = arrow::ipc::internal::json::ArrayFromJSON(
arrow::decimal128(10, 3), R"(["1.234", "1234.000", "-9876.543", "666.888"])")
.ValueOrDie();
auto f2 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::timestamp(arrow::TimeUnit::NANO),
R"(["1970-01-01T00:00:59","2000-02-29T23:23:23",
"1899-01-01T00:59:20","2033-05-18T03:33:20"])")
.ValueOrDie();
auto f3 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::binary(), R"(["aaa", "bb", "ccc", "bbb"])")
.ValueOrDie();
auto f4 = arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_({
field("sub1", arrow::int64()),
field("sub2", arrow::int64()),
field("sub3", arrow::int64()),
field("sub4", arrow::int64()),
}),
R"([
[1, 3, 2, 5],
[2, 2, 1, 3],
[3, 2, 1, 3],
[4, 1, 0, 2]
])")
.ValueOrDie();
auto f5 = arrow::ipc::internal::json::ArrayFromJSON(arrow::list(arrow::int64()),
"[[1, 1, 2], [3], [2], [-4]]")
.ValueOrDie();
auto f6 = arrow::ipc::internal::json::ArrayFromJSON(arrow::map(arrow::int32(), arrow::int64()),
R"([[[1, 3], [4, 4]],
[[1, 5], [7, 6], [100, 7]],
[[0, 9]],
null])")
.ValueOrDie();
auto f7 = arrow::ipc::internal::json::ArrayFromJSON(
arrow::map(arrow::int32(), arrow::list(arrow::int64())),
R"([[[1, [10, 20]], [4, [40, 50, 100]]],
[[1, [1, 2]], [7, [6]], [100, [8]]],
[[0, [9]]],
null])")
.ValueOrDie();
auto f8 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::timestamp(arrow::TimeUnit::SECOND),
R"(["1970-01-01T00:00:59","2000-02-29T23:23:23",
"1899-01-01T00:59:20","2033-05-18T03:33:20"])")
.ValueOrDie();
auto f9 = arrow::ipc::internal::json::ArrayFromJSON(
arrow::timestamp(arrow::TimeUnit::MILLI),
R"(["1970-01-01T00:00:59.001","2000-02-29T23:23:23",
"1899-01-01T00:59:20","2033-05-18T03:33:20"])")
.ValueOrDie();
auto f10 = arrow::ipc::internal::json::ArrayFromJSON(
arrow::timestamp(arrow::TimeUnit::MICRO),
R"(["1970-01-01T00:00:59.000001","2000-02-29T23:23:23",
"1899-01-01T00:59:20","2033-05-18T03:33:20"])")
.ValueOrDie();
auto data = arrow::StructArray::Make({f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10},
target_type->fields())
.ValueOrDie();
auto row = ColumnarRow(data->fields(), pool, 0);
ASSERT_EQ(row.GetFieldCount(), 11);
ASSERT_EQ(row.GetDate(0), 109);
ASSERT_EQ(row.GetDecimal(1, 10, 3), Decimal(10, 3, 1234));
auto ts = row.GetTimestamp(2, /*precision=*/9);
ASSERT_EQ(ts, Timestamp(59000, 0));
ASSERT_EQ(*row.GetBinary(3), Bytes("aaa", pool.get()));
ASSERT_EQ(std::string(row.GetStringView(3)), "aaa");
auto result_row = row.GetRow(4, 4);
ASSERT_EQ(result_row->GetLong(0), 1);
ASSERT_EQ(result_row->GetLong(1), 3);
ASSERT_EQ(result_row->GetLong(2), 2);
ASSERT_EQ(result_row->GetLong(3), 5);
std::vector<int64_t> values = {1, 1, 2};
auto result_array = row.GetArray(5);
ASSERT_EQ(result_array->ToLongArray().value(), values);
auto result_key = row.GetMap(6)->KeyArray();
auto result_value = row.GetMap(6)->ValueArray();
ASSERT_EQ(result_key->ToIntArray().value(), std::vector<int32_t>({1, 4}));
ASSERT_EQ(result_value->ToLongArray().value(), std::vector<int64_t>({3, 4}));
result_key = row.GetMap(7)->KeyArray();
result_value = row.GetMap(7)->ValueArray();
ASSERT_EQ(result_key->ToIntArray().value(), std::vector<int32_t>({1, 4}));
ASSERT_EQ(2, result_value->Size());
ASSERT_EQ(result_value->GetArray(0)->ToLongArray().value(), std::vector<int64_t>({10, 20}));
ASSERT_EQ(result_value->GetArray(1)->ToLongArray().value(),
std::vector<int64_t>({40, 50, 100}));
auto ts_second = row.GetTimestamp(8, /*precision=*/0);
ASSERT_EQ(ts_second, Timestamp(59000, 0));
auto ts_milli = row.GetTimestamp(9, /*precision=*/3);
ASSERT_EQ(ts_milli, Timestamp(59001, 0));
auto ts_micro = row.GetTimestamp(10, /*precision=*/6);
ASSERT_EQ(ts_micro, Timestamp(59000, 1000));
}
TEST(ColumnarRowTest, TestTimestampType) {
auto pool = GetDefaultPool();
auto timezone = DateTimeUtils::GetLocalTimezoneName();
arrow::FieldVector fields = {
arrow::field("ts_sec", arrow::timestamp(arrow::TimeUnit::SECOND)),
arrow::field("ts_milli", arrow::timestamp(arrow::TimeUnit::MILLI)),
arrow::field("ts_micro", arrow::timestamp(arrow::TimeUnit::MICRO)),
arrow::field("ts_nano", arrow::timestamp(arrow::TimeUnit::NANO)),
arrow::field("ts_tz_sec", arrow::timestamp(arrow::TimeUnit::SECOND, timezone)),
arrow::field("ts_tz_milli", arrow::timestamp(arrow::TimeUnit::MILLI, timezone)),
arrow::field("ts_tz_micro", arrow::timestamp(arrow::TimeUnit::MICRO, timezone)),
arrow::field("ts_tz_nano", arrow::timestamp(arrow::TimeUnit::NANO, timezone)),
};
auto array = std::dynamic_pointer_cast<arrow::StructArray>(
arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(fields), R"([
["1970-01-01 00:00:01", "1970-01-01 00:00:00.001", "1970-01-01 00:00:00.000001", "1970-01-01 00:00:00.000000001", "1970-01-01 00:00:02", "1970-01-01 00:00:00.002", "1970-01-01 00:00:00.000002", "1970-01-01 00:00:00.000000002"],
["1970-01-01 00:00:03", "1970-01-01 00:00:00.003", null, "1970-01-01 00:00:00.000000003", "1970-01-01 00:00:04", "1970-01-01 00:00:00.004", "1970-01-01 00:00:00.000004", "1970-01-01 00:00:00.000000004"],
["1970-01-01 00:00:05", "1970-01-01 00:00:00.005", null, null, "1970-01-01 00:00:06", null, "1970-01-01 00:00:00.000006", null]
])")
.ValueOrDie());
{
auto row = ColumnarRow(array->fields(), pool, 0);
ASSERT_EQ(row.GetFieldCount(), 8);
ASSERT_EQ(row.GetTimestamp(0, /*precision=*/0), Timestamp(1000, 0));
ASSERT_EQ(row.GetTimestamp(1, /*precision=*/3), Timestamp(1, 0));
ASSERT_EQ(row.GetTimestamp(2, /*precision=*/6), Timestamp(0, 1000));
ASSERT_EQ(row.GetTimestamp(3, /*precision=*/9), Timestamp(0, 1));
ASSERT_EQ(row.GetTimestamp(4, /*precision=*/0), Timestamp(2000, 0));
ASSERT_EQ(row.GetTimestamp(5, /*precision=*/3), Timestamp(2, 0));
ASSERT_EQ(row.GetTimestamp(6, /*precision=*/6), Timestamp(0, 2000));
ASSERT_EQ(row.GetTimestamp(7, /*precision=*/9), Timestamp(0, 2));
}
{
auto row = ColumnarRow(array->fields(), pool, 2);
ASSERT_EQ(row.GetFieldCount(), 8);
ASSERT_EQ(row.GetTimestamp(0, /*precision=*/0), Timestamp(5000, 0));
ASSERT_EQ(row.GetTimestamp(1, /*precision=*/3), Timestamp(5, 0));
ASSERT_TRUE(row.IsNullAt(2));
ASSERT_TRUE(row.IsNullAt(3));
ASSERT_EQ(row.GetTimestamp(4, /*precision=*/0), Timestamp(6000, 0));
ASSERT_TRUE(row.IsNullAt(5));
ASSERT_EQ(row.GetTimestamp(6, /*precision=*/6), Timestamp(0, 6000));
}
}
TEST(ColumnarRowTest, TestNull) {
auto pool = GetDefaultPool();
std::shared_ptr<arrow::DataType> target_type =
arrow::struct_({arrow::field("f1", arrow::boolean()), arrow::field("f2", arrow::int8())});
auto f1 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::boolean(), R"([null, false, false, true])")
.ValueOrDie();
auto f2 =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int8(), R"([null, 1, 2, 3])").ValueOrDie();
auto data = arrow::StructArray::Make({f1, f2}, target_type->fields()).ValueOrDie();
auto row = ColumnarRow(data->fields(), pool, 0);
row.SetRowKind(RowKind::Insert());
ASSERT_EQ(row.GetFieldCount(), 2);
ASSERT_EQ(row.GetRowKind().value(), RowKind::Insert());
ASSERT_TRUE(row.IsNullAt(0));
ASSERT_TRUE(row.IsNullAt(1));
}
TEST(ColumnarRowTest, TestDictionary) {
auto pool = GetDefaultPool();
auto dict = arrow::ipc::internal::json::ArrayFromJSON(arrow::utf8(), R"(["foo", "bar", "baz"])")
.ValueOrDie();
auto dict_type = arrow::dictionary(arrow::int32(), arrow::utf8());
auto indices =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), "[1, 2, 0, 2, 0]").ValueOrDie();
std::shared_ptr<arrow::DictionaryArray> dict_array =
std::make_shared<arrow::DictionaryArray>(dict_type, indices, dict);
auto f0 = arrow::field("f0", arrow::list(dict_type));
auto f1 = arrow::field("f1", arrow::struct_({arrow::field("sub1", arrow::int64()),
arrow::field("sub2", arrow::binary()),
arrow::field("sub3", dict_type)}));
auto list_offsets =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), R"([0, 5])").ValueOrDie();
auto f0_array = arrow::ListArray::FromArrays(*list_offsets, *dict_array).ValueOrDie();
auto sub1_array =
arrow::ipc::internal::json::ArrayFromJSON(arrow::int64(), R"([1])").ValueOrDie();
auto sub2_array =
arrow::ipc::internal::json::ArrayFromJSON(arrow::binary(), R"(["apple"])").ValueOrDie();
auto sub3_array =
std::make_shared<arrow::DictionaryArray>(dict_type, indices->Slice(0, 1), dict);
auto f1_array = std::make_shared<arrow::StructArray>(
f1->type(), /*length=*/1, arrow::ArrayVector({sub1_array, sub2_array, sub3_array}));
auto struct_type = arrow::struct_({f0, f1});
// data: [["bar", "baz", "foo", "baz", "foo"], [1, "apple", "bar"]]
auto data = std::make_shared<arrow::StructArray>(struct_type, /*length=*/1,
arrow::ArrayVector({f0_array, f1_array}));
auto row = ColumnarRow(data->fields(), pool, 0);
ASSERT_FALSE(row.IsNullAt(0));
auto internal_array = row.GetArray(0);
ASSERT_TRUE(internal_array);
ASSERT_EQ(5, internal_array->Size());
ASSERT_EQ("bar", internal_array->GetString(0).ToString());
ASSERT_EQ("baz", internal_array->GetString(1).ToString());
ASSERT_EQ("foo", internal_array->GetString(2).ToString());
ASSERT_EQ("baz", internal_array->GetString(3).ToString());
ASSERT_EQ("foo", internal_array->GetString(4).ToString());
ASSERT_EQ("bar", std::string(internal_array->GetStringView(0)));
ASSERT_EQ("baz", std::string(internal_array->GetStringView(1)));
ASSERT_EQ("foo", std::string(internal_array->GetStringView(2)));
ASSERT_EQ("baz", std::string(internal_array->GetStringView(3)));
ASSERT_EQ("foo", std::string(internal_array->GetStringView(4)));
ASSERT_FALSE(row.IsNullAt(1));
auto internal_row = row.GetRow(1, 3);
ASSERT_TRUE(internal_row);
ASSERT_EQ(1, internal_row->GetLong(0));
auto bytes = internal_row->GetBinary(1);
ASSERT_EQ("apple", std::string(bytes->data(), bytes->size()));
ASSERT_EQ("apple", std::string(internal_row->GetStringView(1)));
ASSERT_EQ("bar", internal_row->GetString(2).ToString());
ASSERT_EQ("bar", std::string(internal_row->GetStringView(2)));
}
TEST(ColumnarRowTest, TestDataLifeCycle) {
auto pool = GetDefaultPool();
std::shared_ptr<arrow::DataType> target_type = arrow::struct_({arrow::field(
"f0", arrow::struct_({field("sub1", arrow::int64()), field("sub2", arrow::int64()),
field("sub3", arrow::int64()), field("sub4", arrow::int64())}))});
auto f0 = arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_({
field("sub1", arrow::int64()),
field("sub2", arrow::int64()),
field("sub3", arrow::int64()),
field("sub4", arrow::int64()),
}),
R"([
[1, 3, 2, 5],
[2, 2, 1, 3],
[3, 2, 1, 3],
[4, 1, 0, 2]
])")
.ValueOrDie();
auto data = arrow::StructArray::Make({f0}, target_type->fields()).ValueOrDie();
auto row = std::make_unique<ColumnarRow>(data, data->fields(), pool, 0);
data.reset();
f0.reset();
// array data is only held by columnar row
ASSERT_EQ(1, row->struct_array_.use_count());
ASSERT_EQ(row->GetFieldCount(), 1);
ASSERT_FALSE(row->IsNullAt(0));
auto result_row = row->GetRow(0, 4);
ASSERT_FALSE(result_row->IsNullAt(0));
ASSERT_EQ(result_row->GetLong(0), 1);
ASSERT_FALSE(result_row->IsNullAt(1));
ASSERT_EQ(result_row->GetLong(1), 3);
ASSERT_FALSE(result_row->IsNullAt(2));
ASSERT_EQ(result_row->GetLong(2), 2);
ASSERT_FALSE(result_row->IsNullAt(3));
ASSERT_EQ(result_row->GetLong(3), 5);
}
} // namespace paimon::test