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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/MTMVPartitionDefinition.java:
##########
@@ -145,7 +145,7 @@ private RelatedTableInfo 
getRelatedTableInfos(NereidsPlanner planner, String par
             // 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)) {
+            if (!direct.isPctPossible() || hasMatchingDateTruncUnit(direct, 
timeUnit)) {

Review Comment:
   [P2] Reapply an explicit partition rollup during reanalysis
   
   Over two daily RANGE-partitioned DATETIME tables, consider an MV with 
`PARTITION BY (date_trunc(q, 'month'))` and the following source query.
   
   ```sql
   SELECT date_trunc(dt, 'day') AS q FROM a
   UNION ALL
   SELECT dt AS q FROM b
   ```
   
   At CREATE, the checker pushes the explicit MONTH into both arms and 
`MergeDateTrunc` reduces MONTH(DAY(dt)) to MONTH(dt). Both arms pass. If the 
query is supplied through a SQL view, even an equivalent `ALTER VIEW` puts the 
MV in `SCHEMA_CHANGE`. During the next refresh, the direct lookup compares DAY 
with bare lineage, and this return prevents the valid MONTH lookup. Preserve 
the distinction between explicit and inferred rollups so reanalysis can apply 
the original partition clause and still reject changed lineage for an inferred 
partition.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/PartitionIncrementMaintainer.java:
##########
@@ -161,8 +163,24 @@ public Void visitLogicalUnion(LogicalUnion union, 
PartitionIncrementCheckContext
                     if (tableColumnInfo.isReachRelationCheck()) {
                         childAnyReachRelationCheck = true;
                     }
-                    childAnyIsFromTablePartitionColumn
-                            = childAnyIsFromTablePartitionColumn || 
tableColumnInfo.isFromTablePartitionColumn();
+                    if (tableColumnInfo.isFromTablePartitionColumn()) {

Review Comment:
   [P2] Keep join-derived keys from conflicting with their own UNION arm
   
   This query places the inner-join shape covered on its own by 
`PartitionColumnTraceTest.test5000` inside a UNION. Use three DATETIME tables 
with day-aligned RANGE partitions.
   
   ```sql
   SELECT a.q, b.v
   FROM (SELECT date_trunc(dt, 'day') AS q FROM a) a
   JOIN b ON b.dt = a.q
   UNION ALL
   SELECT date_trunc(c.dt, 'day') AS q, c.v FROM c
   ```
   
   Both projected `q` values use DAY. The first arm also tracks bare `b.dt` 
because `visitLogicalJoin` proves it equal to `a.q`. Every matching `b.dt` is 
already a DAY boundary. The loop compares both entries from that first arm and 
fails on DAY versus none. Compare the output partition lineage across arms 
while retaining the equivalent base keys used for refresh.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/PartitionIncrementMaintainer.java:
##########
@@ -161,8 +163,24 @@ public Void visitLogicalUnion(LogicalUnion union, 
PartitionIncrementCheckContext
                     if (tableColumnInfo.isReachRelationCheck()) {
                         childAnyReachRelationCheck = true;
                     }
-                    childAnyIsFromTablePartitionColumn
-                            = childAnyIsFromTablePartitionColumn || 
tableColumnInfo.isFromTablePartitionColumn();
+                    if (tableColumnInfo.isFromTablePartitionColumn()) {
+                        childAnyIsFromTablePartitionColumn = true;
+                        Optional<String> unit = 
extractDateTruncUnit(tableColumnInfo.getPartitionExpression());
+                        if (unit == null) {
+                            context.addFailReason("union all child partition 
expression has invalid date_trunc");
+                            context.setFailFast(true);
+                            return null;
+                        }
+                        if (expectedUnionUnit == null) {
+                            expectedUnionUnit = unit;
+                        } else if (!expectedUnionUnit.equals(unit)) {

Review Comment:
   [P2] Treat DAY truncation of a DATE as an identity in lineage comparisons
   
   For two DATEV2 tables with one-day RANGE partitions, `PARTITION BY (q)` used 
to accept `SELECT date_trunc(d, 'day') AS q FROM a UNION ALL SELECT d AS q FROM 
b`. Both branches return the same DATEV2 value for a given input date, and 
applying the DAY rollup to those daily bounds leaves the ranges unchanged. The 
DATEV2 implementation of `datetime_trunc` leaves DAY values intact, yet this 
comparison rejects DAY versus none. Treat DAY on a DATEV2 input as equivalent 
to the bare value when comparing lineage, while keeping the mismatch check for 
DATETIME.



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