Skip to content
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

Add support for decimal types #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C"
#include "utils/memutils.h"
#include "utils/memdebug.h"
#include "utils/timestamp.h"
#include "utils/numeric.h"
}

#if PG_VERSION_NUM < 130000
Expand Down Expand Up @@ -69,6 +70,8 @@ to_postgres_type(int arrow_type)
return TIMESTAMPOID;
case arrow::Type::DATE32:
return DATEOID;
case arrow::Type::DECIMAL:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be DECIMAL128 and DECIMAL256 used here similar to bytes_to_postgres_type?

return NUMERICOID;
default:
return InvalidOid;
}
Expand Down Expand Up @@ -114,6 +117,18 @@ bytes_to_postgres_type(const char *bytes, Size len, const arrow::DataType *arrow
case arrow::Type::DATE32:
return DateADTGetDatum(*(int32 *) bytes +
(UNIX_EPOCH_JDATE - POSTGRES_EPOCH_JDATE));
case arrow::Type::DECIMAL128: {
auto dectype = (arrow::Decimal128Type *)arrow_type;
std::string val = arrow::Decimal128(bytes).ToString(dectype->scale());

return NumericGetDatum(DirectFunctionCall1(numeric_in, CStringGetDatum(val.c_str())));
}
case arrow::Type::DECIMAL256: {
auto dectype = (arrow::Decimal256Type *)arrow_type;
std::string val = arrow::Decimal256(bytes).ToString(dectype->scale());

return NumericGetDatum(DirectFunctionCall1(numeric_in, CStringGetDatum(val.c_str())));
}
default:
return PointerGetDatum(NULL);
}
Expand Down
16 changes: 16 additions & 0 deletions src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,22 @@ Datum ParquetReader::read_primitive_type(arrow::Array *array,
res = DateADTGetDatum(d + (UNIX_EPOCH_JDATE - POSTGRES_EPOCH_JDATE));
break;
}
case arrow::Type::DECIMAL128:
{
arrow::Decimal128Array *decarray = (arrow::Decimal128Array *) array;
std::string val = decarray->FormatValue(i);

res = NumericGetDatum(DirectFunctionCall1(numeric_in, CStringGetDatum(val.c_str())));
break;
}
case arrow::Type::DECIMAL256:
{
arrow::Decimal256Array *decarray = (arrow::Decimal256Array *) array;
std::string val = decarray->FormatValue(i);

res = NumericGetDatum(DirectFunctionCall1(numeric_in, CStringGetDatum(val.c_str())));
break;
}
/* TODO: add other types */
default:
throw Error("parquet_fdw: unsupported column type: %s",
Expand Down