-
Notifications
You must be signed in to change notification settings - Fork 82
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
VARINT and numerics type conversion #626
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -334,9 +334,29 @@ ConvertNumeric(const duckdb::Value &ddb_value, idx_t scale, NumericVar &result) | |||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// A wrapper function to convert a string to a numeric value, used for `PostgresFunctionGuard` macro. | ||||||||||||||||||||||||||||||||||||||||||
static Datum | ||||||||||||||||||||||||||||||||||||||||||
ConvertStringToNumerics(Datum str, Datum typelem, Datum typmod) { | ||||||||||||||||||||||||||||||||||||||||||
Datum pg_numeric = DirectFunctionCall3(numeric_in, str, typelem, typmod); | ||||||||||||||||||||||||||||||||||||||||||
return pg_numeric; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
static Datum | ||||||||||||||||||||||||||||||||||||||||||
ConvertNumericDatum(const duckdb::Value &value) { | ||||||||||||||||||||||||||||||||||||||||||
auto value_type_id = value.type().id(); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Special handle duckdb VARINT type. | ||||||||||||||||||||||||||||||||||||||||||
if (value.type().id() == duckdb::LogicalTypeId::VARINT) { | ||||||||||||||||||||||||||||||||||||||||||
// The performant way to handle the translation is to parse VARINT out, here we leverage string conversion and | ||||||||||||||||||||||||||||||||||||||||||
// parsing mainly for code simplicity. | ||||||||||||||||||||||||||||||||||||||||||
const std::string value_str = value.ToString(); | ||||||||||||||||||||||||||||||||||||||||||
const Datum pg_numeric = | ||||||||||||||||||||||||||||||||||||||||||
PostgresFunctionGuard(ConvertStringToNumerics, /*str=*/CStringGetDatum(value_str.c_str()), | ||||||||||||||||||||||||||||||||||||||||||
/*typelen=*/ObjectIdGetDatum(InvalidOid), /*typmod=*/Int32GetDatum(-1)); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes most sense to create a function in Datum
StringToNumeric(char* str) {
// ...
} And then have that function call the actual function with a PostgresFunctionGuard and name that actual function Lines 35 to 54 in a8425cb
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! Updated. |
||||||||||||||||||||||||||||||||||||||||||
return pg_numeric; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Special handle duckdb DOUBLE TYPE. | ||||||||||||||||||||||||||||||||||||||||||
if (value_type_id == duckdb::LogicalTypeId::DOUBLE) { | ||||||||||||||||||||||||||||||||||||||||||
return ConvertDoubleDatum(value); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -1112,6 +1132,8 @@ GetPostgresDuckDBType(const duckdb::LogicalType &type) { | |||||||||||||||||||||||||||||||||||||||||
return NUMERICOID; | ||||||||||||||||||||||||||||||||||||||||||
case duckdb::LogicalTypeId::UUID: | ||||||||||||||||||||||||||||||||||||||||||
return UUIDOID; | ||||||||||||||||||||||||||||||||||||||||||
case duckdb::LogicalTypeId::VARINT: | ||||||||||||||||||||||||||||||||||||||||||
return NUMERICOID; | ||||||||||||||||||||||||||||||||||||||||||
case duckdb::LogicalTypeId::LIST: { | ||||||||||||||||||||||||||||||||||||||||||
const duckdb::LogicalType *duck_type = &type; | ||||||||||||||||||||||||||||||||||||||||||
while (duck_type->id() == duckdb::LogicalTypeId::LIST) { | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise compilation failure