Skip to content

Commit

Permalink
fix(interactive): Fix Bugs of Pattern Not Found in Glogue (#4235)
Browse files Browse the repository at this point in the history
<!--
Thanks for your contribution! please review
https://github.com/alibaba/GraphScope/blob/main/CONTRIBUTING.md before
opening an issue.
-->

## What do these changes do?
Some Patterns are not reordered after creation which leads to the
`pattern not found` error in glogue, this PR mainly fix this.

<!-- Please give a short brief about these changes. -->

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->

Fixes
  • Loading branch information
shirly121 committed Sep 18, 2024
1 parent 862fbf2 commit 7567552
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public double estimate(List<PatternEdge> edges, PatternVertex target) {
pattern.addVertex(edge.getSrcVertex());
pattern.addVertex(edge.getDstVertex());
pattern.addEdge(edge.getSrcVertex(), edge.getDstVertex(), edge);
pattern.reordering();
extendFromVertices.add(Utils.getExtendFromVertex(edge, target));
double weight =
(edges.size() == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public PrimitiveCountEstimator(GlogueQuery gq) {

public @Nullable Double estimate(Pattern pattern) {
if (Utils.canLookUpFromGlogue(pattern, gq.getMaxPatternSize())) {
return gq.getRowCount(pattern);
Double countFromGlogue = gq.getRowCount(pattern, true);
if (countFromGlogue != null) {
return countFromGlogue;
}
}
// estimate the pattern graph with intersect, i.e. a->b, c->b, d->b
PatternVertex intersect = getIntersectVertex(pattern);
Expand All @@ -56,7 +59,7 @@ public PrimitiveCountEstimator(GlogueQuery gq) {
public double estimate(PatternVertex vertex) {
double sum = 0.0d;
for (Integer typeId : vertex.getVertexTypeIds()) {
sum += gq.getRowCount(new Pattern(new SinglePatternVertex(typeId)));
sum += gq.getRowCount(new Pattern(new SinglePatternVertex(typeId)), false);
}
return sum * vertex.getElementDetails().getSelectivity();
}
Expand Down Expand Up @@ -106,7 +109,8 @@ public double estimate(PatternEdge edge, EdgeTypeId typeId, int hops) {
edgePattern.addVertex(dstVertex);
edgePattern.addEdge(
srcVertex, dstVertex, new SinglePatternEdge(srcVertex, dstVertex, typeId, 0));
return Math.pow(gq.getRowCount(edgePattern), hops)
edgePattern.reordering();
return Math.pow(gq.getRowCount(edgePattern, false), hops)
/ Math.pow(estimate(dstVertex), hops - 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public static boolean canLookUpFromGlogue(Pattern pattern, int maxPatternSizeInG
return false;
}
ElementDetails details = vertex.getElementDetails();
if (details != null && Double.compare(details.getSelectivity(), 1.0d) != 0) {
if (details != null
&& (Double.compare(details.getSelectivity(), 1.0d) != 0
|| details.isOptional())) {
return false;
}
}
Expand All @@ -121,7 +123,8 @@ public static boolean canLookUpFromGlogue(Pattern pattern, int maxPatternSizeInG
ElementDetails details = edge.getElementDetails();
if (details != null
&& (Double.compare(details.getSelectivity(), 1.0d) != 0
|| details.getRange() != null)) {
|| details.getRange() != null
|| details.isOptional())) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ public double labelConstraintsDeltaCost(
original.addVertex(dst);
}
original.addEdge(src, dst, patternEdge);
original.reordering();
return cost;
}

Expand All @@ -639,6 +640,7 @@ public RelNode visit(GraphExtendIntersect intersect) {
List<ExtendEdge> extendEdges = extendStep.getExtendEdges();
RelNode child = visitChildren(intersect).getInput(0);
Pattern original = new Pattern(intersect.getGlogueEdge().getSrcPattern());
original.reordering();
// convert to GraphLogicalExpand if only one extend edge
if (extendEdges.size() == 1) {
return addCachedCost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.graphscope.common.ir.rel.metadata.glogue.pattern.SinglePatternVertex;
import com.alibaba.graphscope.common.ir.rel.metadata.schema.GlogueSchema;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.jgrapht.Graph;
import org.jgrapht.graph.DirectedPseudograph;
import org.slf4j.Logger;
Expand Down Expand Up @@ -122,6 +123,13 @@ public Double getRowCount(Pattern pattern) {
return this.glogueCardinalityEstimation.getCardinality(pattern);
}

public @Nullable Double getRowCount(Pattern pattern, boolean allowsNull) {
return (glogueCardinalityEstimation instanceof GlogueBasicCardinalityEstimationImpl)
? ((GlogueBasicCardinalityEstimationImpl) glogueCardinalityEstimation)
.getCardinality(pattern, allowsNull)
: getRowCount(pattern);
}

public int getMaxPatternSize() {
return maxPatternSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.graphscope.common.ir.rel.metadata.glogue.pattern.PatternVertex;
import com.alibaba.graphscope.common.ir.rel.metadata.schema.GlogueSchema;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.javatuples.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -156,12 +157,20 @@ private boolean containsPattern(Pattern pattern) {

@Override
public Double getCardinality(Pattern queryPattern) {
return getCardinality(queryPattern, false);
}

public @Nullable Double getCardinality(Pattern queryPattern, boolean allowsNull) {
for (Pattern pattern : this.patternCardinality.keySet()) {
if (pattern.equals(queryPattern)) {
return this.patternCardinality.get(pattern);
}
}
if (allowsNull) {
return null;
}
// if not exist, return 1.0
logger.warn("pattern {} not found in glogue, return count = 1.0", queryPattern);
return 1.0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alibaba.graphscope.common.ir.rel.metadata.glogue.pattern.*;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Set;

public class GlogueQuery {
Expand Down Expand Up @@ -48,10 +50,11 @@ public Set<GlogueEdge> getOutEdges(Pattern pattern) {
/**
* get pattern count
* @param pattern
* @param allowsNull if the flag is set to true, return null when the pattern is not found, otherwise return 1.0d
* @return
*/
public Double getRowCount(Pattern pattern) {
return glogue.getRowCount(pattern);
public @Nullable Double getRowCount(Pattern pattern, boolean allowsNull) {
return glogue.getRowCount(pattern, allowsNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ public boolean equals(Object o) {
return Double.compare(details.selectivity, selectivity) == 0
&& Objects.equals(range, details.range)
&& Objects.equals(pxdInnerGetVTypes, details.pxdInnerGetVTypes)
&& optional == details.optional;
&& optional == details.optional
&& Objects.equals(resultOpt, details.resultOpt)
&& Objects.equals(pathOpt, details.pathOpt);
}

@Override
public int hashCode() {
return Objects.hash(selectivity, range, optional);
return Objects.hash(selectivity, range, optional, pxdInnerGetVTypes, resultOpt, pathOpt);
}

public double getSelectivity() {
Expand Down Expand Up @@ -117,20 +119,6 @@ public GraphOpt.PathExpandPath getPathOpt() {

@Override
public int compareTo(ElementDetails o) {
int compare = Double.compare(this.selectivity, o.selectivity);
if (compare != 0) {
return compare;
}
compare = Boolean.compare(this.optional, o.optional);
if (compare != 0) {
return compare;
}
if (!this.pxdInnerGetVTypes.equals(o.pxdInnerGetVTypes)) {
return -1;
}
if (this.range != null && o.range != null) {
return this.range.compareTo(o.range);
}
return this.range == null && o.range == null ? 0 : this.range == null ? -1 : 1;
return o == null ? -1 : this.hashCode() - o.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -691,46 +691,44 @@ public void ldbc10_test() {
+ " LogicalJoin(condition=[AND(=(person, person), =(friend, friend))],"
+ " joinType=[anti])\n"
+ " LogicalFilter(condition=[<>(friend, person)])\n"
+ " MultiJoin(joinFilter=[=(post1, post1)],"
+ " GraphPhysicalGetV(tableConfig=[{isAll=false, tables=[POST]}],"
+ " alias=[post], opt=[START], physicalOpt=[ITSELF])\n"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(HASCREATOR,"
+ " POST, PERSON)]], alias=[_], startAlias=[friend], opt=[IN],"
+ " physicalOpt=[VERTEX], optional=[true])\n"
+ " MultiJoin(joinFilter=[=(tag, tag)],"
+ " isFullOuterJoin=[false], joinTypes=[[INNER, INNER]],"
+ " outerJoinConditions=[[NULL, NULL]], projFields=[[ALL, ALL]])\n"
+ " GraphPhysicalGetV(tableConfig=[{isAll=false,"
+ " tables=[POST]}], alias=[post1], opt=[START], physicalOpt=[ITSELF])\n"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(HASCREATOR,"
+ " POST, PERSON)]], alias=[_], startAlias=[friend], opt=[IN],"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(HASTAG,"
+ " POST, TAG)]], alias=[tag], startAlias=[post1], opt=[OUT],"
+ " physicalOpt=[VERTEX], optional=[true])\n"
+ " CommonTableScan(table=[[common#-1626533514]])\n"
+ " GraphPhysicalGetV(tableConfig=[{isAll=false,"
+ " tables=[POST]}], alias=[post1], opt=[START], physicalOpt=[ITSELF])\n"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(HASTAG, POST,"
+ " TAG)]], alias=[_], startAlias=[tag], opt=[IN], physicalOpt=[VERTEX],"
+ " optional=[true])\n"
+ " CommonTableScan(table=[[common#-1626533514]])\n"
+ " CommonTableScan(table=[[common#-2135802270]])\n"
+ " GraphPhysicalExpand(tableConfig=[{isAll=false,"
+ " tables=[HASINTEREST]}], alias=[tag], startAlias=[person], opt=[OUT],"
+ " physicalOpt=[VERTEX], optional=[true])\n"
+ " CommonTableScan(table=[[common#-2135802270]])\n"
+ " GraphPhysicalExpand(tableConfig=[{isAll=false, tables=[KNOWS]}],"
+ " alias=[friend], startAlias=[person], opt=[BOTH], physicalOpt=[VERTEX])\n"
+ " GraphLogicalSource(tableConfig=[{isAll=false,"
+ " tables=[PERSON]}], alias=[person], opt=[VERTEX], uniqueKeyFilters=[=(_.id,"
+ " ?0)])\n"
+ "common#-1626533514:\n"
+ "GraphPhysicalGetV(tableConfig=[{isAll=false, tables=[POST]}], alias=[post],"
+ "common#-2135802270:\n"
+ "GraphPhysicalGetV(tableConfig=[{isAll=false, tables=[POST]}], alias=[post1],"
+ " opt=[START], physicalOpt=[ITSELF])\n"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(HASCREATOR, POST, PERSON)]],"
+ " alias=[_], startAlias=[friend], opt=[IN], physicalOpt=[VERTEX],"
+ " optional=[true])\n"
+ " GraphPhysicalExpand(tableConfig=[{isAll=false, tables=[HASINTEREST]}],"
+ " alias=[tag], startAlias=[person], opt=[OUT], physicalOpt=[VERTEX],"
+ " optional=[true])\n"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(ISLOCATEDIN, PERSON,"
+ " GraphPhysicalExpand(tableConfig=[[EdgeLabel(ISLOCATEDIN, PERSON,"
+ " PLACE)]], alias=[city], startAlias=[friend], opt=[OUT],"
+ " physicalOpt=[VERTEX])\n"
+ " GraphLogicalGetV(tableConfig=[{isAll=false, tables=[PERSON]}],"
+ " GraphLogicalGetV(tableConfig=[{isAll=false, tables=[PERSON]}],"
+ " alias=[friend], opt=[END])\n"
+ " "
+ " "
+ " GraphLogicalPathExpand(fused=[GraphPhysicalExpand(tableConfig=[{isAll=false,"
+ " tables=[KNOWS]}], alias=[_], opt=[BOTH], physicalOpt=[VERTEX])\n"
+ "], offset=[2], fetch=[1], path_opt=[ARBITRARY], result_opt=[END_V],"
+ " alias=[_], start_alias=[person])\n"
+ " GraphLogicalSource(tableConfig=[{isAll=false, tables=[PERSON]}],"
+ " GraphLogicalSource(tableConfig=[{isAll=false, tables=[PERSON]}],"
+ " alias=[person], opt=[VERTEX], uniqueKeyFilters=[=(_.id, ?0)])",
com.alibaba.graphscope.common.ir.tools.Utils.toString(after).trim());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public void CBO_4_3_test() {
+ " 90491.99999999999\n"
+ " GraphPhysicalExpand(tableConfig=[{isAll=false, tables=[HASINTEREST]}],"
+ " alias=[post], startAlias=[person1], opt=[OUT], physicalOpt=[VERTEX]):"
+ " rowcount = 1.0\n"
+ " rowcount = 446.2587786663215\n"
+ " CommonTableScan(table=[[common#114550231]]): rowcount ="
+ " 90491.99999999999\n"
+ "common#114550231:\n"
Expand Down

0 comments on commit 7567552

Please sign in to comment.