Repository navigation
Conversation
…on 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: apache#55860
|
@xxlaykxx can you fill out the PR description template properly? Thanks. |
done |
|
It's still not correct. |
Should be ok now, sorry |
lidavidm
left a comment
There was a problem hiding this comment.
Looks reasonable.
CC @alinaliBQ too
|
|
||
| std::shared_ptr<Array> as_int = original_array; | ||
| if (original_array->type_id() == Type::TIME32) { | ||
| as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int32())); |
There was a problem hiding this comment.
(We can't directly cast to int64 here?)
There was a problem hiding this comment.
Unfortunately not — there's no time32 -> int64 cast kernel. cast_int64 only registers zero-copy casts from date64, duration, timestamp and time64, while time32 is registered only on cast_int32 (see scalar_cast_numeric.cc), so a direct cast fails with a "not implemented" error. Going through int32 first is zero-copy, so the extra step only costs the widening to int64 that we need anyway for the add. The previous code did the same two-step cast for this reason. I can add a short comment there if you think it helps.
There was a problem hiding this comment.
yea I think adding a short comment here will help.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the unit-aware TIME-to-TIMESTAMP conversion change.
No open issues 🎉
What changed in this PR
Fixes Flight SQL ODBC TIME-to-TIMESTAMP conversion to respect the source time unit, addressing incorrect values for seconds and microseconds reported in #55860.
Changes:
- Consolidates TIME32 and TIME64 conversion into a unit-aware helper.
- Adds tests for all four time units, checking converted values and timestamp precision.
| File | Description |
|---|---|
| cpp/src/arrow/flight/sql/odbc/odbc_impl/util.cc | Uses the source unit for today’s date offset and resulting timestamp. |
| cpp/src/arrow/flight/sql/odbc/odbc_impl/util_test.cc | Covers seconds, milliseconds, microseconds, and nanoseconds. |
Review effort: Balanced
| } | ||
|
|
||
| namespace { | ||
| int64_t UnitsPerSecond(TimeUnit::type unit) { |
There was a problem hiding this comment.
I think you could resuse this existing helper function here and in the test file:
const int64_t units_per_second = arrow::util::GetTimestampConversion(TimeUnit::SECOND, unit).second;
…Second helper Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alinaliBQ
left a comment
There was a problem hiding this comment.
overall lgtm. I have optional suggestions for tests
|
|
||
| std::shared_ptr<Array> as_int = original_array; | ||
| if (original_array->type_id() == Type::TIME32) { | ||
| as_int = CheckConversion(arrow::compute::Cast(as_int, arrow::int32())); |
There was a problem hiding this comment.
yea I think adding a short comment here will help.
| @@ -23,6 +23,8 @@ | |||
| #include "arrow/testing/builder.h" | |||
There was a problem hiding this comment.
(optional) Suggest to add tests for:
- null values
- midnight (value
0) and empty array
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:time64is always treated as nanoseconds, sotime64[us]values come back 1000x too small. For example,12:34:56.123456is returned as00:00:45.296123.time32is always treated as milliseconds, sotime32[s]values are also wrong.The result is silently wrong data, with no error. Only
time64[ns]andtime32[ms]are converted correctly.What changes are included in this PR?
GetConverterare replaced by one helper,ConvertTimeToTodayTimestamp, which reads the unit from the array'sTimeType.timestamp(unit), so the result keeps the precision of the input.time64[ns]andtime32[ms]is unchanged.Are these changes tested?
Yes.
util_test.ccnow 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]andtime32[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]andtime32[s]columns without reporting an error.Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
Used AI coding tools to help draft the implementation and unit tests. I reviewed the diff and verified the tests locally.
PR code and description written by:
Reviewed before submission by: