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


##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandler.java:
##########
@@ -436,15 +443,147 @@ private ImmutableList<Expression> 
buildApplyJoinConjuncts(IvmIncrRefreshContext
                 if (deltaKey == null) {
                     continue;
                 }
+                addedMvSlots.add(mvKey.getExprId());
                 conjuncts.add(new NullSafeEqual(mvKey, deltaKey));
             }
         }
+        // When an aggregate MV defines explicit KEY, PARTITION BY, or HASH 
distribution columns,
+        // those visible columns precede __DORIS_IVM_ROW_ID_COL__ in the 
physical schema. Adding
+        // redundant equality conjuncts on any visible key column that 
directly passes through a
+        // group key unlocks Bucket Shuffle Join (and runtime-filter / prefix 
pruning when both
+        // sides are non-nullable and use EqualTo) even without 
ivm_use_full_keys.
+        Set<String> visibleMvKeyNames = 
collectVisibleMvKeyColumnNames(ctx.getMtmv());
+        if (!visibleMvKeyNames.isEmpty()) {
+            Map<String, Integer> directGroupKeyByMvColName = 
resolveDirectGroupKeyMapping(
+                    rewriteState.normalizedRootPlan(), normalizedAgg, aggMeta, 
visibleMvKeyNames);
+            for (String keyColName : visibleMvKeyNames) {
+                Integer groupKeyIndex = 
directGroupKeyByMvColName.get(keyColName);
+                if (groupKeyIndex == null) {
+                    continue;
+                }
+                Slot mvKey = 
helper.findSlotByNameOrNull(rawMvScan.getOutput(), keyColName);
+                Slot deltaKey = delta.deltaGroupKeySlots.get(groupKeyIndex);
+                if (mvKey == null || 
!mvKey.getDataType().equals(deltaKey.getDataType())
+                        || !addedMvSlots.add(mvKey.getExprId())) {
+                    continue;
+                }
+                conjuncts.add(!mvKey.nullable() && !deltaKey.nullable()

Review Comment:
   [P2] Preserve the old group match for computed CHAR keys
   
   Consider a grouped IVM view with `KEY(c)`, full keys off, and 
`use_max_length_of_varchar_in_ctas=false` at creation. A reduced plan is:
   
   ```text
   Project(c, k, cnt)
     Aggregate(GROUP BY c, k, COUNT(*) AS cnt)
       Project(ELEMENT_AT(CAST(ARRAY(v) AS ARRAY<CHAR(8)>), 1) AS c, k)
   ```
   
   Both operands have type `CHAR(8)`, so the new apply join adds `mv.c <=> 
delta.c`. The nested cast and `ELEMENT_AT` forward all bytes of the computed 
value. The CHAR page decoder uses `strnlen`, so an embedded NUL makes the MV 
scan return only the prefix. The two rows still have the same composite group 
row ID, which is calculated before the storage conversion, but the additional 
equality excludes the old row. With one existing row and one new row in the 
same group, the apply step can write a count of 1 where it should write 2. Skip 
computed CHAR keys unless the planner can prove the values survive the storage 
round trip unchanged.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandler.java:
##########
@@ -436,15 +443,147 @@ private ImmutableList<Expression> 
buildApplyJoinConjuncts(IvmIncrRefreshContext
                 if (deltaKey == null) {
                     continue;
                 }
+                addedMvSlots.add(mvKey.getExprId());
                 conjuncts.add(new NullSafeEqual(mvKey, deltaKey));
             }
         }
+        // When an aggregate MV defines explicit KEY, PARTITION BY, or HASH 
distribution columns,
+        // those visible columns precede __DORIS_IVM_ROW_ID_COL__ in the 
physical schema. Adding
+        // redundant equality conjuncts on any visible key column that 
directly passes through a
+        // group key unlocks Bucket Shuffle Join (and runtime-filter / prefix 
pruning when both
+        // sides are non-nullable and use EqualTo) even without 
ivm_use_full_keys.
+        Set<String> visibleMvKeyNames = 
collectVisibleMvKeyColumnNames(ctx.getMtmv());
+        if (!visibleMvKeyNames.isEmpty()) {
+            Map<String, Integer> directGroupKeyByMvColName = 
resolveDirectGroupKeyMapping(
+                    rewriteState.normalizedRootPlan(), normalizedAgg, aggMeta, 
visibleMvKeyNames);
+            for (String keyColName : visibleMvKeyNames) {
+                Integer groupKeyIndex = 
directGroupKeyByMvColName.get(keyColName);
+                if (groupKeyIndex == null) {
+                    continue;
+                }
+                Slot mvKey = 
helper.findSlotByNameOrNull(rawMvScan.getOutput(), keyColName);
+                Slot deltaKey = delta.deltaGroupKeySlots.get(groupKeyIndex);
+                if (mvKey == null || 
!mvKey.getDataType().equals(deltaKey.getDataType())

Review Comment:
   [P3] Keep the bucket-key match when MV creation widens a VARCHAR
   
   For a single-partition aggregate MV with `DISTRIBUTED BY HASH(g)` and full 
keys off, consider:
   
   ```text
   Project(g, k2, cnt)
     Aggregate(GROUP BY g, k2, COUNT(*) AS cnt)
       Project(SUBSTRING(k, 1, 40000) AS g, k2)
   ```
   
   The delta key is `VARCHAR(40000)`, while `MTMVPlanUtil.getDataType` gives 
the computed MV column the maximum VARCHAR width under the default 
`use_max_length_of_varchar_in_ctas` setting. The exact type check drops `mv.g = 
delta.g` even though widening preserves the value. The remaining row-ID match 
cannot use the MV's natural hash distribution on `g`. Allow a value-preserving 
VARCHAR widening on the delta side. 
`ChildOutputPropertyDeriver.isSameHashValue` already recognizes this widening 
as hash-preserving.



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