yujun777 commented on code in PR #70289:
URL: https://github.com/apache/doris/pull/70289#discussion_r4767035103


##########
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:
   Fixed in 00e3984a422: updated `MergeDateTrunc` to resolve date and time-unit 
argument positions via `DateTrunc.getMonotonicFunctionChildIndex()`, and 
extended 
`MTMVPartitionReanalysisTest.testReanalyzeCoarserExplicitUnitOverFinerQueryTrunc`
 to cover both argument orders.



##########
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:
   This is a pre-existing limitation in 
`PartitionIncrementMaintainer.getRelatedTableColumnInfosWithCheck` (which 
deduplicates by `BaseTableInfo` without comparing branch partition expressions) 
and already affects `CREATE MATERIALIZED VIEW` and unaliased MVs. It only 
appeared unreachable before because aliased EXPR MVs failed unconditionally 
with `partition column can not find from sql select column`. Keeping this PR 
focused on output-column reanalysis rather than redesigning 
`PartitionIncrementChecker`.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/PartitionIncrementMaintainer.java:
##########
@@ -770,15 +777,22 @@ public static final class PartitionIncrementCheckContext {
         public PartitionIncrementCheckContext(NamedExpression 
mvPartitionColumn,
                 Expression mvPartitionExpression, Map<CTEId, Plan> 
producerCteIdToPlanMap,
                 Plan originalPlan,
-                CascadesContext cascadesContext) {
+                CascadesContext cascadesContext, 
Predicate<RelatedTableColumnInfo> partitionLineageCheck) {
             this.partitionAndRefExpressionMap.put(mvPartitionColumn, 
RelatedTableColumnInfo.of(
                     mvPartitionColumn, mvPartitionExpression, true, false));
             this.cascadesContext = cascadesContext;
             this.producerCteIdToPlanMap = producerCteIdToPlanMap;
             this.originalPlan = originalPlan;
+            this.partitionLineageCheck = partitionLineageCheck;
             this.expressionRewriteContext = new 
ExpressionRewriteContext(cascadesContext);
         }
 
+        public boolean hasCompatiblePartitionLineage() {
+            return partitionLineageCheck == null || 
partitionAndRefExpressionMap.values().stream()

Review Comment:
   This is a pre-existing limitation in `PartitionIncrementChecker`'s CTE 
producer slot tracking (shared across `CREATE MATERIALIZED VIEW` and unaliased 
MVs). Previously it was only masked for aliased MVs because reanalysis 
unconditionally failed to look up the base column name in the MV output. 
Keeping this PR scoped to partition-column reanalysis without adding ad-hoc 
plan whitelists.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/MTMVPartitionDefinition.java:
##########
@@ -72,30 +86,87 @@ public class MTMVPartitionDefinition {
      * @return MTMVPartitionInfo
      */
     public MTMVPartitionInfo 
analyzeAndTransferToMTMVPartitionInfo(NereidsPlanner planner) {
-        MTMVPartitionInfo mtmvPartitionInfo = new 
MTMVPartitionInfo(partitionType);
-        if (this.partitionType == MTMVPartitionType.SELF_MANAGE) {
-            return mtmvPartitionInfo;
+        if (partitionType == MTMVPartitionType.SELF_MANAGE) {
+            return new MTMVPartitionInfo(partitionType);
+        }
+        if (existingPartitionInfo != null) {
+            return reanalyzePartition(planner);
+        }
+        PartitionColumn declared = partitionType == MTMVPartitionType.EXPR
+                ? partitionColumnFromExpression(functionCallExpression) : new 
PartitionColumn(partitionCol, null);
+        MTMVPartitionInfo analyzed = analyzePartition(planner, declared, 
partitionType);
+        analyzed.setDeclaredPartitionTimeUnit(declared.timeUnit() == null ? "" 
: declared.timeUnit());
+        partitionType = analyzed.getPartitionType();
+        return analyzed;
+    }
+
+    private MTMVPartitionInfo reanalyzePartition(NereidsPlanner planner) {
+        String declaredUnit = 
existingPartitionInfo.getDeclaredPartitionTimeUnit();
+        if (partitionType == MTMVPartitionType.FOLLOW_BASE_TABLE || 
"".equals(declaredUnit)) {
+            MTMVPartitionInfo analyzed = analyzePartition(planner,
+                    new PartitionColumn(partitionCol, null), 
MTMVPartitionType.FOLLOW_BASE_TABLE);

Review Comment:
   A UNION ALL column mixing `MONTH` and `QUARTER` truncations only passed 
previously when the query also projected the raw `dt` column and the buggy 
reanalysis path accidentally resolved `dt` instead of `q`. That behavior is not 
intentional and does not warrant keeping the broken base-column lookup path.



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