seawinde commented on code in PR #37929: URL: https://github.com/apache/doris/pull/37929#discussion_r1680568378
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java: ########## @@ -568,6 +563,93 @@ protected List<Expression> rewriteExpression(List<? extends Expression> sourceEx return rewrittenExpressions; } + /** + * if query contains variant slot reference, extend the expression mapping for rewrte + * such as targetToTargetReplacementMappingQueryBased is + * id#0 -> id#8 + * type#1 -> type#9 + * payload#4 -> payload#10 + * query variants is payload['issue']['number']#20 + * then we can add payload['issue']['number']#20 -> element_at(element_at(payload#10, 'issue'), 'number') + * to targetToTargetReplacementMappingQueryBased + * */ + private void extendMappingByVariant(Set<SlotReference> queryVariants, + Map<Expression, Expression> targetToTargetReplacementMappingQueryBased) { + if (queryVariants.isEmpty()) { + return; + } + Map<List<String>, Expression> viewNameToExprMap = new HashMap<>(); + for (Map.Entry<Expression, Expression> targetExpressionEntry : + targetToTargetReplacementMappingQueryBased.entrySet()) { + if (targetExpressionEntry.getKey() instanceof SlotReference + && ((SlotReference) targetExpressionEntry.getKey()).getDataType() instanceof VariantType) { + List<String> nameIdentifier = new ArrayList<>(); + nameIdentifier.add(((SlotReference) targetExpressionEntry.getKey()).getName()); + nameIdentifier.addAll(((SlotReference) targetExpressionEntry.getKey()).getSubPath()); + viewNameToExprMap.put(nameIdentifier, targetExpressionEntry.getValue()); + } + } + if (viewNameToExprMap.isEmpty()) { + return; + } + Map<List<String>, SlotReference> queryNameAndExpressionMap = new HashMap<>(); + for (SlotReference slotReference : queryVariants) { + List<String> nameIdentifier = new ArrayList<>(); + nameIdentifier.add(slotReference.getName()); + nameIdentifier.addAll(slotReference.getSubPath()); + queryNameAndExpressionMap.put(nameIdentifier, slotReference); + } + for (Map.Entry<List<String>, ? extends Expression> queryNameEntry : queryNameAndExpressionMap.entrySet()) { + Expression minExpr = null; + List<String> minCompensateName = null; + for (Map.Entry<List<String>, Expression> entry : viewNameToExprMap.entrySet()) { + if (!containsAllWithOrder(queryNameEntry.getKey(), entry.getKey())) { + continue; + } + List<String> removedQueryName = new ArrayList<>(queryNameEntry.getKey()); + removedQueryName.removeAll(entry.getKey()); + if (minCompensateName == null) { + minCompensateName = removedQueryName; + minExpr = entry.getValue(); + } + if (removedQueryName.size() < minCompensateName.size()) { + minCompensateName = removedQueryName; + minExpr = entry.getValue(); + } + } + if (minExpr != null) { + targetToTargetReplacementMappingQueryBased.put(queryNameEntry.getValue(), + constructElementAt(minExpr, minCompensateName)); Review Comment: yeah, this will push into scan node again by RBO -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org