-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement double/int -> pg numeric conversion YQL-16767 (#727)
- Loading branch information
Showing
5 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <arrow/array.h> | ||
#include <arrow/array/builder_binary.h> | ||
|
||
extern "C" { | ||
#include "utils/numeric.h" | ||
} | ||
|
||
namespace NYql { | ||
|
||
Numeric PgFloatToNumeric(double item, ui64 scale, int digits); | ||
|
||
template<typename T> | ||
std::shared_ptr<arrow::Array> PgConvertNumeric(const std::shared_ptr<arrow::Array>& value) { | ||
TArenaMemoryContext arena; | ||
const auto& data = value->data(); | ||
size_t length = data->length; | ||
arrow::BinaryBuilder builder; | ||
auto input = data->GetValues<T>(1); | ||
for (size_t i = 0; i < length; ++i) { | ||
if (value->IsNull(i)) { | ||
builder.AppendNull(); | ||
continue; | ||
} | ||
T item = input[i]; | ||
Numeric v; | ||
if constexpr(std::is_same_v<T,double>) { | ||
v = PgFloatToNumeric(item, 1000000000000LL, 12); | ||
} else if constexpr(std::is_same_v<T,float>) { | ||
v = PgFloatToNumeric(item, 1000000LL, 6); | ||
} else { | ||
v = int64_to_numeric(item); | ||
} | ||
auto datum = NumericGetDatum(v); | ||
auto ptr = (char*)datum; | ||
auto len = GetFullVarSize((const text*)datum); | ||
NUdf::ZeroMemoryContext(ptr); | ||
ARROW_OK(builder.Append(ptr - sizeof(void*), len + sizeof(void*))); | ||
} | ||
|
||
std::shared_ptr<arrow::BinaryArray> ret; | ||
ARROW_OK(builder.Finish(&ret)); | ||
return ret; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <arrow/api.h> | ||
#include <arrow/array.h> | ||
|
||
#include <library/cpp/testing/unittest/registar.h> | ||
|
||
#include "arrow.h" | ||
#include "arrow_impl.h" | ||
|
||
extern "C" { | ||
#include "utils/fmgrprotos.h" | ||
} | ||
|
||
namespace NYql { | ||
|
||
Y_UNIT_TEST_SUITE(TArrowUtilsTests) { | ||
|
||
Y_UNIT_TEST(TestPgFloatToNumeric) { | ||
TArenaMemoryContext arena; | ||
auto n = PgFloatToNumeric(711.56, 1000000000000LL, 12); | ||
auto value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); | ||
UNIT_ASSERT_VALUES_EQUAL(value, "711.56"); | ||
|
||
n = PgFloatToNumeric(-711.56, 1000000000000LL, 12); | ||
value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); | ||
UNIT_ASSERT_VALUES_EQUAL(value, "-711.56"); | ||
|
||
n = PgFloatToNumeric(711.56f, 100000LL, 5); | ||
value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); | ||
UNIT_ASSERT_VALUES_EQUAL(value, "711.56"); | ||
|
||
n = PgFloatToNumeric(-711.56f, 100000LL, 5); | ||
value = TString(DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(n)))); | ||
UNIT_ASSERT_VALUES_EQUAL(value, "-711.56"); | ||
} | ||
|
||
|
||
Y_UNIT_TEST(PgConvertNumericDouble) { | ||
TArenaMemoryContext arena; | ||
|
||
arrow::DoubleBuilder builder; | ||
builder.Append(1.1); | ||
builder.Append(31.37); | ||
builder.AppendNull(); | ||
builder.Append(-1.337); | ||
builder.Append(0.0); | ||
|
||
std::shared_ptr<arrow::Array> array; | ||
builder.Finish(&array); | ||
|
||
auto result = PgConvertNumeric<double>(array); | ||
const auto& data = result->data(); | ||
|
||
const char* expected[] = { | ||
"1.1", "31.37", nullptr, "-1.337", "0" | ||
}; | ||
|
||
NUdf::TStringBlockReader<arrow::BinaryType, true> reader; | ||
for (int i = 0; i < 5; i++) { | ||
auto item = reader.GetItem(*data, i); | ||
if (!item) { | ||
UNIT_ASSERT(expected[i] == nullptr); | ||
} else { | ||
const char* addr = item.AsStringRef().Data() + sizeof(void*); | ||
UNIT_ASSERT(expected[i] != nullptr); | ||
UNIT_ASSERT_VALUES_EQUAL( | ||
TString(DatumGetCString(DirectFunctionCall1(numeric_out, (Datum)addr))), | ||
expected[i] | ||
); | ||
} | ||
} | ||
} | ||
|
||
Y_UNIT_TEST(PgConvertNumericInt) { | ||
TArenaMemoryContext arena; | ||
|
||
arrow::Int64Builder builder; | ||
builder.Append(11); | ||
builder.Append(3137); | ||
builder.AppendNull(); | ||
builder.Append(-1337); | ||
builder.Append(0); | ||
|
||
std::shared_ptr<arrow::Array> array; | ||
builder.Finish(&array); | ||
|
||
auto result = PgConvertNumeric<i64>(array); | ||
const auto& data = result->data(); | ||
|
||
const char* expected[] = { | ||
"11", "3137", nullptr, "-1337", "0" | ||
}; | ||
|
||
NUdf::TStringBlockReader<arrow::BinaryType, true> reader; | ||
for (int i = 0; i < 5; i++) { | ||
auto item = reader.GetItem(*data, i); | ||
if (!item) { | ||
UNIT_ASSERT(expected[i] == nullptr); | ||
} else { | ||
const char* addr = item.AsStringRef().Data() + sizeof(void*); | ||
UNIT_ASSERT(expected[i] != nullptr); | ||
UNIT_ASSERT_VALUES_EQUAL( | ||
TString(DatumGetCString(DirectFunctionCall1(numeric_out, (Datum)addr))), | ||
expected[i] | ||
); | ||
} | ||
} | ||
} | ||
|
||
} // Y_UNIT_TEST_SUITE(TArrowUtilsTests) | ||
|
||
} // namespace NYql | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters