Skip to content

Commit 7f86def

Browse files
committed
[C++] Remove IPC dependency from array_union_test.cc by moving MakeUnion to testing utilities
1 parent 7de2f61 commit 7f86def

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed

cpp/src/arrow/array/array_union_test.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include "arrow/array.h"
2323
#include "arrow/array/builder_nested.h"
2424
#include "arrow/array/builder_union.h"
25-
// TODO ipc shouldn't be included here
26-
#include "arrow/ipc/test_common.h"
2725
#include "arrow/testing/builder.h"
2826
#include "arrow/testing/gtest_util.h"
2927
#include "arrow/testing/util.h"
@@ -37,7 +35,7 @@ using internal::checked_pointer_cast;
3735

3836
TEST(TestUnionArray, TestSliceEquals) {
3937
std::shared_ptr<RecordBatch> batch;
40-
ASSERT_OK(ipc::test::MakeUnion(&batch));
38+
ASSERT_OK(MakeUnion(&batch));
4139

4240
auto CheckUnion = [](std::shared_ptr<Array> array) {
4341
const int64_t size = array->length();

cpp/src/arrow/integration/json_integration_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ TEST(TestJsonArrayWriter, NestedTypes) {
927927

928928
TEST(TestJsonArrayWriter, Unions) {
929929
std::shared_ptr<RecordBatch> batch;
930-
ASSERT_OK(MakeUnion(&batch));
930+
ASSERT_OK(ipc::test::MakeUnion(&batch));
931931

932932
for (int i = 0; i < batch->num_columns(); ++i) {
933933
TestArrayRoundTrip(*batch->column(i));
@@ -1155,7 +1155,7 @@ const std::vector<ipc::test::MakeRecordBatch*> kBatchCases = {
11551155
&MakeDeeplyNestedListView,
11561156
&MakeStringTypesRecordBatchWithNulls,
11571157
&MakeStruct,
1158-
&MakeUnion,
1158+
&ipc::test::MakeUnion,
11591159
&MakeDictionary,
11601160
&MakeNestedDictionary,
11611161
&MakeMap,

cpp/src/arrow/ipc/test_common.cc

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -670,55 +670,8 @@ Status MakeRunEndEncoded(std::shared_ptr<RecordBatch>* out) {
670670
}
671671

672672
Status MakeUnion(std::shared_ptr<RecordBatch>* out) {
673-
// Define schema
674-
std::vector<std::shared_ptr<Field>> union_fields(
675-
{field("u0", int32()), field("u1", uint8())});
676-
677-
std::vector<int8_t> type_codes = {5, 10};
678-
auto sparse_type = sparse_union(union_fields, type_codes);
679-
auto dense_type = dense_union(union_fields, type_codes);
680-
681-
auto f0 = field("sparse", sparse_type);
682-
auto f1 = field("dense", dense_type);
683-
684-
auto schema = ::arrow::schema({f0, f1});
685-
686-
// Create data
687-
std::vector<std::shared_ptr<Array>> sparse_children(2);
688-
std::vector<std::shared_ptr<Array>> dense_children(2);
689-
690-
const int64_t length = 7;
691-
692-
std::shared_ptr<Buffer> type_ids_buffer;
693-
std::vector<uint8_t> type_ids = {5, 10, 5, 5, 10, 10, 5};
694-
RETURN_NOT_OK(CopyBufferFromVector(type_ids, default_memory_pool(), &type_ids_buffer));
695-
696-
std::vector<int32_t> u0_values = {0, 1, 2, 3, 4, 5, 6};
697-
ArrayFromVector<Int32Type, int32_t>(u0_values, &sparse_children[0]);
698-
699-
std::vector<uint8_t> u1_values = {10, 11, 12, 13, 14, 15, 16};
700-
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &sparse_children[1]);
701-
702-
// dense children
703-
u0_values = {0, 2, 3, 7};
704-
ArrayFromVector<Int32Type, int32_t>(u0_values, &dense_children[0]);
705-
706-
u1_values = {11, 14, 15};
707-
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &dense_children[1]);
708-
709-
std::shared_ptr<Buffer> offsets_buffer;
710-
std::vector<int32_t> offsets = {0, 0, 1, 2, 1, 2, 3};
711-
RETURN_NOT_OK(CopyBufferFromVector(offsets, default_memory_pool(), &offsets_buffer));
712-
713-
auto sparse = std::make_shared<SparseUnionArray>(sparse_type, length, sparse_children,
714-
type_ids_buffer);
715-
auto dense = std::make_shared<DenseUnionArray>(dense_type, length, dense_children,
716-
type_ids_buffer, offsets_buffer);
717-
718-
// construct batch
719-
std::vector<std::shared_ptr<Array>> arrays = {sparse, dense};
720-
*out = RecordBatch::Make(schema, length, arrays);
721-
return Status::OK();
673+
// Delegate to the shared implementation in arrow::testing::util
674+
return ::arrow::MakeUnion(out);
722675
}
723676

724677
Status MakeDictionary(std::shared_ptr<RecordBatch>* out) {

cpp/src/arrow/testing/util.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@
3939
# include <unistd.h> // IWYU pragma: keep
4040
#endif
4141

42+
#include "arrow/array.h"
43+
#include "arrow/array/builder_union.h"
4244
#include "arrow/config.h"
4345
#include "arrow/table.h"
46+
#include "arrow/testing/builder.h"
4447
#include "arrow/testing/random.h"
4548
#include "arrow/type.h"
4649
#include "arrow/util/cpu_info.h"
@@ -242,4 +245,55 @@ std::vector<int64_t> GetSupportedHardwareFlags(
242245
return hardware_flags;
243246
}
244247

248+
Status MakeUnion(std::shared_ptr<RecordBatch>* out) {
249+
// Define schema
250+
std::vector<std::shared_ptr<Field>> union_fields(
251+
{field("u0", int32()), field("u1", uint8())});
252+
253+
std::vector<int8_t> type_codes = {5, 10};
254+
auto sparse_type = sparse_union(union_fields, type_codes);
255+
auto dense_type = dense_union(union_fields, type_codes);
256+
257+
auto f0 = field("sparse", sparse_type);
258+
auto f1 = field("dense", dense_type);
259+
auto schema = ::arrow::schema({f0, f1});
260+
261+
// Create data
262+
std::vector<std::shared_ptr<Array>> sparse_children(2);
263+
std::vector<std::shared_ptr<Array>> dense_children(2);
264+
265+
const int64_t length = 7;
266+
267+
std::shared_ptr<Buffer> type_ids_buffer;
268+
std::vector<uint8_t> type_ids = {5, 10, 5, 5, 10, 10, 5};
269+
RETURN_NOT_OK(CopyBufferFromVector(type_ids, default_memory_pool(), &type_ids_buffer));
270+
271+
std::vector<int32_t> u0_values = {0, 1, 2, 3, 4, 5, 6};
272+
ArrayFromVector<Int32Type, int32_t>(u0_values, &sparse_children[0]);
273+
274+
std::vector<uint8_t> u1_values = {10, 11, 12, 13, 14, 15, 16};
275+
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &sparse_children[1]);
276+
277+
// dense children
278+
u0_values = {0, 2, 3, 7};
279+
ArrayFromVector<Int32Type, int32_t>(u0_values, &dense_children[0]);
280+
281+
u1_values = {11, 14, 15};
282+
ArrayFromVector<UInt8Type, uint8_t>(u1_values, &dense_children[1]);
283+
284+
std::shared_ptr<Buffer> offsets_buffer;
285+
std::vector<int32_t> offsets = {0, 0, 1, 2, 1, 2, 3};
286+
RETURN_NOT_OK(CopyBufferFromVector(offsets, default_memory_pool(), &offsets_buffer));
287+
288+
auto sparse = std::make_shared<SparseUnionArray>(sparse_type, length, sparse_children,
289+
type_ids_buffer);
290+
auto dense = std::make_shared<DenseUnionArray>(dense_type, length, dense_children,
291+
type_ids_buffer, offsets_buffer);
292+
293+
// construct batch
294+
std::vector<std::shared_ptr<Array>> arrays = {sparse, dense};
295+
*out = RecordBatch::Make(schema, length, arrays);
296+
return Status::OK();
297+
}
298+
245299
} // namespace arrow

cpp/src/arrow/testing/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,10 @@ ARROW_TESTING_EXPORT
142142
std::vector<int64_t> GetSupportedHardwareFlags(
143143
const std::vector<int64_t>& candidate_flags);
144144

145+
// Creates a RecordBatch containing both sparse and dense union arrays with the same
146+
// union type definition. The union type has two fields: "u0" (int32) and "u1" (uint8)
147+
// with type codes 5 and 10 respectively. Both arrays have length 7.
148+
ARROW_TESTING_EXPORT
149+
Status MakeUnion(std::shared_ptr<RecordBatch>* out);
150+
145151
} // namespace arrow

0 commit comments

Comments
 (0)