-
Notifications
You must be signed in to change notification settings - Fork 699
Implement double/int -> pg numeric conversion YQL-16767 #727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8303fa3
Implement double/int -> pg numeric conversion YQL-16767
resetius 461f696
Polish
resetius 6c5c7d4
Polish
resetius 8d67e08
Polish
resetius 39fc63d
Add tests
resetius 8898b56
Polish
resetius 8d7b46d
Polish
resetius 3c859b9
Polish
resetius fd9800c
Polish
resetius b33422f
Polish
resetius 692dccd
Polish
resetius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
#pragma once | ||
|
||
#include <arrow/array.h> | ||
#include <arrow/array/builder_binary.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 hidden or 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" | ||
|
||
extern "C" { | ||
#include "utils/numeric.h" | ||
#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] | ||
resetius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
} | ||
|
||
} // Y_UNIT_TEST_SUITE(TArrowUtilsTests) | ||
|
||
} // namespace NYql | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.