|
35 | 35 | #include "arrow/type.h" |
36 | 36 | #include "arrow/type_traits.h" |
37 | 37 | #include "arrow/types/primitive.h" |
| 38 | +#include "arrow/types/string.h" |
| 39 | +#include "arrow/types/struct.h" |
38 | 40 | #include "arrow/util/memory-pool.h" |
39 | 41 | #include "arrow/util/status.h" |
40 | 42 |
|
@@ -64,15 +66,20 @@ void TestArrayRoundTrip(const Array& array) { |
64 | 66 |
|
65 | 67 | ASSERT_OK(WriteJsonArray(name, array, &writer)); |
66 | 68 |
|
| 69 | + std::string array_as_json = sb.GetString(); |
| 70 | + |
67 | 71 | rj::Document d; |
68 | | - d.Parse(sb.GetString()); |
| 72 | + d.Parse(array_as_json); |
| 73 | + |
| 74 | + if (d.HasParseError()) { FAIL() << "JSON parsing failed"; } |
69 | 75 |
|
70 | 76 | std::shared_ptr<Array> out; |
71 | 77 | ASSERT_OK(ReadJsonArray(default_memory_pool(), d, array.type(), &out)); |
72 | 78 |
|
73 | | - ASSERT_TRUE(array.Equals(out)); |
74 | | -} |
| 79 | + std::cout << array_as_json << std::endl; |
75 | 80 |
|
| 81 | + ASSERT_TRUE(array.Equals(out)) << array_as_json; |
| 82 | +} |
76 | 83 |
|
77 | 84 | template <typename T, typename ValueType> |
78 | 85 | void CheckPrimitive(const std::shared_ptr<DataType>& type, |
@@ -109,12 +116,70 @@ TEST(TestJsonSchemaWriter, FlatTypes) { |
109 | 116 | TestSchemaRoundTrip(schema); |
110 | 117 | } |
111 | 118 |
|
| 119 | +template <typename T> |
| 120 | +void PrimitiveTypesCheckOne() { |
| 121 | + using c_type = typename T::c_type; |
| 122 | + |
| 123 | + std::vector<bool> is_valid = {true, false, true, true, true, false, true, true}; |
| 124 | + std::vector<c_type> values = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 125 | + CheckPrimitive<T, c_type>(std::make_shared<T>(), is_valid, values); |
| 126 | +} |
112 | 127 |
|
113 | 128 | TEST(TestJsonArrayWriter, PrimitiveTypes) { |
| 129 | + PrimitiveTypesCheckOne<Int8Type>(); |
| 130 | + PrimitiveTypesCheckOne<Int16Type>(); |
| 131 | + PrimitiveTypesCheckOne<Int32Type>(); |
| 132 | + PrimitiveTypesCheckOne<Int64Type>(); |
| 133 | + PrimitiveTypesCheckOne<UInt8Type>(); |
| 134 | + PrimitiveTypesCheckOne<UInt16Type>(); |
| 135 | + PrimitiveTypesCheckOne<UInt32Type>(); |
| 136 | + PrimitiveTypesCheckOne<UInt64Type>(); |
| 137 | + PrimitiveTypesCheckOne<FloatType>(); |
| 138 | + PrimitiveTypesCheckOne<DoubleType>(); |
| 139 | + |
114 | 140 | std::vector<bool> is_valid = {true, false, true, true, true, false, true, true}; |
| 141 | + std::vector<std::string> values = {"foo", "bar", "", "baz", "qux", "foo", "a", "1"}; |
| 142 | + |
| 143 | + CheckPrimitive<StringType, std::string>(utf8(), is_valid, values); |
| 144 | + CheckPrimitive<BinaryType, std::string>(binary(), is_valid, values); |
| 145 | +} |
| 146 | + |
| 147 | +TEST(TestJsonArrayWriter, NestedTypes) { |
| 148 | + auto value_type = int32(); |
| 149 | + |
| 150 | + std::vector<bool> values_is_valid = {true, false, true, true, false, true, true}; |
| 151 | + std::vector<int32_t> values = {0, 1, 2, 3, 4, 5, 6}; |
| 152 | + |
| 153 | + std::shared_ptr<Buffer> values_buffer = test::GetBufferFromVector(values); |
| 154 | + std::shared_ptr<Buffer> values_bitmap; |
| 155 | + ASSERT_OK(test::GetBitmapFromBoolVector(values_is_valid, &values_bitmap)); |
| 156 | + auto values_array = std::make_shared<Int32Array>( |
| 157 | + value_type, static_cast<int32_t>(values.size()), values_buffer, 2, values_bitmap); |
| 158 | + |
| 159 | + // List |
| 160 | + std::vector<bool> list_is_valid = {true, false, true, true, true}; |
| 161 | + std::vector<int32_t> offsets = {0, 0, 0, 1, 4, 7}; |
| 162 | + |
| 163 | + std::shared_ptr<Buffer> list_bitmap; |
| 164 | + ASSERT_OK(test::GetBitmapFromBoolVector(list_is_valid, &list_bitmap)); |
| 165 | + std::shared_ptr<Buffer> offsets_buffer = test::GetBufferFromVector(offsets); |
| 166 | + |
| 167 | + ListArray list_array(list(value_type), 5, offsets_buffer, values_array, 1, list_bitmap); |
| 168 | + |
| 169 | + TestArrayRoundTrip(list_array); |
| 170 | + |
| 171 | + // Struct |
| 172 | + std::vector<bool> struct_is_valid = {true, false, true, true, true, false, true}; |
| 173 | + std::shared_ptr<Buffer> struct_bitmap; |
| 174 | + ASSERT_OK(test::GetBitmapFromBoolVector(struct_is_valid, &struct_bitmap)); |
| 175 | + |
| 176 | + auto struct_type = |
| 177 | + struct_({field("f1", int32()), field("f2", int32()), field("f3", int32())}); |
115 | 178 |
|
116 | | - std::vector<uint8_t> u1 = {0, 1, 2, 3, 4, 5, 6, 7}; |
117 | | - CheckPrimitive<UInt8Type, uint8_t>(uint8(), is_valid, u1); |
| 179 | + std::vector<std::shared_ptr<Array>> fields = {values_array, values_array, values_array}; |
| 180 | + StructArray struct_array( |
| 181 | + struct_type, static_cast<int>(struct_is_valid.size()), fields, 2, struct_bitmap); |
| 182 | + TestArrayRoundTrip(struct_array); |
118 | 183 | } |
119 | 184 |
|
120 | 185 | } // namespace ipc |
|
0 commit comments