Skip to content

Backport #123796 to 26.8: Derive optimize_and_compare_chain ranges through a DateTime constant compared with DateTime64 - #124065

Merged
clickhouse-gh[bot] merged 2 commits into
26.8from
backport/26.8/123796
Oct 12, 2026
Merged

clickhouse-gh[bot] merged 2 commits into
26.8from
backport/26.8/123796

Conversation

@robot-ch-test-poll2

@robot-ch-test-poll2 robot-ch-test-poll2 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Original pull-request #123796

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 26.8.26.4

…through a `DateTime` constant compared with `DateTime64`
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backport Changes, backported to release branch. Do not use manually - automated use only! label Oct 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [f709699]

Summary: ✅

@yariks5s

yariks5s commented Oct 8, 2026

Copy link
Copy Markdown
Member

@groeneai please investigate what is broken here

…his branch

tryConvertFieldToTypeExact comes from #121078, which is not on this branch.
At this call site only a widening conversion (DateTime or lower-scale
DateTime64 to DateTime64) reaches it, which is exact or overflows to Null,
so the strict tryConvertFieldToType gives the same result.

In 05330, the "implied condition is not duplicated" case compares `a` with a
DateTime64 constant instead of a DateTime one: on this branch the existing
`a < <DateTime>` conjunct hits TYPE_MISMATCH in tryConvertToColumnType
(#117903, fixed on master by #121078), so it is not seen as implying the
derived bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

@yariks5s, the build fails on one missing function: LogicalExpressionOptimizerPass.cpp calls tryConvertFieldToTypeExact, which comes from #121078, and that PR is on neither 26.8 nor 26.9 (#124066 fails the same way). Backporting #121078 itself would not apply cleanly, because it edits Time64 conversion code from #106019, which is also missing on both branches. Details: #123796 (comment)

I pushed a one-commit fix for each branch to my fork. Each is a fast-forward of the robot branch:

git fetch https://github.com/groeneai/ClickHouse fix-backport-26.8-123796 && git push origin FETCH_HEAD:backport/26.8/123796
git fetch https://github.com/groeneai/ClickHouse fix-backport-26.9-123796 && git push origin FETCH_HEAD:backport/26.9/123796

Each commit changes 3 lines:

  • tryConvertFieldToTypeExact(...) becomes the strict tryConvertFieldToType(..., {}, /*strict=*/true). Only a widening conversion reaches this call (a DateTime or lower-scale DateTime64 constant to DateTime64), and that conversion is exact or overflows to Null, so the result matches master.
  • In 05330, the implied condition is not duplicated case now compares a with a DateTime64 constant instead of a DateTime one. Without this change the case prints 0 on both branches: the existing a < <DateTime> conjunct is not recognized there, because its DateTime64 to DateTime round trip throws TYPE_MISMATCH (TYPE_MISMATCH counted in system.errors during key condition analysis (regression in 26.7.1) #117903, fixed on master by Convert a DateTime64 field back to DateTime #121078). The derived bound is then added a second time (4 less nodes instead of master's 3).

Validation: I built both branches with the fix (Debug). 05330 fails on the 0 line with only the first change, and passes with both changes in 50 of 50 randomized-settings runs on each branch.

@yariks5s

yariks5s commented Oct 8, 2026

Copy link
Copy Markdown
Member

@groeneai please fix it

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

@yariks5s, I cannot push to backport/26.8/123796 or backport/26.9/123796: my account has only triage access on this repository, so the fix has to be pushed by someone with write access. It is ready on my fork, and both commits are still fast-forwards of the current heads (e8613aa and 4173f2a):

git fetch https://github.com/groeneai/ClickHouse fix-backport-26.8-123796 && git push origin FETCH_HEAD:backport/26.8/123796
git fetch https://github.com/groeneai/ClickHouse fix-backport-26.9-123796 && git push origin FETCH_HEAD:backport/26.9/123796

@PedroTadim

Copy link
Copy Markdown
Member

@yariks5s groeneai has no permission to commit to public backport PRs

@yariks5s

Copy link
Copy Markdown
Member

@groeneai create a patch that will fix conflicts in this backport

@groeneai

Copy link
Copy Markdown
Collaborator

@yariks5s, there is no merge conflict here: the branch merges cleanly but does not compile, because tryConvertFieldToTypeExact comes from #121078, which is on neither 26.8 nor 26.9. This patch fixes it and applies as is to both backport/26.8/123796 and backport/26.9/123796 (#124066):

diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp
--- a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp
+++ b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp
@@ -2212,7 +2212,7 @@ class LogicalExpressionOptimizerVisitor : public InDepthQueryTreeVisitorWithCont
             converted = tryConvertFieldToType(
                 constant_node->getValue(), *other_type, constant_type.get(), getFormatSettings(getContext()));
         else if (isLowerScaleTimePoint(constant_type, other_type))
-            converted = tryConvertFieldToTypeExact(constant_node->getValue(), *other_type, constant_type.get());
+            converted = tryConvertFieldToType(constant_node->getValue(), *other_type, constant_type.get(), {}, /*strict=*/true);
         if (converted.isNull())
             return {};
 
diff --git a/tests/queries/0_stateless/05330_optimize_and_compare_chain_time_point_constant_scale.sql b/tests/queries/0_stateless/05330_optimize_and_compare_chain_time_point_constant_scale.sql
--- a/tests/queries/0_stateless/05330_optimize_and_compare_chain_time_point_constant_scale.sql
+++ b/tests/queries/0_stateless/05330_optimize_and_compare_chain_time_point_constant_scale.sql
@@ -84,12 +84,12 @@ SELECT 'overflowing constant stays unchained',
 SELECT 'implied condition is not duplicated',
     (SELECT count() FROM (EXPLAIN QUERY TREE
         SELECT * FROM values('a DateTime64(6, \'UTC\'), b DateTime64(6, \'UTC\')', ('2020-01-01 00:00:00', '2020-01-01 00:00:01'))
-        WHERE a < b AND b < toDateTime('2020-01-01 00:00:02', 'UTC') AND a < toDateTime('2020-01-01 00:00:02', 'UTC')
+        WHERE a < b AND b < toDateTime('2020-01-01 00:00:02', 'UTC') AND a < toDateTime64('2020-01-01 00:00:02', 6, 'UTC')
         SETTINGS optimize_and_compare_chain = 1) WHERE explain LIKE '%function_name: less,%')
     =
     (SELECT count() FROM (EXPLAIN QUERY TREE
         SELECT * FROM values('a DateTime64(6, \'UTC\'), b DateTime64(6, \'UTC\')', ('2020-01-01 00:00:00', '2020-01-01 00:00:01'))
-        WHERE a < b AND b < toDateTime('2020-01-01 00:00:02', 'UTC') AND a < toDateTime('2020-01-01 00:00:02', 'UTC')
+        WHERE a < b AND b < toDateTime('2020-01-01 00:00:02', 'UTC') AND a < toDateTime64('2020-01-01 00:00:02', 6, 'UTC')
         SETTINGS optimize_and_compare_chain = 0) WHERE explain LIKE '%function_name: less,%');
 
 -- The derived condition carries a typed constant, which is sent to the shards as is.

The test change is needed because on these branches the DateTime constant in that case hits TYPE_MISMATCH (#117903, fixed on master by #121078). I built both branches with the patch, and the test passed 50 of 50 randomized runs on each.

As a git am-ready file, on either branch:

curl -sSL https://github.com/groeneai/ClickHouse/commit/f70969964ef75c44d5fdedf256933b272b030e55.patch | git am

@clickhouse-gh
clickhouse-gh Bot merged commit c9652db into 26.8 Oct 12, 2026
40 of 41 checks passed
@clickhouse-gh
clickhouse-gh Bot deleted the backport/26.8/123796 branch October 12, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants