Repository navigation
Fix Time64 to Time cast - #111399
Fix Time64 to Time cast#111399yariks5s wants to merge 38 commits into
Conversation
The Time64 -> Time conversion branch was never implemented, so any such cast (including implicit ones, e.g. reading a Time column back from an Arrow file that stores it as Time64(0)) failed with CANNOT_CONVERT_TYPE. Add the missing branch: both types store local seconds since midnight, so the conversion just drops the fractional part.
|
Workflow [PR], commit [522e9ce] Summary: ✅
AI ReviewSummaryI reviewed the current PR head, the full diff, and all prior review threads. The change now consistently covers Final Verdict✅ No remaining review findings. |
…nto yarik/fix-time64-to-time-cast
Use the ConvertImpl template parameter instead of the global default so SET date_time_overflow_behavior = 'throw' raises on out-of-range Time64, matching the numeric -> Time casts. Add a throw-mode regression.
Drop the divergent accurate-or-null null-map special case; overflow is governed solely by date_time_overflow_behavior (clamp under ignore/saturate, throw under throw), matching the sibling DateTime64 -> DateTime conversion. accurate/accurateOrNull from a decimal date/time source use the plain path in both cases, so behaviour stays consistent.
convertFieldToType had no Decimal64 -> Time case, so a constant Time64 inserted into a Time column (e.g. INSERT ... VALUES) threw TYPE_MISMATCH. Add it with the same truncate-and-clamp semantics as the Time64 -> Time column conversion, and add a VALUES regression.
…e IN-set elements) convertFieldToType is used with strict=true by IN-set construction to drop lossy elements. The Time64 -> Time branch truncated/clamped before checking strict, so a fractional or out-of-range Time64 would wrongly match. Reject such values when strict, and add an IN regression.
Decimal64 is also the Field storage for Decimal64/DateTime64, so gate the constant Time64->Time path on a Time64 source hint (a DateTime64 constant into a Time column no longer takes the truncate/clamp path). Also honor date_time_overflow_behavior in the constant path so INSERT ... VALUES under 'throw' raises on out-of-range, matching the column conversion. Add regressions.
… be evaluated client-side
Nullable(Tuple(...)) is a valid type, so the tuple hint propagation must look through the wrapper like the scalar Time64 branch does; factor the unwrapping into a shared helper and add a Nullable(Tuple(Time64, ...)) regression.
Probing a Time set with a Time64 column went through the plain truncating cast, so IN (SELECT ...) matched values the literal IN correctly rejects. Reuse the DateTime64 precision-loss null map for Time64 sources.
With transform_null_in the probe reaches Set::execute still wrapped in Nullable, so the column-based sub-second guard was skipped and the value matched after truncation. Key the guard off the unwrapped type (fixes the pre-existing DateTime64 case too) and unwrap the type before reading the scale in checkDateTimePrecision.
Whole-second Time64 values outside the Time range were clamped by the cast and could match set elements the strict literal path rejects. Mark them lossy in the precision null map alongside fractional values.
The internal cast saturates DateTime64 values at the DateTime range boundaries, so an out-of-range probe could match the boundary element (e.g. -1 matched toDateTime(0)). Apply the same whole-second range marking as for Time64, with the DateTime bounds.
…n Set::execute Composite probes bypassed the top-level type guard, so nested sub-second or out-of-range values matched after the lossy element cast while the strict literal path rejects them. Walk the probe and target types together, mark lossy positions, and aggregate element flags to rows via array offsets.
Wrapping the cast result in ColumnNullable fails for composite probes (Array cannot be inside Nullable). Accumulate lossy flags across key columns and merge them into the final null map after extractNestedColumnsAndNullMap instead.
Same-family rescaling truncates like the Field conversion and is not marked; cross-family fractions are lossy only below the target scale (Time64(3) -> DateTime64(3) is exact); whole-second range checks apply only where the cast clamps or saturates (-> DateTime, Time64 -> Time).
Route the pair through the accurate strategies in createWrapper and handle them in the ConvertImpl branch: accurateCast throws CANNOT_CONVERT_TYPE and accurateCastOrNull returns NULL for fractional or out-of-range values, independently of date_time_overflow_behavior.
Restore the original DateTime64 sub-second handling and replace the generalized marking with a dedicated plan: a type-only pre-check accepts only trees whose differing leaves are all Time64 -> Time (through Array/Tuple/Map, Nullable and target LowCardinality), such probes use a plain cast plus a null-aware loss map, everything else keeps the accurate cast path.
Replace the all-or-nothing Time64 plan with recursive temporal preprocessing: Time64 -> Time and DateTime64 -> DateTime leaves are pre-converted with the plain-cast semantics (floor, clamp/saturate) and lossy rows marked, composite columns are rebuilt around them (named tuples matched by name like createTupleWrapper), and the remaining conversions keep accurate semantics. This also covers the DateTime64 saturation boundaries and composite probes, and mixed tuples no longer throw. processDateTime64Column stays for other DateTime64 conversions.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 285/298 (95.64%) · Uncovered code |
Manerone
left a comment
There was a problem hiding this comment.
A lot of the changes in this PR are already covered by master (#106019 and later work on convertFieldToType and Set), and the PR now conflicts with it. The cases still missing on master are:
-
Nested
Time64probes inINthrow instead of returning 0:SELECT [CAST('01:02:03.5' AS Time64(1))] IN (SELECT [CAST('01:02:03' AS Time)]); SELECT map('k', CAST('01:02:03.5' AS Time64(1))) IN (SELECT map('k', CAST('01:02:03' AS Time))); -- Code: 70. DB::Exception: Value 37235 cannot be safely converted into type Time
-
A
Time64constant inside aMapinVALUESfails (ArrayandTuplealready work):CREATE TABLE t (m Map(String, Time)) ENGINE = Memory; INSERT INTO t VALUES (map('k', CAST('01:02:03.5' AS Time64(1)))); -- Code: 53. DB::Exception: Type mismatch in IN or VALUES section. Expected: Time. Got: Decimal64
Could you merge master and reduce the PR to these two cases?
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes cases where casting from Time64 to Time was throwing errors. Closes #111388