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


##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandler.java:
##########
@@ -436,15 +443,168 @@ 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 || 
!isApplyKeyTypeCompatible(mvKey.getDataType(), deltaKey.getDataType())
+                        || !addedMvSlots.add(mvKey.getExprId())) {
+                    continue;
+                }
+                conjuncts.add(!mvKey.nullable() && !deltaKey.nullable()
+                        ? new EqualTo(mvKey, deltaKey)
+                        : new NullSafeEqual(mvKey, deltaKey));

Review Comment:
   [P3] Avoid serializing a second hash key for a row-ID-distributed MV
   
   For an unpartitioned MV with `KEY(k)`, full keys off, and `SELECT k, 
COUNT(*) FROM t GROUP BY k` over a nullable native VARCHAR, the default hash 
distribution is on the hidden row ID. `IvmUtil.buildRowIdHash` uses the sole 
VARCHAR group key as that row ID, so the existing row-ID comparison already 
supplies the bucket key. The added `mv.k <=> delta.k` cannot push a runtime 
filter through this outer apply join, but it moves BE from the single-string 
hash path to the serialized composite path in `get_hash_key_type`. Skip this 
extra comparison when the row ID already supplies the bucket key and the 
visible key cannot provide pruning.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandler.java:
##########
@@ -436,15 +443,168 @@ 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 || 
!isApplyKeyTypeCompatible(mvKey.getDataType(), deltaKey.getDataType())
+                        || !addedMvSlots.add(mvKey.getExprId())) {
+                    continue;
+                }
+                conjuncts.add(!mvKey.nullable() && !deltaKey.nullable()
+                        ? new EqualTo(mvKey, deltaKey)
+                        : new NullSafeEqual(mvKey, deltaKey));
+            }
+        }
         // Row-id may be NULL (single key used directly as largeint), so match 
with null-safe
         // equality: a NULL row-id matches the MV row of the NULL-key group.
-        conjuncts.add(new NullSafeEqual(mvRowId, delta.rowIdSlot));
+        if (addedMvSlots.add(mvRowId.getExprId())) {
+            conjuncts.add(new NullSafeEqual(mvRowId, delta.rowIdSlot));
+        }
         return conjuncts.build();
     }
 
+    private boolean 
isApplyKeyTypeCompatible(org.apache.doris.nereids.types.DataType mvType,
+            org.apache.doris.nereids.types.DataType deltaType) {
+        // Skip CHAR: storage CHAR decoding truncates at embedded NUL bytes 
(strnlen), which can
+        // differ from a computed CHAR value in the delta and miss an existing 
MV group.
+        if (mvType.isCharType() || deltaType.isCharType()) {
+            return false;
+        }
+        if (mvType.equals(deltaType)) {

Review Comment:
   [P2] Preserve old-group matches when a non-strict insert truncates VARCHAR
   
   Use `KEY(g)`, full keys off, and `enable_insert_strict=false` for refresh. 
This reduced normalized plan reads a base column `v VARCHAR(65533)`.
   
   ```text
   Project(g, k, cnt)
     Aggregate(GROUP BY g, k, COUNT(*) AS cnt)
       Project(REPEAT(v, 70000) AS g, k)
   ```
   
   For `v = 'a'`, BE `FunctionStringRepeat` produces 70,000 bytes. Its VARCHAR 
type passes this comparison with the MV's `VARCHAR(65533)`. The non-strict 
tablet sink truncates the stored `g` to 65,533 bytes in `validate_string`, 
after the composite row ID has been computed. When a second row enters the same 
group, the row IDs match and `mv.g <=> delta.g` fails. The apply step treats 
the group as new and overwrites a count of 1 with 1 when it should write 2. 
Keep the row-ID-only match unless the planner can prove the VARCHAR values 
survive storage unchanged.



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