morrySnow commented on code in PR #37929:
URL: https://github.com/apache/doris/pull/37929#discussion_r1680494787
##########
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:
after rewrite, these new element_at will be push into scan node too?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/mapping/SlotMapping.java:
##########
@@ -68,20 +70,23 @@ public static SlotMapping generate(RelationMapping
relationMapping) {
BiMap<MappedRelation, MappedRelation> mappedRelationMap =
relationMapping.getMappedRelationMap();
for (Map.Entry<MappedRelation, MappedRelation> mappedRelationEntry :
mappedRelationMap.entrySet()) {
MappedRelation sourceRelation = mappedRelationEntry.getKey();
- Map<String, Slot> sourceSlotNameToSlotMap =
sourceRelation.getSlotNameToSlotMap();
+ Map<List<String>, Slot> sourceSlotNameToSlotMap =
sourceRelation.getSlotNameToSlotMap();
MappedRelation targetRelation = mappedRelationEntry.getValue();
- Map<String, Slot> targetSlotNameSlotMap =
targetRelation.getSlotNameToSlotMap();
+ Map<List<String>, Slot> targetSlotNameSlotMap =
targetRelation.getSlotNameToSlotMap();
- for (String sourceSlotName : sourceSlotNameToSlotMap.keySet()) {
+ for (List<String> sourceSlotName :
sourceSlotNameToSlotMap.keySet()) {
+ Slot sourceSlot = sourceSlotNameToSlotMap.get(sourceSlotName);
Slot targetSlot = targetSlotNameSlotMap.get(sourceSlotName);
// source slot can not map from target, bail out
- if (targetSlot == null) {
+ if (targetSlot == null && !(((SlotReference)
sourceSlot).getDataType() instanceof VariantType)) {
LOG.warn(String.format("SlotMapping generate is null,
source relation is %s, "
+ "target relation is %s", sourceRelation,
targetRelation));
return null;
}
- Slot sourceSlot = sourceSlotNameToSlotMap.get(sourceSlotName);
+ if (targetSlot == null) {
+ continue;
+ }
Review Comment:
add comment to explain why not print warn log when data type is variant
--
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]