Skip to content

Make Time[64] respect timezones while converting from DateTime[64]#90310

Merged
yariks5s merged 11 commits intomasterfrom
yarik/time-time64-datetime-conversion-respect-timezones
Nov 19, 2025
Merged

Make Time[64] respect timezones while converting from DateTime[64]#90310
yariks5s merged 11 commits intomasterfrom
yarik/time-time64-datetime-conversion-respect-timezones

Conversation

@yariks5s
Copy link
Copy Markdown
Member

@yariks5s yariks5s commented Nov 18, 2025

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):

Time and Time64 now should respect timezones correctly while converting from DateTime and DateTime64 (it must show time in the same timezone as it shows for the user as a DateTime[64]). Closes #89896

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Nov 18, 2025

Workflow [PR], commit [ed5ca9b]

Summary:

job_name test_name status info comment
Stress test (amd_msan) failure
Server died FAIL cidb
Hung check failed, possible deadlock found (see hung_check.log) FAIL cidb
Killed by signal (in clickhouse-server.log) FAIL cidb
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL cidb
Killed by signal (output files) FAIL cidb
Found signal in gdb.log FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb
BuzzHouse (arm_asan) failure
Let op! ERROR cidb
BuzzHouse (amd_ubsan) failure
Let op! ERROR cidb

@clickhouse-gh clickhouse-gh bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 18, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where toTime() and toTime64() functions were not properly respecting timezones when converting from DateTime and DateTime64 types. The fix ensures that the timezone information from the source DateTime type (or session timezone if not explicit) is used to compute the correct local time-of-day value.

Key changes:

  • Modified timezone offset calculation logic to use signed integers and handle negative offsets correctly
  • Added timezone extraction logic to prefer explicit DateTime/DateTime64 timezone over session timezone
  • Added Time64-to-Time64 scale conversion handling

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/Functions/FunctionsConversion.h Updated ToTimeImpl, ToTimeTransformFromDateTime, and ToTime64Transform to correctly compute local seconds-of-day using timezone offsets; added timezone preference logic for DateTime64→Time64 conversion; added Time64→Time64 scale conversion; formatting fixes
src/Functions/DateTimeTransforms.h Added timezone extraction from source DateTime/DateTime64 types when converting to Time/Time64
tests/queries/0_stateless/03365_time64_from_datetime_timezone_respect.sql Test queries covering timezone-aware conversions with explicit and implicit timezones
tests/queries/0_stateless/03365_time64_from_datetime_timezone_respect.reference Expected output for timezone conversion tests
Comments suppressed due to low confidence (1)

src/Functions/FunctionsConversion.h:1728

  • This per-row timezone lookup inside the conversion loop (line 1725) will override the timezone extracted at lines 1718-1723. However, Time64 does not support per-row timezones, so this logic appears incorrect. Either this should be removed, or if an explicit timezone argument is provided (arguments.size() > 2), it should be used instead of extracting from the source type, but applied once before the loop, not per-row.
                if (arguments.size() > 2 && !arguments[2].column.get()->getDataAt(i).toString().empty())
                    time_zone = &DateLUT::instance(arguments[2].column.get()->getDataAt(i).toString());

@yariks5s yariks5s requested a review from Copilot November 19, 2025 14:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (3)

src/Functions/FunctionsConversion.h:1730

  • The timezone lookup via DateLUT::instance() is performed inside the loop for every row, even when argument[2] is a constant column. For constant timezone arguments, this lookup should be hoisted outside the loop to avoid repeated lookups. Consider checking if arguments[2].column is a ColumnConst and extracting the timezone once before the loop.
                if (arguments.size() > 2 && !arguments[2].column.get()->getDataAt(i).toString().empty())
                    time_zone = &DateLUT::instance(arguments[2].column.get()->getDataAt(i).toString());

src/Functions/FunctionsConversion.h:1

  • The scale multiplier calculation on line 1751 occurs inside the loop but produces the same value for all rows. This should be computed once before the loop starts (around line 1707 where the scale is determined) to avoid redundant calculations.
#pragma once

src/Functions/FunctionsConversion.h:1754

  • [nitpick] The comment uses C-style comment syntax ('//'), which is inconsistent with the C++-style comment syntax ('///') used elsewhere in this file for documentation comments (see lines 1758, 240, 401, 681). For consistency with the codebase style, use '///' for documentation comments.
                // Get the UTC seconds (the integer part) from the input value

@yakov-olkhovskiy yakov-olkhovskiy self-assigned this Nov 19, 2025
@yariks5s
Copy link
Copy Markdown
Member Author

Stress test (amd_msan) -- Bug about concat function

BuzzHouse -- not related to current changes

@yariks5s yariks5s enabled auto-merge November 19, 2025 23:29
@yariks5s yariks5s disabled auto-merge November 19, 2025 23:29
@yariks5s yariks5s added this pull request to the merge queue Nov 19, 2025
Merged via the queue into master with commit 6b53b34 Nov 19, 2025
127 of 132 checks passed
@yariks5s yariks5s deleted the yarik/time-time64-datetime-conversion-respect-timezones branch November 19, 2025 23:54
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toTime/toTime64 issues

4 participants