|
| 1 | +#include <arrow/api.h> |
| 2 | +#include <arrow/array.h> |
| 3 | + |
| 4 | +#include <library/cpp/testing/unittest/registar.h> |
| 5 | + |
| 6 | +#include "arrow.h" |
| 7 | +#include "arrow_impl.h" |
| 8 | + |
| 9 | +extern "C" { |
| 10 | +#include "utils/fmgrprotos.h" |
| 11 | +} |
| 12 | + |
| 13 | +namespace NYql { |
| 14 | + |
| 15 | +Y_UNIT_TEST_SUITE(TArrowUtilsTests) { |
| 16 | + |
| 17 | +Y_UNIT_TEST(TestPgFloatToNumeric) { |
| 18 | + TArenaMemoryContext arena; |
| 19 | + auto n = PgFloatToNumeric(711.56, 1000000000000LL, 12); |
| 20 | + auto value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); |
| 21 | + UNIT_ASSERT_VALUES_EQUAL(value, "711.56"); |
| 22 | + |
| 23 | + n = PgFloatToNumeric(-711.56, 1000000000000LL, 12); |
| 24 | + value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); |
| 25 | + UNIT_ASSERT_VALUES_EQUAL(value, "-711.56"); |
| 26 | + |
| 27 | + n = PgFloatToNumeric(711.56f, 100000LL, 5); |
| 28 | + value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); |
| 29 | + UNIT_ASSERT_VALUES_EQUAL(value, "711.56"); |
| 30 | + |
| 31 | + n = PgFloatToNumeric(-711.56f, 100000LL, 5); |
| 32 | + value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); |
| 33 | + UNIT_ASSERT_VALUES_EQUAL(value, "-711.56"); |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +Y_UNIT_TEST(PgConvertNumericDouble) { |
| 38 | + TArenaMemoryContext arena; |
| 39 | + |
| 40 | + arrow::DoubleBuilder builder; |
| 41 | + builder.Append(1.1); |
| 42 | + builder.Append(31.37); |
| 43 | + builder.AppendNull(); |
| 44 | + builder.Append(-1.337); |
| 45 | + builder.Append(0.0); |
| 46 | + |
| 47 | + std::shared_ptr<arrow::Array> array; |
| 48 | + builder.Finish(&array); |
| 49 | + |
| 50 | + auto result = PgConvertNumeric<double>(array); |
| 51 | + const auto& data = result->data(); |
| 52 | + |
| 53 | + const char* expected[] = { |
| 54 | + "1.1", "31.37", nullptr, "-1.337", "0" |
| 55 | + }; |
| 56 | + |
| 57 | + NUdf::TStringBlockReader<arrow::BinaryType, true> reader; |
| 58 | + for (int i = 0; i < 5; i++) { |
| 59 | + auto item = reader.GetItem(*data, i); |
| 60 | + if (!item) { |
| 61 | + UNIT_ASSERT(expected[i] == nullptr); |
| 62 | + } else { |
| 63 | + const char* addr = item.AsStringRef().Data() + sizeof(void*); |
| 64 | + UNIT_ASSERT(expected[i] != nullptr); |
| 65 | + UNIT_ASSERT_VALUES_EQUAL( |
| 66 | + TString(DatumGetCString(DirectFunctionCall1(numeric_out, (Datum)addr))), |
| 67 | + expected[i] |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +Y_UNIT_TEST(PgConvertNumericInt) { |
| 74 | + TArenaMemoryContext arena; |
| 75 | + |
| 76 | + arrow::Int64Builder builder; |
| 77 | + builder.Append(11); |
| 78 | + builder.Append(3137); |
| 79 | + builder.AppendNull(); |
| 80 | + builder.Append(-1337); |
| 81 | + builder.Append(0); |
| 82 | + |
| 83 | + std::shared_ptr<arrow::Array> array; |
| 84 | + builder.Finish(&array); |
| 85 | + |
| 86 | + auto result = PgConvertNumeric<i64>(array); |
| 87 | + const auto& data = result->data(); |
| 88 | + |
| 89 | + const char* expected[] = { |
| 90 | + "11", "3137", nullptr, "-1337", "0" |
| 91 | + }; |
| 92 | + |
| 93 | + NUdf::TStringBlockReader<arrow::BinaryType, true> reader; |
| 94 | + for (int i = 0; i < 5; i++) { |
| 95 | + auto item = reader.GetItem(*data, i); |
| 96 | + if (!item) { |
| 97 | + UNIT_ASSERT(expected[i] == nullptr); |
| 98 | + } else { |
| 99 | + const char* addr = item.AsStringRef().Data() + sizeof(void*); |
| 100 | + UNIT_ASSERT(expected[i] != nullptr); |
| 101 | + UNIT_ASSERT_VALUES_EQUAL( |
| 102 | + TString(DatumGetCString(DirectFunctionCall1(numeric_out, (Datum)addr))), |
| 103 | + expected[i] |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +} // Y_UNIT_TEST_SUITE(TArrowUtilsTests) |
| 110 | + |
| 111 | +} // namespace NYql |
| 112 | + |
0 commit comments