Skip to content

Fix Time64 to Time cast - #111399

Open
yariks5s wants to merge 38 commits into
masterfrom
yarik/fix-time64-to-time-cast
Open

yariks5s wants to merge 38 commits into
masterfrom
yarik/fix-time64-to-time-cast

Conversation

@yariks5s

@yariks5s yariks5s commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

yariks5s added 2 commits July 22, 2026 13:01
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.
@yariks5s yariks5s changed the title Fix time64 to time cast Fix Time64 to Time cast Jul 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [522e9ce]

Summary: ✅


AI Review

Summary

I reviewed the current PR head, the full diff, and all prior review threads. The change now consistently covers Time64 -> Time conversion in regular casts, constant materialization, and IN/set probe handling, including accurate/accurate-or-null behavior and composite probes. I did not find any remaining correctness issues in the current code, and the current CI for this PR is green.

Final Verdict

✅ No remaining review findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 22, 2026
Comment thread src/Functions/FunctionsConversion.h Resolved
Comment thread src/Functions/FunctionsConversion.h Resolved
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.
Comment thread src/Functions/FunctionsConversion.h Resolved
yariks5s added 2 commits July 22, 2026 17:16
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.
Comment thread src/Functions/FunctionsConversion.h Resolved
yariks5s added 3 commits July 22, 2026 21:34
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.
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
…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.
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
yariks5s added 3 commits July 23, 2026 20:55
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.
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
yariks5s added 3 commits July 27, 2026 11:11
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.
Comment thread src/Functions/FunctionsConversion.h Resolved
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.
Comment thread src/Interpreters/Set.cpp Resolved
Comment thread src/Interpreters/Set.cpp Resolved
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.
Comment thread src/Interpreters/Set.cpp Resolved
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.
Comment thread src/Interpreters/Set.cpp Resolved
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.
Comment thread src/Interpreters/Set.cpp Resolved
yariks5s added 3 commits July 29, 2026 13:19
…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.
Comment thread src/Interpreters/Set.cpp Resolved
yariks5s added 5 commits July 29, 2026 15:42
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.
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 285/298 (95.64%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot added comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. and removed comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Nov 19, 2026

@Manerone Manerone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:

  1. Nested Time64 probes in IN throw 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
  2. A Time64 constant inside a Map in VALUES fails (Array and Tuple already 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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Time64 → Time conversion is unsupported, making Arrow files with Time columns unreadable after ClickHouse writes them

2 participants