morningman opened a new issue, #69368:
URL: https://github.com/apache/doris/issues/69368

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### Version
   
   4.1.3 (`doris-4.1.3-rc02-7126cf65d96`, Docker image 
`apache/doris:all-in-one-4.1.3`). Judging from the code, master (`9e60fab4db0`) 
behaves the same.
   
   ### What's Wrong?
   
   On a table partitioned by RANGE on a `DATETIME` column, a filter `ts >= 
UTC_DATE()` reads every partition that has data. These filters read only the 
matching partitions:
   
   - `ts >= CURRENT_DATE()`
   - `ts >= CAST(UTC_TIMESTAMP() AS DATE)`
   - `ts >= UTC_TIMESTAMP() - INTERVAL ...`
   
   The results are correct; only the partition pruning is missing.
   
   `EXPLAIN` shows that FE does not fold `utc_date()` into a literal; it stays 
in the predicate. `current_date()` and `utc_timestamp()` become literals. 
`UTC_TIME()` is not folded either.
   
   The cause seems to be in 
[`DateTimeAcquire`](https://github.com/apache/doris/blob/9e60fab4db00998d490d916c4bf304b15490c1b9/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeAcquire.java#L112-L165),
 which holds the FE implementations used for constant folding:
   
   - It has an `@ExecFunction` for `now`, `current_timestamp`, `curdate`, 
`current_date`, `curtime`, `current_time`, `utc_timestamp`, and others.
   - It has none for `utc_date` or `utc_time`. Those two were added in #57347 
with a BE implementation only.
   
   `SET enable_fold_constant_by_be = true` does not change the plan.
   
   ### What You Expected?
   
   FE should fold `UTC_DATE()`, and `UTC_TIME()` too, like `CURRENT_DATE()` and 
`UTC_TIMESTAMP()`. Then `ts >= UTC_DATE()` reads only the partitions from today 
(UTC) on.
   
   ### How to Reproduce?
   
   One FE and one BE (the all-in-one image), default configuration; session 
`time_zone` is `UTC`. The partitions are relative to today, so this works on 
any day.
   
   ```sql
   CREATE DATABASE IF NOT EXISTS repro;
   USE repro;
   CREATE TABLE events (
     ts DATETIME NOT NULL,
     v INT
   )
   DUPLICATE KEY(ts)
   PARTITION BY RANGE(ts) ()
   DISTRIBUTED BY HASH(v) BUCKETS 1
   PROPERTIES (
     "replication_num" = "1",
     "dynamic_partition.enable" = "true",
     "dynamic_partition.time_unit" = "DAY",
     "dynamic_partition.start" = "-3",
     "dynamic_partition.end" = "1",
     "dynamic_partition.prefix" = "p",
     "dynamic_partition.create_history_partition" = "true"
   );
   INSERT INTO events VALUES
     (UTC_TIMESTAMP() - INTERVAL 3 DAY, 1),
     (UTC_TIMESTAMP() - INTERVAL 2 DAY, 2),
     (UTC_TIMESTAMP() - INTERVAL 1 DAY, 3),
     (UTC_TIMESTAMP(), 4);
   
   EXPLAIN SELECT COUNT(*) FROM events WHERE ts >= UTC_DATE();
   ```
   
   On 2026-10-21 the table has five partitions, four of them with data. Each 
query below is `EXPLAIN SELECT COUNT(*) FROM events WHERE <filter>`:
   
   | Filter | `partitions=` in the scan node |
   | --- | --- |
   | `ts >= UTC_DATE()` | `4/5 (p20261018,p20261019,p20261020,p20261021)` |
   | `ts >= UTC_DATE() - INTERVAL 1 DAY` | `4/5 
(p20261018,p20261019,p20261020,p20261021)` |
   | `ts >= CURRENT_DATE()` | `1/5 (p20261021)` |
   | `ts >= CAST(UTC_TIMESTAMP() AS DATE)` | `1/5 (p20261021)` |
   | `ts >= DATE(UTC_TIMESTAMP())` | `1/5 (p20261021)` |
   | `ts >= UTC_TIMESTAMP() - INTERVAL 1 HOUR` | `1/5 (p20261021)` |
   
   For `ts >= UTC_DATE()`, the scan node shows `PREDICATES: (ts[#0] >= 
CAST(utc_date() AS datetimev2(0)))`.
   
   `EXPLAIN SELECT UTC_TIME(), CURRENT_TIME(), UTC_DATE(), CURDATE()` shows:
   
   ```text
     0:VUNION(27)
        constant exprs:
            utc_time() | "05:09:21" | utc_date() | '2026-10-21'
   ```
   
   ### Anything Else?
   
   We found this while writing a lab about partition pruning on time-series 
data, where `UTC_DATE()` is a natural way to write "today in UTC". Until it is 
fixed, `CAST(UTC_TIMESTAMP() AS DATE)` prunes as expected.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to