hello-stephen commented on code in PR #70289:
URL: https://github.com/apache/doris/pull/70289#discussion_r4766786909


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/MTMVPartitionDefinition.java:
##########
@@ -105,12 +135,46 @@ public MTMVPartitionInfo 
analyzeAndTransferToMTMVPartitionInfo(NereidsPlanner pl
         return mtmvPartitionInfo;
     }
 
+    private RelatedTableInfo getRelatedTableInfos(NereidsPlanner planner, 
String partitionColName, String timeUnit) {
+        CascadesContext cascadesContext = planner.getCascadesContext();
+        Plan rewrittenPlan = planner.getRewrittenPlan();
+        if (isReanalysis && timeUnit != null) {
+            // Reanalysis for EXPR partitions may come from either `PARTITION 
BY (col)` where the query
+            // already truncates `col`, or `PARTITION BY (date_trunc(col, 
unit))`.
+            // Try tracing `col` without adding a redundant outer `date_trunc` 
first so units like
+            // WEEK/QUARTER or reversed `date_trunc(unit, col)` do not fail 
redundant merging.
+            RelatedTableInfo direct = 
MaterializedViewUtils.getRelatedTableInfos(
+                    partitionColName, null, rewrittenPlan, cascadesContext);
+            if (hasMatchingDateTruncUnit(direct, timeUnit)) {
+                return direct;
+            }
+        }
+        return MaterializedViewUtils.getRelatedTableInfos(partitionColName, 
timeUnit, rewrittenPlan, cascadesContext);
+    }
+
+    private static boolean hasMatchingDateTruncUnit(RelatedTableInfo 
relatedTableInfo, String expectedUnit) {
+        if (!relatedTableInfo.isPctPossible() || 
relatedTableInfo.getTableColumnInfos().isEmpty()) {
+            return false;
+        }
+        for (RelatedTableColumnInfo info : 
relatedTableInfo.getTableColumnInfos()) {

Review Comment:
   [P1] Validate every self-UNION occurrence before deduplicating tables
   
   For a range-partitioned DUP source `s(dt DATETIMEV2 NOT NULL, v 
VARCHAR(10))`, create a view that UNION ALLs two `date_trunc(dt,'day') AS q, v` 
projections of `s`, and an AUTO-refresh `PARTITION BY(q)` MV selecting only 
`q,v` from that view. ALTER the view so only the second branch uses MONTH. The 
reduced source-inferred plan is:
   
   ```text
   UnionAll(q, v)
     Project(q=DAY(dt#0), v)    Scan(s)
     Project(q=MONTH(dt#3), v)  Scan(s)
   ```
   
   The ExprIds are representative: when DAY occupies the earlier hash bucket, 
`getRelatedTableColumnInfosWithCheck` drops MONTH via `checkedTableSet` because 
both scans have the same `BaseTableInfo`. This loop therefore sees only DAY, 
returns true and passes the stored source/unit compatibility check. The base 
reanalysis rejects this change because its stored raw `dt` is absent from the 
`q,v` output.
   
   With Jan1 and Jan20 source day partitions, a Jan20 change contributes to 
`q=Jan1` through MONTH, but the retained DAY mapping does not invalidate Jan1. 
A Jan1 partition refresh also filters both scans to Jan1 bounds and omits those 
contributions. Validate all occurrence lineages before table deduplication and 
add this ALTER VIEW negative case. This is separate from the existing CTE 
report: these are distinct scan keys, with no CTE overwrite. Verified by source 
tracing; no runtime reproduction was run.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/MTMVPartitionDefinition.java:
##########
@@ -105,12 +135,46 @@ public MTMVPartitionInfo 
analyzeAndTransferToMTMVPartitionInfo(NereidsPlanner pl
         return mtmvPartitionInfo;
     }
 
+    private RelatedTableInfo getRelatedTableInfos(NereidsPlanner planner, 
String partitionColName, String timeUnit) {
+        CascadesContext cascadesContext = planner.getCascadesContext();
+        Plan rewrittenPlan = planner.getRewrittenPlan();
+        if (isReanalysis && timeUnit != null) {
+            // Reanalysis for EXPR partitions may come from either `PARTITION 
BY (col)` where the query
+            // already truncates `col`, or `PARTITION BY (date_trunc(col, 
unit))`.
+            // Try tracing `col` without adding a redundant outer `date_trunc` 
first so units like
+            // WEEK/QUARTER or reversed `date_trunc(unit, col)` do not fail 
redundant merging.
+            RelatedTableInfo direct = 
MaterializedViewUtils.getRelatedTableInfos(
+                    partitionColName, null, rewrittenPlan, cascadesContext);
+            if (hasMatchingDateTruncUnit(direct, timeUnit)) {
+                return direct;
+            }
+        }
+        return MaterializedViewUtils.getRelatedTableInfos(partitionColName, 
timeUnit, rewrittenPlan, cascadesContext);

Review Comment:
   [P2] Handle reversed arguments in the coarser-unit fallback
   
   Use a range-partitioned DUP source with `dt DATETIMEV2 NOT NULL`, a view 
`SELECT date_trunc(dt,'day') AS q, dt, v FROM s`, and a regular MV with 
`PARTITION BY(date_trunc(q,'month')) AS SELECT q,dt,v FROM the_view`. Creation 
merges MONTH(DAY(dt)) and stores MONTH(dt). Now ALTER the view merely to spell 
`q` as `date_trunc('day',dt)`, retaining raw `dt`; the query meaning, result 
types and monthly mapping are unchanged.
   
   Direct tracing finds DAY rather than the stored MONTH, so this fallback 
produces:
   
   ```text
   Project(date_trunc(q, 'month'))
     Project(q=date_trunc('day', dt), dt, v)
       Scan(s)
   ```
   
   `MergeDateTrunc` reads the inner call's argument 1 as its unit, encounters 
the `dt` slot and leaves two truncations. `checkPartition` then rejects 
reanalysis, preventing refresh. The base path resolves the stored MONTH(dt) 
against the projected raw `dt` and succeeds. Normalize semantic argument 
positions when composing different units, and cover this equivalent ALTER VIEW 
spelling. The added reversed-argument tests use matching units and bypass this 
fallback; the coarser-unit test uses ordinary argument order. Verified by 
source tracing; no runtime reproduction was run.



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