hakanuzum opened a new pull request, #62535:
URL: https://github.com/apache/doris/pull/62535

   ### What problem does this PR solve?
   
   Issue Number: close #62395
   
   Related PR: None
   
   **Problem Summary:**
   
   MTMV (Materialized View) partition rollup and incremental refresh logic 
previously only handled partition expressions of the form:
   
   ```sql
   date_trunc(partition_col, 'unit')
   ```
   
   It could not recognize or process expressions where a hour-level offset is 
applied to the partition column before truncation, such as:
   
   ```sql
   date_trunc(date_add(partition_col, INTERVAL N HOUR), 'unit')
   date_trunc(date_sub(partition_col, INTERVAL N HOUR), 'unit')
   ```
   
   This pattern is widely used in production pipelines where raw data is stored 
in UTC but the MV must partition by local business-day boundaries (e.g., UTC+3 
→ `date_add(event_time, INTERVAL 3 HOUR)`). Without this support:
   
   - Partition lineage between the base table and MTMV could not be established.
   - Incremental refresh predicate generation failed silently, causing full 
refresh fallback or incorrect partition mapping.
   - `PartitionIncrementMaintainer` rejected these expressions as unsupported, 
blocking partition-aware query rewrite.
   
   The root causes were:
   1. `MTMVPartitionExprFactory` only returned `MTMVPartitionExprDateTrunc` for 
any `date_trunc(...)` call, with no branch for the inner `date_add`/`date_sub` 
case.
   2. `PartitionIncrementChecker.SUPPORT_EXPRESSION_TYPES` did not include 
`HoursAdd`, `HoursSub`, or `Cast`, so any MV defined with these expressions was 
rejected during increment check.
   3. There was no rollup service implementation that could compute partition 
identity and generate range bounds when an hour offset is involved.
   
   ---
   
   ### What is changed and how does it work?
   
   **New class — `MTMVPartitionExprDateTruncDateAddSub`:**
   
   Implements `MTMVPartitionExprService` for expressions of the form  
`date_trunc(date_add/date_sub(col,INTERVAL N HOUR), 'unit')`.
   
   Key responsibilities:
   - Parses the `date_trunc` outer function and extracts the truncation unit.
   - Parses the inner `TimestampArithmeticExpr` (`date_add`/`date_sub`) and 
converts the hour offset to a signed `long` (`date_sub` → negative offset).
   - `getRollUpIdentity()`: applies the offset to each partition value and 
truncates, returning a stable identity string for lineage mapping.
   - `generateRollUpPartitionKeyDesc()`: computes `[lower, upper)` range bounds 
in the MV partition space, applying the offset before truncation and 
incrementing by the appropriate time unit (`day`, `week`, `month`, `year`, 
`quarter`, `hour`).
   - `analyze()`: validates that the truncation unit is supported and that the 
base table uses RANGE partitioning with a DATE/DATETIME column.
   - `toSql()`: regenerates the canonical SQL expression for the partition 
expression.
   
   **`MTMVPartitionExprFactory` — routing logic extended:**
   
   Added a guard inside the `date_trunc` branch: if the first argument is a 
`TimestampArithmeticExpr` (i.e., `date_add`/`date_sub`), the factory now 
returns  `MTMVPartitionExprDateTruncDateAddSub` instead of the plain 
`MTMVPartitionExprDateTrunc`.
   
   ```java
   if (paramsExprs.size() == 2 && paramsExprs.get(0) instanceof 
TimestampArithmeticExpr) {
       return new MTMVPartitionExprDateTruncDateAddSub(functionCallExpr);
   }
   return new MTMVPartitionExprDateTrunc(functionCallExpr);
   ```
   
   **`PartitionIncrementMaintainer.PartitionIncrementChecker`:**
   
   Extended `SUPPORT_EXPRESSION_TYPES` to include `Cast`, `HoursAdd`, and 
`HoursSub`, 
   allowing the increment checker to traverse and validate MV partition 
expressions 
   that contain these node types:
   
   ```java
   ImmutableSet.of(Cast.class, DateTrunc.class, HoursAdd.class, HoursSub.class,
       SlotReference.class, Literal.class)
   ```
   
   **`UpdateMvByPartitionCommand` and `MTMVPartitionDefinition`:**
   
   Minor adjustments to pass the hour-offset partition expr through the 
incremental 
   refresh predicate builder without being rejected.
   
   ---
   
   ### Example
   
   **Base table** partitioned by hour-level UTC timestamp:
   
   ```sql
   CREATE TABLE orders (
       order_id BIGINT,
       event_time DATETIME NOT NULL
   ) PARTITION BY RANGE(event_time) ( ... )
   DISTRIBUTED BY HASH(order_id);
   ```
   
   **MTMV** partitioned by local business day (UTC+3):
   
   ```sql
   CREATE MATERIALIZED VIEW orders_by_local_day
   BUILD IMMEDIATE REFRESH ON MANUAL
   PARTITION BY (date_trunc(date_add(event_time, INTERVAL 3 HOUR), 'day'))
   DISTRIBUTED BY HASH(order_id)
   AS SELECT
       date_trunc(date_add(event_time, INTERVAL 3 HOUR), 'day') AS local_day,
       count(*) AS cnt
   FROM orders
   GROUP BY local_day;
   ```
   
   **Before this PR:** Creating this MV would fail or fall back to full refresh 
because the partition expression `date_trunc(date_add(...), 'day')` was not 
recognized.
   
   **After this PR:** Partition lineage is correctly established. Incremental 
refresh only recomputes partitions whose source data changed, and the predicate 
pushed down to the base table correctly accounts for the 3-hour offset window.
   
   ---
   
   ### Behavior change
   
   | Scenario | Before | After |
   |---|---|---|
   | `date_trunc(col, 'day')` | ✅ Supported | ✅ No change |
   | `date_trunc(date_add(col, INTERVAL N HOUR), 'day/week/month/year')` | ❌ 
Lineage broken, full refresh | ✅ Incremental refresh works |
   | `date_trunc(date_sub(col, INTERVAL N HOUR), 'day/week/month/year')` | ❌ 
Not recognized | ✅ Supported |
   | `PartitionIncrementChecker` with `HoursAdd`/`HoursSub` nodes | ❌ Rejected 
| ✅ Passes validation |
   
   Backward compatible. No change to existing `date_trunc(col, unit)` behavior.
   
   ---
   
   ### Release note
   
   Support MTMV partition rollup and incremental refresh for  
`date_trunc(date_add/date_sub(partition_col, INTERVAL N HOUR), 'unit')`.  This 
enables local-timezone day partitioning on UTC-stored tables via Async 
Materialized Views.
   
   ---
   
   ### Check List (For Author)
   
   - Test
     - [x] FE Unit Test:  
       `./run-fe-ut.sh --run 
org.apache.doris.mtmv.MTMVRelatedPartitionDescRollUpGeneratorTest`
     - [x] Regression test:  
       `./run-regression-test.sh --run -d mtmv_p0 -s 
test_rollup_partition_mtmv_date_add`
     - [x] Manual test: Validated with FE build against a local single-node 
cluster.  
       `MVN_OPT='-Dmaven.build.cache.enabled=false' ./build.sh --fe --clean 
-j$(nproc)`
   
   - Behavior changed: Yes  
     `date_trunc(date_add/date_sub(..., INTERVAL N HOUR), unit)` partition 
expressions are now supported for MTMV partition lineage and incremental 
refresh.
   
   - Does this need documentation: Yes  
     The MTMV partition expression documentation should be updated to list 
`date_trunc(date_add/date_sub(col, INTERVAL N HOUR), unit)` as a supported form.


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