|
21 | 21 | #include <ranges> |
22 | 22 | #include <vector> |
23 | 23 |
|
| 24 | +#include <arrow/array/builder_decimal.h> |
24 | 25 | #include <arrow/array/builder_nested.h> |
25 | 26 | #include <arrow/array/builder_primitive.h> |
26 | 27 | #include <arrow/c/bridge.h> |
|
31 | 32 | #include <avro/Compiler.hh> |
32 | 33 | #include <avro/Generic.hh> |
33 | 34 | #include <avro/Node.hh> |
| 35 | +#include <avro/Specific.hh> |
| 36 | +#include <avro/Stream.hh> |
34 | 37 | #include <avro/Types.hh> |
35 | 38 | #include <gmock/gmock.h> |
36 | 39 | #include <gtest/gtest.h> |
37 | 40 |
|
38 | 41 | #include "iceberg/avro/avro_data_util_internal.h" |
| 42 | +#include "iceberg/avro/avro_direct_encoder_internal.h" |
39 | 43 | #include "iceberg/avro/avro_schema_util_internal.h" |
40 | 44 | #include "iceberg/expression/literal.h" |
41 | 45 | #include "iceberg/schema.h" |
42 | 46 | #include "iceberg/schema_internal.h" |
43 | 47 | #include "iceberg/schema_util.h" |
44 | 48 | #include "iceberg/test/matchers.h" |
45 | 49 | #include "iceberg/type.h" |
| 50 | +#include "iceberg/util/string_util.h" |
46 | 51 |
|
47 | 52 | namespace iceberg::avro { |
48 | 53 |
|
| 54 | +class AvroDecimalEncodingTest : public ::testing::TestWithParam<bool> {}; |
| 55 | + |
| 56 | +TEST_P(AvroDecimalEncodingTest, FixedWidthBigEndianBytes) { |
| 57 | + struct TestCase { |
| 58 | + int32_t precision; |
| 59 | + std::string unscaled; |
| 60 | + std::string expected_hex; |
| 61 | + }; |
| 62 | + const std::vector<TestCase> cases{ |
| 63 | + {.precision = 1, .unscaled = "0", .expected_hex = "00"}, |
| 64 | + {.precision = 1, .unscaled = "1", .expected_hex = "01"}, |
| 65 | + {.precision = 1, .unscaled = "-1", .expected_hex = "ff"}, |
| 66 | + {.precision = 2, .unscaled = "99", .expected_hex = "63"}, |
| 67 | + {.precision = 2, .unscaled = "-99", .expected_hex = "9d"}, |
| 68 | + {.precision = 3, .unscaled = "127", .expected_hex = "007f"}, |
| 69 | + {.precision = 3, .unscaled = "128", .expected_hex = "0080"}, |
| 70 | + {.precision = 3, .unscaled = "-128", .expected_hex = "ff80"}, |
| 71 | + {.precision = 3, .unscaled = "-129", .expected_hex = "ff7f"}, |
| 72 | + {.precision = 5, .unscaled = "32767", .expected_hex = "007fff"}, |
| 73 | + {.precision = 5, .unscaled = "32768", .expected_hex = "008000"}, |
| 74 | + {.precision = 5, .unscaled = "-32768", .expected_hex = "ff8000"}, |
| 75 | + {.precision = 5, .unscaled = "-32769", .expected_hex = "ff7fff"}, |
| 76 | + {.precision = 10, .unscaled = "1001", .expected_hex = "00000003e9"}, |
| 77 | + {.precision = 10, .unscaled = "-1001", .expected_hex = "fffffffc17"}, |
| 78 | + {.precision = 38, |
| 79 | + .unscaled = "0", |
| 80 | + .expected_hex = "00000000000000000000000000000000"}, |
| 81 | + {.precision = 38, |
| 82 | + .unscaled = "1", |
| 83 | + .expected_hex = "00000000000000000000000000000001"}, |
| 84 | + {.precision = 38, |
| 85 | + .unscaled = "-1", |
| 86 | + .expected_hex = "ffffffffffffffffffffffffffffffff"}, |
| 87 | + {.precision = 38, |
| 88 | + .unscaled = "128", |
| 89 | + .expected_hex = "00000000000000000000000000000080"}, |
| 90 | + {.precision = 38, |
| 91 | + .unscaled = "-129", |
| 92 | + .expected_hex = "ffffffffffffffffffffffffffffff7f"}, |
| 93 | + {.precision = 38, |
| 94 | + .unscaled = "1208925819614629174706299", |
| 95 | + .expected_hex = "0000000000010000000000000000007b"}, |
| 96 | + {.precision = 38, |
| 97 | + .unscaled = "-1208925819614629174706299", |
| 98 | + .expected_hex = "fffffffffffeffffffffffffffffff85"}, |
| 99 | + {.precision = 38, |
| 100 | + .unscaled = "99999999999999999999999999999999999999", |
| 101 | + .expected_hex = "4b3b4ca85a86c47a098a223fffffffff"}, |
| 102 | + {.precision = 38, |
| 103 | + .unscaled = "-99999999999999999999999999999999999999", |
| 104 | + .expected_hex = "b4c4b357a5793b85f675ddc000000001"}, |
| 105 | + }; |
| 106 | + for (const auto& test : cases) { |
| 107 | + SCOPED_TRACE(test.unscaled); |
| 108 | + auto type = decimal(test.precision, 0); |
| 109 | + ::avro::NodePtr node; |
| 110 | + ASSERT_THAT(ToAvroNodeVisitor{}.Visit(*type, &node), IsOk()); |
| 111 | + ICEBERG_UNWRAP_OR_FAIL(auto expected, |
| 112 | + StringUtils::HexStringToBytes(test.expected_hex)); |
| 113 | + ASSERT_EQ(node->fixedSize(), expected.size()); |
| 114 | + ::arrow::Decimal128Builder builder(::arrow::decimal128(test.precision, 0), |
| 115 | + ::arrow::default_memory_pool()); |
| 116 | + ASSERT_TRUE(builder.Append(::arrow::Decimal128(test.unscaled)).ok()); |
| 117 | + auto array = builder.Finish().ValueOrDie(); |
| 118 | + auto stream = ::avro::memoryOutputStream(); |
| 119 | + auto encoder = ::avro::binaryEncoder(); |
| 120 | + encoder->init(*stream); |
| 121 | + if (GetParam()) { |
| 122 | + EncodeContext context; |
| 123 | + ASSERT_THAT(EncodeArrowToAvro(node, *encoder, *type, *array, 0, context), IsOk()); |
| 124 | + } else { |
| 125 | + ::avro::GenericDatum datum(node); |
| 126 | + ASSERT_THAT(ExtractDatumFromArray(*array, 0, &datum), IsOk()); |
| 127 | + EXPECT_EQ(datum.value<::avro::GenericFixed>().value(), expected); |
| 128 | + ::avro::encode(*encoder, datum); |
| 129 | + } |
| 130 | + encoder->flush(); |
| 131 | + EXPECT_EQ(*::avro::snapshot(*stream), expected); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +TEST_P(AvroDecimalEncodingTest, RejectsFixedWidthOverflow) { |
| 136 | + auto type = decimal(2, 0); |
| 137 | + ::avro::NodePtr node; |
| 138 | + ASSERT_THAT(ToAvroNodeVisitor{}.Visit(*type, &node), IsOk()); |
| 139 | + ASSERT_EQ(node->fixedSize(), 1); |
| 140 | + // Arrow's builder accepts these invalid values; do not silently truncate them. |
| 141 | + for (const auto& unscaled : {"128", "-129", "256", "-257"}) { |
| 142 | + SCOPED_TRACE(unscaled); |
| 143 | + ::arrow::Decimal128Builder builder(::arrow::decimal128(2, 0), |
| 144 | + ::arrow::default_memory_pool()); |
| 145 | + ASSERT_TRUE(builder.Append(::arrow::Decimal128(std::string(unscaled))).ok()); |
| 146 | + auto array = builder.Finish().ValueOrDie(); |
| 147 | + auto stream = ::avro::memoryOutputStream(); |
| 148 | + auto encoder = ::avro::binaryEncoder(); |
| 149 | + encoder->init(*stream); |
| 150 | + if (GetParam()) { |
| 151 | + EncodeContext context; |
| 152 | + EXPECT_THAT(EncodeArrowToAvro(node, *encoder, *type, *array, 0, context), |
| 153 | + IsError(ErrorKind::kInvalidArgument)); |
| 154 | + } else { |
| 155 | + ::avro::GenericDatum datum(node); |
| 156 | + EXPECT_THAT(ExtractDatumFromArray(*array, 0, &datum), |
| 157 | + IsError(ErrorKind::kInvalidArgument)); |
| 158 | + } |
| 159 | + encoder->flush(); |
| 160 | + EXPECT_TRUE(::avro::snapshot(*stream)->empty()); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +TEST(AvroDecimalFixedTest, SignedBoundariesAndPadding) { |
| 165 | + std::vector<uint8_t> bytes; |
| 166 | + ASSERT_THAT(EncodeDecimalToFixed(::arrow::Decimal128(127), 1, &bytes), IsOk()); |
| 167 | + EXPECT_EQ(bytes, (std::vector<uint8_t>{0x7f})); |
| 168 | + ASSERT_THAT(EncodeDecimalToFixed(::arrow::Decimal128(-128), 1, &bytes), IsOk()); |
| 169 | + EXPECT_EQ(bytes, (std::vector<uint8_t>{0x80})); |
| 170 | + ASSERT_THAT(EncodeDecimalToFixed(::arrow::Decimal128(1), 17, &bytes), IsOk()); |
| 171 | + ICEBERG_UNWRAP_OR_FAIL( |
| 172 | + auto positive, StringUtils::HexStringToBytes("0000000000000000000000000000000001")); |
| 173 | + EXPECT_EQ(bytes, positive); |
| 174 | + ASSERT_THAT(EncodeDecimalToFixed(::arrow::Decimal128(-1), 17, &bytes), IsOk()); |
| 175 | + ICEBERG_UNWRAP_OR_FAIL( |
| 176 | + auto negative, StringUtils::HexStringToBytes("ffffffffffffffffffffffffffffffffff")); |
| 177 | + EXPECT_EQ(bytes, negative); |
| 178 | + EXPECT_THAT(EncodeDecimalToFixed(::arrow::Decimal128(0), 0, &bytes), |
| 179 | + IsError(ErrorKind::kInvalidArgument)); |
| 180 | +} |
| 181 | + |
| 182 | +INSTANTIATE_TEST_SUITE_P(WriterBackends, AvroDecimalEncodingTest, |
| 183 | + ::testing::Values(true, false), |
| 184 | + [](const ::testing::TestParamInfo<bool>& info) { |
| 185 | + return info.param ? "DirectEncoder" : "GenericDatum"; |
| 186 | + }); |
| 187 | + |
49 | 188 | /// \brief Test case structure for parameterized primitive type tests |
50 | 189 | struct AppendDatumParam { |
51 | 190 | std::string name; |
|
0 commit comments