bug: `PARTITION BY (to_date(ts))` stores partition values in the writer's time zone but prunes with the reader's, so rows written from a UTC session are missing from filtered queries in an Asia/Shanghai session and survive a matching DELETE

Open
#21,165 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@KKould is already working on this.

Since Nov 23, 2026.

  • #21176 by @KKould — closed without merging
  • #21212 by @KKould — open

Assessment

This issue has not been assessed yet.

Description

C-bug
Search before asking
  • I had searched in the issues and found no similar issues.
Version

main 057efa66e6 (v1.2.961-057efa66e6, nightly image of 2026-11-08); also v1.2.960-nightly-3c9357c866.

What's Wrong?

A fuse table partitioned by a time-zone dependent expression (PARTITION BY (to_date(ts)), (to_yyyymm(ts)),
(date_trunc(day, ts))) computes the partition value of each row with the writing session's time zone and
stores it in the segment's partition statistics. Partition pruning folds the partition expression over the
reading session's filter with the reader's time zone and compares with the stored values. When the two
sessions use different timezone settings, blocks holding matching rows are pruned:

  • a filtered SELECT silently misses rows (count_if() of the same predicate, which is not pruned, finds them);
  • a DELETE (or UPDATE) with the predicate skips them: the rows stay although they match.

Sessions with the same time zone are consistent; CLUSTER BY (to_date(ts)) is not affected (cluster statistics
already fall back for context-dependent keys). #20595 rejects explicit context functions (timezone(),
current_user(), …) in cluster/partition keys and documents that "implicit timezone/other settings dependencies
of ordinary functions and casts remain a separate unresolved issue requiring coordinated definition/runtime
changes"; to_date(ts) is such an implicit dependency, and this is what it does to query and DML results.

Cause: on write, the partition key is evaluated by the cluster-statistics generator built with the writing
session's function context
(append.rs#L469-L478),
and the value is stored as the segment's partition statistics. On read, PartitionPruner::try_create(func_ctx, …)
(fuse_pruner.rs#L158-L162)
folds the partition expressions and projects the filter with the reading session's FunctionContext. Nothing ties
the two contexts together: analyze_table_keys
(ddl/table.rs#L2888)
accepts any deterministic expression, including time-zone dependent ones, and the table does not record the time
zone its partition values were computed in. Possible fixes: evaluate partition keys in a fixed context recorded in
the table (e.g. UTC, or the definer's time zone) on both write and read, or reject time-zone dependent partition
keys, or skip partition pruning when the reader's context differs from the writer's.

How to Reproduce?
-- session A
SET timezone = 'UTC';
CREATE TABLE events (id INT, ts TIMESTAMP) PARTITION BY (to_date(ts));
INSERT INTO events VALUES (1, '2024-01-31 20:00:00'), (2, '2024-01-31 10:00:00'), (3, '2024-02-01 10:00:00');

-- session B (row 1 is 2024-02-01 04:00 in Shanghai)
SET timezone = 'Asia/Shanghai';
SELECT id, ts, to_date(ts) FROM events ORDER BY id;
SELECT id FROM events WHERE to_date(ts) = '2024-02-01' ORDER BY id;
SELECT count_if(to_date(ts) = '2024-02-01') FROM events;
SELECT id FROM events WHERE ts >= '2024-02-01 00:00:00' AND ts < '2024-02-02 00:00:00' ORDER BY id;
SELECT id FROM events WHERE ts = '2024-02-01 04:00:00';
DELETE FROM events WHERE to_date(ts) = '2024-02-01';
SELECT id, ts, to_date(ts) FROM events ORDER BY id;

Result on main 057efa66e6 (session B):

1 | 2024-02-01 04:00:00.000000 | 2024-02-01
2 | 2024-01-31 18:00:00.000000 | 2024-01-31
3 | 2024-02-01 18:00:00.000000 | 2024-02-01
WHERE to_date(ts) = '2024-02-01':     3                 <- expected 1, 3
count_if(to_date(ts) = '2024-02-01'): 2
WHERE ts >= '2024-02-01' AND ts < '2024-02-02': 3       <- expected 1, 3
WHERE ts = '2024-02-01 04:00:00':     (no rows)         <- expected 1
DELETE ... WHERE to_date(ts) = '2024-02-01': 1 row deleted
1 | 2024-02-01 04:00:00.000000 | 2024-02-01             <- matches the DELETE predicate but survives
2 | 2024-01-31 18:00:00.000000 | 2024-01-31

Expected: rows 1 and 3 for both range predicates, row 1 for the equality, and the DELETE removes rows 1 and 3.
With PARTITION BY (to_yyyymm(ts)) and PARTITION BY (date_trunc(day, ts)) the to_date(ts) predicate happens to
return rows 1 and 3, but both ts predicates miss row 1 (with date_trunc(day, ts) the range query returns no rows
at all).

Single node, default settings apart from the session time zones, HTTP handler.

Are you willing to submit PR?
  • Yes I am willing to submit a PR!
Dominant language
Rust
Stars
9.5k
Forks
903
Avg merge
2d 13h
Merged PRs (30d)
204

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from databendlabs/databend

All issues in databendlabs/databend

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.