Skip to content

Commit

Permalink
tmp save
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Nov 27, 2023
1 parent 848de01 commit fc4acab
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.memo.Group;
import org.apache.doris.nereids.rules.exploration.mv.Predicates.SplitPredicate;
import org.apache.doris.nereids.rules.exploration.mv.mapping.EquivalenceClassSetMapping;
import org.apache.doris.nereids.rules.exploration.mv.mapping.ExpressionIndexMapping;
import org.apache.doris.nereids.rules.exploration.mv.mapping.RelationMapping;
import org.apache.doris.nereids.rules.exploration.mv.mapping.SlotMapping;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.algebra.CatalogRelation;
Expand All @@ -37,7 +37,6 @@
import com.google.common.collect.Sets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -150,7 +149,7 @@ protected List<NamedExpression> rewriteExpression(List<? extends Expression> sou
// mv sql plan expressions transform to query based
List<? extends Expression> queryBasedExpressions = ExpressionUtils.replace(
shuttledTargetExpressions.stream().map(Expression.class::cast).collect(Collectors.toList()),
(Map)sourceToTargetSlotMapping.inverse().toMap());
(Map)sourceToTargetSlotMapping.inverse().toMappedSlotMap());
// mv sql query based expression and index mapping
ExpressionIndexMapping.generate(queryBasedExpressions);
// TODO visit source expression and replace the expression with expressionIndexMapping
Expand All @@ -171,7 +170,8 @@ protected SplitPredicate predicatesCompensate(
EquivalenceClass queryEquivalenceClass = queryStructInfo.getEquivalenceClass();
EquivalenceClass viewEquivalenceClass = viewStructInfo.getEquivalenceClass();
// viewEquivalenceClass to query based

EquivalenceClass viewEquivalenceClassQueryBased =
viewEquivalenceClass.permute(queryToViewSlotMapping.inverse().toSlotReferenceMap());
SplitPredicate splitPredicate = null;
if (queryEquivalenceClass.isEmpty()
&& viewEquivalenceClass.isEmpty()) {
Expand All @@ -182,6 +182,22 @@ protected SplitPredicate predicatesCompensate(
&& !viewEquivalenceClass.isEmpty()) {
return SplitPredicate.empty();
}
EquivalenceClassSetMapping equivalenceClassSetMapping =
EquivalenceClassSetMapping.generate(queryEquivalenceClass, viewEquivalenceClassQueryBased);
// can not map all can not compensate
if (equivalenceClassSetMapping.getEquivalenceClassSetMap().size() !=
queryEquivalenceClass.getEquivalenceSetList().size()) {
return SplitPredicate.empty();
}
// do compensate
Expression compensatePredicate;
equivalenceClassSetMapping.getEquivalenceClassSetMap().forEach(
equivalenceMapping -> {

}
);


// TODO range predicates and residual predicates compensate
return splitPredicate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
package org.apache.doris.nereids.rules.exploration.mv;

import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -32,11 +35,16 @@
*/
public class EquivalenceClass {

private final Map<SlotReference, Set<SlotReference>> equivalenceSlotMap = new LinkedHashMap<>();
private Map<SlotReference, Set<SlotReference>> equivalenceSlotMap = new LinkedHashMap<>();
private List<Set<SlotReference>> equivalenceSlotList;

public EquivalenceClass() {
}

public EquivalenceClass(Map<SlotReference, Set<SlotReference>> equivalenceSlotMap) {
this.equivalenceSlotMap = equivalenceSlotMap;
}

/**
* EquivalenceClass
*/
Expand Down Expand Up @@ -82,16 +90,46 @@ public boolean isEmpty() {
return equivalenceSlotMap.isEmpty();
}

public EquivalenceClass permute(Map<? extends Expression, ? extends Expression> mapping) {
return null;
public EquivalenceClass permute(Map<SlotReference, SlotReference> mapping) {

Map<SlotReference, Set<SlotReference>> permutedEquivalenceSlotMap = new HashMap<>();
for (Map.Entry<SlotReference, Set<SlotReference>> slotReferenceSetEntry : equivalenceSlotMap.entrySet()) {
SlotReference mappedSlotReferenceKey = mapping.get(slotReferenceSetEntry.getKey());
if (mappedSlotReferenceKey == null) {
// can not permute then need to return null
return null;
}
Set<SlotReference> equivalenceValueSet = slotReferenceSetEntry.getValue();
final Set<SlotReference> mappedSlotReferenceSet = new HashSet<>();
for (SlotReference target : equivalenceValueSet) {
SlotReference mappedSlotReferenceValue = mapping.get(target);
if (mappedSlotReferenceValue == null) {
return null;
}
mappedSlotReferenceSet.add(mappedSlotReferenceValue);
}
permutedEquivalenceSlotMap.put(mappedSlotReferenceKey, mappedSlotReferenceSet);
}
return new EquivalenceClass(permutedEquivalenceSlotMap);
}

/**
* EquivalenceClass
* Return the list of equivalence set, remove duplicate
*/
public List<Set<SlotReference>> getEquivalenceValues() {
List<Set<SlotReference>> values = new ArrayList<>();
equivalenceSlotMap.values().forEach(each -> values.add(each));
return values;
public List<Set<SlotReference>> getEquivalenceSetList() {

if (equivalenceSlotList != null) {
return equivalenceSlotList;
}
List<Set<SlotReference>> equivalenceSets = new ArrayList<>();
Set<Set<SlotReference>> visited = new HashSet<>();
equivalenceSlotMap.values().forEach(slotSet -> {
if (!visited.contains(slotSet)) {
equivalenceSets.add(slotSet);
}
visited.add(slotSet);
});
this.equivalenceSlotList = equivalenceSets;
return this.equivalenceSlotList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.rules.exploration.mv.mapping;

import org.apache.doris.nereids.rules.exploration.mv.EquivalenceClass;
import org.apache.doris.nereids.trees.expressions.SlotReference;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**EquivalenceClassSetMapping */
public class EquivalenceClassSetMapping extends Mapping{

private final Map<Set<SlotReference>, Set<SlotReference>> equivalenceClassSetMap;

public EquivalenceClassSetMapping(Map<Set<SlotReference>,
Set<SlotReference>> equivalenceClassSetMap) {
this.equivalenceClassSetMap = equivalenceClassSetMap;
}

public static EquivalenceClassSetMapping of(Map<Set<SlotReference>, Set<SlotReference>> equivalenceClassSetMap) {
return new EquivalenceClassSetMapping(equivalenceClassSetMap);
}

/** source equivalence set map to target equivalence set*/
public static EquivalenceClassSetMapping generate(EquivalenceClass source, EquivalenceClass target) {

Map<Set<SlotReference>, Set<SlotReference>> equivalenceClassSetMap = new HashMap<>();
List<Set<SlotReference>> sourceSets = source.getEquivalenceSetList();
List<Set<SlotReference>> targetSets = target.getEquivalenceSetList();

for (Set<SlotReference> sourceSet : sourceSets) {
for (Set<SlotReference> targetSet : targetSets) {
if (sourceSet.containsAll(targetSet)) {
equivalenceClassSetMap.put(sourceSet, targetSet);
}
}
}
return EquivalenceClassSetMapping.of(equivalenceClassSetMap);
}

public Map<Set<SlotReference>, Set<SlotReference>> getEquivalenceClassSetMap() {
return equivalenceClassSetMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package org.apache.doris.nereids.rules.exploration.mv.mapping;

import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand All @@ -32,6 +34,7 @@
public class SlotMapping extends Mapping {

private final BiMap<MappedSlot, MappedSlot> relationSlotMap;
private Map<SlotReference, SlotReference> slotReferenceMap;

public SlotMapping(BiMap<MappedSlot, MappedSlot> relationSlotMap) {
this.relationSlotMap = relationSlotMap;
Expand Down Expand Up @@ -73,7 +76,21 @@ public static SlotMapping generate(RelationMapping relationMapping) {
return SlotMapping.of(relationSlotMap);
}

public Map<MappedSlot, MappedSlot> toMap() {
public Map<MappedSlot, MappedSlot> toMappedSlotMap() {
return (Map)this.getRelationSlotMap();
}


public Map<SlotReference, SlotReference> toSlotReferenceMap() {
if (this.slotReferenceMap != null) {
return this.slotReferenceMap;
}
Map<SlotReference, SlotReference> slotReferenceSlotReferenceMap = new HashMap<>();
for (Map.Entry<MappedSlot, MappedSlot> entry : this.getRelationSlotMap().entrySet()) {
slotReferenceSlotReferenceMap.put((SlotReference) entry.getKey().getSlot(),
(SlotReference) entry.getValue().getSlot());
}
this.slotReferenceMap = slotReferenceSlotReferenceMap;
return this.slotReferenceMap;
}
}

0 comments on commit fc4acab

Please sign in to comment.