From 01d350c8bd732967e3d35fd7470603b455ec8508 Mon Sep 17 00:00:00 2001 From: Yashwant Bezawada Date: Sat, 27 Dec 2025 03:14:16 -0600 Subject: [PATCH] Fix Arrow decimal type conversion to float instead of integer Arrow Decimal128 columns were being incorrectly converted to integers, causing values like 3.14 to display as their internal representation (e.g., 314) instead of the actual decimal value. This change: - Maps "decimal" and "decimal128" Arrow types to DTYPE_FLOAT64 instead of DTYPE_INT64 in convert_type() - Uses Decimal128::ToDouble(scale) to properly convert decimal values to floating-point, respecting the decimal scale - Updates tests to expect "float" schema type for decimal columns Fixes #2582 Signed-off-by: Yashwant Bezawada --- .../tests/table/test_table_arrow.py | 4 ++-- .../cpp/perspective/src/cpp/arrow_loader.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rust/perspective-python/perspective/tests/table/test_table_arrow.py b/rust/perspective-python/perspective/tests/table/test_table_arrow.py index fa57e2f038..743bf9b99e 100644 --- a/rust/perspective-python/perspective/tests/table/test_table_arrow.py +++ b/rust/perspective-python/perspective/tests/table/test_table_arrow.py @@ -185,7 +185,7 @@ def test_table_arrow_loads_decimal_stream(self, util): tbl = Table(arrow_data) assert tbl.size() == 10 assert tbl.schema() == { - "a": "integer", + "a": "float", } assert tbl.view().to_columns() == {"a": data[0]} @@ -389,7 +389,7 @@ def test_table_arrow_loads_decimal128_legacy(self, util): tbl = Table(arrow_data) assert tbl.size() == 10 assert tbl.schema() == { - "a": "integer", + "a": "float", } assert tbl.view().to_columns() == {"a": data[0]} diff --git a/rust/perspective-server/cpp/perspective/src/cpp/arrow_loader.cpp b/rust/perspective-server/cpp/perspective/src/cpp/arrow_loader.cpp index d6ef38d572..3b3e2257e5 100644 --- a/rust/perspective-server/cpp/perspective/src/cpp/arrow_loader.cpp +++ b/rust/perspective-server/cpp/perspective/src/cpp/arrow_loader.cpp @@ -170,9 +170,12 @@ convert_type(const std::string& src) { if (src == "uint64") { return DTYPE_UINT64; } - if (src == "decimal" || src == "decimal128" || src == "int64") { + if (src == "int64") { return DTYPE_INT64; } + if (src == "decimal" || src == "decimal128") { + return DTYPE_FLOAT64; + } if (src == "float") { return DTYPE_FLOAT32; } @@ -750,16 +753,13 @@ copy_array( case arrow::Decimal128Type::type_id: case arrow::DecimalType::type_id: { std::shared_ptr scol = - std::static_pointer_cast(src); + std::static_pointer_cast(src); + auto decimal_type = + std::static_pointer_cast(src->type()); + int32_t scale = decimal_type->scale(); auto* vals = (arrow::Decimal128*)scol->raw_values(); for (uint32_t i = 0; i < len; ++i) { - arrow::Status status = - vals[i].ToInteger(dest->get_nth(offset + i)); - if (!status.ok()) { - PSP_COMPLAIN_AND_ABORT( - "Could not write Decimal to column: " + status.message() - ); - }; + dest->set_nth(offset + i, vals[i].ToDouble(scale)); } } break; case arrow::BooleanType::type_id: {