From b58954c576e6440a1f66cfe45133a317ff8a29c0 Mon Sep 17 00:00:00 2001 From: Ivan Chesnov Date: Mon, 30 Nov 2026 17:01:07 +0200 Subject: [PATCH 1/2] GH-55860: [C++][FlightRPC][ODBC] Fix TIME to TIMESTAMP conversion ignoring the time unit ### Rationale for this change When a TIME column is fetched as a timestamp through the Flight SQL ODBC driver, the converter in `GetConverter` (`odbc_impl/util.cc`) assumes a fixed unit instead of reading it from the array type: * `time64` is always treated as nanoseconds, so `time64[us]` values come back 1000x too small. For example, `12:34:56.123456` is returned as `00:00:45.296123`. * `time32` is always treated as milliseconds, so `time32[s]` values are also wrong. The result is silently wrong data, with no error. Only `time64[ns]` and `time32[ms]` are converted correctly. ### What changes are included in this PR? * The separate TIME32 and TIME64 to TIMESTAMP branches in `GetConverter` are replaced by one helper, `ConvertTimeToTodayTimestamp`, which reads the unit from the array's `TimeType`. * The helper adds today's date in that unit and casts to `timestamp(unit)`, so the result keeps the precision of the input. * Behaviour for `time64[ns]` and `time32[ms]` is unchanged. ### Are these changes tested? Yes. `util_test.cc` now has one conversion test per unit (`time32[s]`, `time32[ms]`, `time64[us]`, `time64[ns]`), replacing the two previous tests that covered only milliseconds and nanoseconds. Each test also checks that the resulting timestamp has the same unit as the input. ### Are there any user-facing changes? Yes. ODBC clients now get correct values for `time64[us]` and `time32[s]` columns. There are no API changes. **This PR contains a "Critical Fix".** Before this change the driver returned incorrect TIME values for `time64[us]` and `time32[s]` columns without reporting an error. * GitHub Issue: #55860 --- .../arrow/flight/sql/odbc/odbc_impl/util.cc | 84 +++++++++-------- .../flight/sql/odbc/odbc_impl/util_test.cc | 90 ++++++++++--------- 2 files changed, 87 insertions(+), 87 deletions(-) diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc index fe6a2d4d7c38..354d855bac38 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc @@ -26,6 +26,7 @@ #include "arrow/compute/api.h" #include "arrow/type.h" #include "arrow/type_fwd.h" +#include "arrow/util/checked_cast.h" #include "arrow/flight/sql/odbc/odbc_impl/json_converter.h" @@ -906,6 +907,42 @@ std::shared_ptr CheckConversion(const arrow::Result& result) { } } +namespace { +int64_t UnitsPerSecond(TimeUnit::type unit) { + switch (unit) { + case TimeUnit::SECOND: + return 1; + case TimeUnit::MILLI: + return 1000; + case TimeUnit::MICRO: + return 1000000; + case TimeUnit::NANO: + return 1000000000; + } + throw DriverException("Unrecognized time unit"); +} + +// Converts a time32/time64 array to a timestamp array of the same unit, holding +// today's date plus the time of day. +std::shared_ptr ConvertTimeToTodayTimestamp( + const std::shared_ptr& original_array) { + const auto unit = + arrow::internal::checked_cast(*original_array->type()).unit(); + + std::shared_ptr as_int = original_array; + if (original_array->type_id() == Type::TIME32) { + as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int32())); + } + as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int64())); + + auto shifted = CheckConversion(arrow::compute::Add( + as_int, + std::make_shared(GetTodayTimeFromEpoch() * UnitsPerSecond(unit)))); + + return CheckConversion(arrow::compute::Cast(shifted, arrow::timestamp(unit))); +} +} // namespace + ArrayConvertTask GetConverter(Type::type original_type_id, CDataType target_type) { // The else statement has a convert the works for the most case of array // conversion. In case, we find conversion that the default one can't handle @@ -923,50 +960,9 @@ ArrayConvertTask GetConverter(Type::type original_type_id, CDataType target_type return CheckConversion( arrow::compute::CallFunction("cast", {first_converted_array}, &cast_options)); }; - } else if (original_type_id == Type::TIME32 && target_type == CDataType_TIMESTAMP) { - return [=](const std::shared_ptr& original_array) { - arrow::compute::CastOptions cast_options; - cast_options.to_type = arrow::int32(); - - auto first_converted_array = - CheckConversion(arrow::compute::Cast(original_array, cast_options)); - - cast_options.to_type = arrow::int64(); - - auto second_converted_array = - CheckConversion(arrow::compute::Cast(first_converted_array, cast_options)); - - auto seconds_from_epoch = GetTodayTimeFromEpoch(); - - auto third_converted_array = CheckConversion( - arrow::compute::Add(second_converted_array, - std::make_shared(seconds_from_epoch * 1000))); - - arrow::compute::CastOptions cast_options_2; - cast_options_2.to_type = arrow::timestamp(TimeUnit::MILLI); - - return CheckConversion(arrow::compute::Cast(third_converted_array, cast_options_2)); - }; - } else if (original_type_id == Type::TIME64 && target_type == CDataType_TIMESTAMP) { - return [=](const std::shared_ptr& original_array) { - arrow::compute::CastOptions cast_options; - cast_options.to_type = arrow::int64(); - - auto first_converted_array = - CheckConversion(arrow::compute::Cast(original_array, cast_options)); - - auto seconds_from_epoch = GetTodayTimeFromEpoch(); - - auto second_converted_array = CheckConversion(arrow::compute::Add( - first_converted_array, - std::make_shared(seconds_from_epoch * 1000000000))); - - arrow::compute::CastOptions cast_options_2; - cast_options_2.to_type = arrow::timestamp(TimeUnit::NANO); - - return CheckConversion( - arrow::compute::Cast(second_converted_array, cast_options_2)); - }; + } else if ((original_type_id == Type::TIME32 || original_type_id == Type::TIME64) && + target_type == CDataType_TIMESTAMP) { + return ConvertTimeToTodayTimestamp; } else if (original_type_id == Type::STRING && target_type == CDataType_DATE) { return [=](const std::shared_ptr& original_array) { // The Strptime requires a date format. Using the ISO 8601 format diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc index bfcec15b4dac..7930b94dcb86 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc @@ -23,6 +23,7 @@ #include "arrow/testing/builder.h" #include "arrow/testing/gtest_util.h" #include "arrow/testing/util.h" +#include "arrow/util/checked_cast.h" #include "gtest/gtest.h" namespace arrow::flight::sql::odbc { @@ -65,64 +66,67 @@ void TestArrayConversion(const std::vector& input, AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type); } -void TestTime32ArrayConversion(const std::vector& input, - const std::shared_ptr& expected_array, - CDataType c_type, Type::type arrow_type) { - std::shared_ptr original_array; - ArrayFromVector(time32(TimeUnit::MILLI), input, &original_array); - - auto converted_array = convertArray(original_array, c_type); - - AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type); +int64_t UnitsPerSecond(TimeUnit::type unit) { + switch (unit) { + case TimeUnit::SECOND: + return 1; + case TimeUnit::MILLI: + return 1000; + case TimeUnit::MICRO: + return 1000000; + case TimeUnit::NANO: + return 1000000000; + } + return 0; } -void TestTime64ArrayConversion(const std::vector& input, - const std::shared_ptr& expected_array, - CDataType c_type, Type::type arrow_type) { - std::shared_ptr original_array; - ArrayFromVector(time64(TimeUnit::NANO), input, &original_array); - - auto converted_array = convertArray(original_array, c_type); - - AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type); -} +// The converted timestamp must keep the unit of the time array: today's date plus +// the time of day, with the input values otherwise untouched. +template +void TestTimeToTimestampConversion( + const std::shared_ptr& time_type, + const std::vector& input) { + const auto unit = arrow::internal::checked_cast(*time_type).unit(); -TEST(Utils, Time32ToTimeStampArray) { - std::vector input_data = {14896, 17820}; + std::shared_ptr original_array; + ArrayFromVector(time_type, input, + &original_array); const auto seconds_from_epoch = GetTodayTimeFromEpoch(); std::vector expected_data; - expected_data.reserve(2); - - for (const auto& item : input_data) { - expected_data.emplace_back(item + seconds_from_epoch * 1000); + expected_data.reserve(input.size()); + for (const auto& item : input) { + expected_data.emplace_back(item + seconds_from_epoch * UnitsPerSecond(unit)); } std::shared_ptr expected; - auto timestamp_field = field("timestamp_field", timestamp(TimeUnit::MILLI)); - ArrayFromVector(timestamp_field->type(), expected_data, - &expected); + ArrayFromVector(timestamp(unit), expected_data, &expected); - TestTime32ArrayConversion(input_data, expected, CDataType_TIMESTAMP, Type::TIMESTAMP); -} + auto converted_array = convertArray(original_array, CDataType_TIMESTAMP); -TEST(Utils, Time64ToTimeStampArray) { - std::vector input_data = {1579489200000, 1646881200000}; + ASSERT_TRUE(converted_array->type()->Equals(*timestamp(unit))) + << converted_array->type()->ToString(); + AssertConvertedArray(expected, converted_array, input.size(), Type::TIMESTAMP); +} - const auto seconds_from_epoch = GetTodayTimeFromEpoch(); - std::vector expected_data; - expected_data.reserve(2); +// 12:34:56 and 23:59:59, with as many fractional digits as the unit allows. +TEST(Utils, Time32SecondsToTimeStampArray) { + TestTimeToTimestampConversion(time32(TimeUnit::SECOND), {45296, 86399}); +} - for (const auto& item : input_data) { - expected_data.emplace_back(item + seconds_from_epoch * 1000000000); - } +TEST(Utils, Time32MillisToTimeStampArray) { + TestTimeToTimestampConversion(time32(TimeUnit::MILLI), + {45296123, 86399999}); +} - std::shared_ptr expected; - auto timestamp_field = field("timestamp_field", timestamp(TimeUnit::NANO)); - ArrayFromVector(timestamp_field->type(), expected_data, - &expected); +TEST(Utils, Time64MicrosToTimeStampArray) { + TestTimeToTimestampConversion(time64(TimeUnit::MICRO), + {45296123456, 86399999999}); +} - TestTime64ArrayConversion(input_data, expected, CDataType_TIMESTAMP, Type::TIMESTAMP); +TEST(Utils, Time64NanosToTimeStampArray) { + TestTimeToTimestampConversion(time64(TimeUnit::NANO), + {45296123456789, 86399999999999}); } TEST(Utils, StringToDateArray) { From 9eca8b3c8a2d83306cea9dcbb333ee4cd780acb5 Mon Sep 17 00:00:00 2001 From: Ivan Chesnov Date: Wed, 2 Dec 2026 21:06:39 +0200 Subject: [PATCH 2/2] Reuse arrow::util::GetTimestampConversion instead of a local UnitsPerSecond helper Co-Authored-By: Claude Fable 5.2 --- .../arrow/flight/sql/odbc/odbc_impl/util.cc | 20 ++++--------------- .../flight/sql/odbc/odbc_impl/util_test.cc | 19 ++++-------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc index 354d855bac38..b9df09751953 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc @@ -27,6 +27,7 @@ #include "arrow/type.h" #include "arrow/type_fwd.h" #include "arrow/util/checked_cast.h" +#include "arrow/util/time.h" #include "arrow/flight/sql/odbc/odbc_impl/json_converter.h" @@ -908,20 +909,6 @@ std::shared_ptr CheckConversion(const arrow::Result& result) { } namespace { -int64_t UnitsPerSecond(TimeUnit::type unit) { - switch (unit) { - case TimeUnit::SECOND: - return 1; - case TimeUnit::MILLI: - return 1000; - case TimeUnit::MICRO: - return 1000000; - case TimeUnit::NANO: - return 1000000000; - } - throw DriverException("Unrecognized time unit"); -} - // Converts a time32/time64 array to a timestamp array of the same unit, holding // today's date plus the time of day. std::shared_ptr ConvertTimeToTodayTimestamp( @@ -935,9 +922,10 @@ std::shared_ptr ConvertTimeToTodayTimestamp( } as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int64())); + const int64_t units_per_second = + arrow::util::GetTimestampConversion(TimeUnit::SECOND, unit).second; auto shifted = CheckConversion(arrow::compute::Add( - as_int, - std::make_shared(GetTodayTimeFromEpoch() * UnitsPerSecond(unit)))); + as_int, std::make_shared(GetTodayTimeFromEpoch() * units_per_second))); return CheckConversion(arrow::compute::Cast(shifted, arrow::timestamp(unit))); } diff --git a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc index 7930b94dcb86..cc88b553f476 100644 --- a/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc +++ b/cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc @@ -24,6 +24,7 @@ #include "arrow/testing/gtest_util.h" #include "arrow/testing/util.h" #include "arrow/util/checked_cast.h" +#include "arrow/util/time.h" #include "gtest/gtest.h" namespace arrow::flight::sql::odbc { @@ -66,20 +67,6 @@ void TestArrayConversion(const std::vector& input, AssertConvertedArray(expected_array, converted_array, input.size(), arrow_type); } -int64_t UnitsPerSecond(TimeUnit::type unit) { - switch (unit) { - case TimeUnit::SECOND: - return 1; - case TimeUnit::MILLI: - return 1000; - case TimeUnit::MICRO: - return 1000000; - case TimeUnit::NANO: - return 1000000000; - } - return 0; -} - // The converted timestamp must keep the unit of the time array: today's date plus // the time of day, with the input values otherwise untouched. template @@ -93,10 +80,12 @@ void TestTimeToTimestampConversion( &original_array); const auto seconds_from_epoch = GetTodayTimeFromEpoch(); + const int64_t units_per_second = + arrow::util::GetTimestampConversion(TimeUnit::SECOND, unit).second; std::vector expected_data; expected_data.reserve(input.size()); for (const auto& item : input) { - expected_data.emplace_back(item + seconds_from_epoch * UnitsPerSecond(unit)); + expected_data.emplace_back(item + seconds_from_epoch * units_per_second); } std::shared_ptr expected;