Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.hugegraph.exception.NotFoundException;
import org.apache.hugegraph.exception.NotSupportException;
import org.apache.hugegraph.iterator.FilterIterator;
import org.apache.hugegraph.schema.IndexLabel;
import org.apache.hugegraph.schema.PropertyKey;
import org.apache.hugegraph.schema.SchemaLabel;
import org.apache.hugegraph.structure.HugeElement;
Expand All @@ -68,6 +69,7 @@
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.MatchStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.MaxGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.MeanGlobalStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.MinGlobalStep;
Expand Down Expand Up @@ -172,6 +174,27 @@ public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
Step<?, ?> nextStep = step.getNextStep();
if (step instanceof HasStep) {
HasContainerHolder holder = (HasContainerHolder) step;
/*
* Range/neq predicates before match() may trigger a no-index
* query after MatchStep reorders filters. Keep known-indexed
* boolean predicates pushed down, and leave the rest for
* TinkerPop to evaluate.
*/
if (followedByMatchStep(step) &&
hasUnusableMatchPredicate(newStep, holder)) {
List<HasContainer> extracted =
extractUsableHasContainers(newStep, holder);
for (HasContainer has : extracted) {
holder.removeHasContainer(has);
}
if (holder.getHasContainers().isEmpty()) {
TraversalHelper.copyLabels(step, step.getPreviousStep(),
false);
traversal.removeStep(step);
}
step = nextStep;
continue;
}
Comment thread
LegendPei marked this conversation as resolved.
if (extractHasContainers(newStep, holder)) {
TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
traversal.removeStep(step);
Expand All @@ -181,6 +204,203 @@ public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
}
}

private static boolean followedByMatchStep(Step<?, ?> step) {
Step<?, ?> next = step.getNextStep();
while (next instanceof HasStep ||
next instanceof NoOpBarrierStep ||
next instanceof IdentityStep) {
next = next.getNextStep();
}
return next instanceof MatchStep;
}

private static boolean hasUnusableMatchPredicate(HugeGraphStep<?, ?> step,
HasContainerHolder holder) {
HugeGraph graph = tryGetGraph(step);
for (HasContainer has : holder.getHasContainers()) {
if (!hasMatchIndexSensitivePredicate(has)) {
continue;
}
if (graph == null || !hasUsableMatchIndex(graph, step, has)) {
return true;
}
}
return false;
}

private static List<HasContainer> extractUsableHasContainers(
HugeGraphStep<?, ?> step, HasContainerHolder holder) {
List<HasContainer> extracted = new ArrayList<>();
HugeGraph graph = tryGetGraph(step);
for (HasContainer has : holder.getHasContainers()) {
if (hasMatchIndexSensitivePredicate(has) &&
(graph == null || !hasUsableMatchIndex(graph, step, has))) {
continue;
}
if (!canExtractHasContainer(graph, has)) {
continue;
}
if (!GraphStep.processHasContainerIds(step, has)) {
step.addHasContainer(has);
}
extracted.add(has);
}
return extracted;
}

private static boolean hasMatchIndexSensitivePredicate(HasContainer has) {
if (hasNullPredicate(has)) {
return true;
}
List<P<Object>> predicates = new ArrayList<>();
collectPredicates(predicates, ImmutableList.of(has.getPredicate()));
for (P<Object> pred : predicates) {
BiPredicate<?, ?> bp = pred.getBiPredicate();
if (bp == Compare.neq ||
bp == Compare.gt || bp == Compare.gte ||
bp == Compare.lt || bp == Compare.lte) {
return true;
}
}
return false;
}

private static boolean hasUsableMatchIndex(HugeGraph graph,
HugeGraphStep<?, ?> step,
HasContainer has) {
if (isSysProp(has.getKey())) {
return false;
}

PropertyKey pkey;
try {
pkey = graph.propertyKey(has.getKey());
} catch (NotFoundException e) {
return false;
}
if (!hasOnlyUsableNeqPredicates(pkey, has)) {
return false;
}
if (!canExtractHasContainer(graph, has)) {
return false;
}

Collection<? extends SchemaLabel> schemaLabels = step.returnsVertex() ?
graph.vertexLabels() :
graph.edgeLabels();
boolean seen = false;
Comment thread
LegendPei marked this conversation as resolved.
for (SchemaLabel schemaLabel : schemaLabels) {
if (!schemaLabel.properties().contains(pkey.id())) {
continue;
}
seen = true;
if (pkey.dataType() == DataType.BOOLEAN &&
!hasBooleanIndex(graph, schemaLabel, pkey)) {
return false;
}
if (pkey.dataType().isNumber() &&
(!hasOnlyRangePredicates(has) ||
!hasRangeIndex(graph, schemaLabel, pkey))) {
return false;
}
if (pkey.dataType() != DataType.BOOLEAN &&
!pkey.dataType().isNumber()) {
return false;
}
}
return seen;
}

private static boolean hasOnlyUsableNeqPredicates(PropertyKey pkey,
HasContainer has) {
if (hasNullPredicate(has)) {
return false;
}
List<P<Object>> predicates = new ArrayList<>();
collectPredicates(predicates, ImmutableList.of(has.getPredicate()));
for (P<Object> pred : predicates) {
if (pred.getBiPredicate() != Compare.neq) {
continue;
}
if (pkey.dataType() == DataType.BOOLEAN &&
pred.getValue() instanceof Boolean) {
continue;
}
return false;
}
return true;
}

private static boolean hasNullPredicate(HasContainer has) {
List<P<Object>> predicates = new ArrayList<>();
collectPredicates(predicates, ImmutableList.of(has.getPredicate()));
for (P<Object> pred : predicates) {
if (pred.getValue() == null) {
return true;
}
}
return false;
}

private static boolean hasBooleanIndex(HugeGraph graph,
SchemaLabel schemaLabel,
PropertyKey pkey) {
for (Id id : schemaLabel.indexLabels()) {
IndexLabel indexLabel = indexLabelOrNull(graph, id);
if (indexLabel == null ||
!matchSingleFieldIndex(indexLabel, pkey)) {
continue;
}
if (indexLabel.indexType().isSecondary()) {
return true;
}
}
return false;
Comment thread
LegendPei marked this conversation as resolved.
}

private static boolean hasRangeIndex(HugeGraph graph,
SchemaLabel schemaLabel,
PropertyKey pkey) {
for (Id id : schemaLabel.indexLabels()) {
IndexLabel indexLabel = indexLabelOrNull(graph, id);
if (indexLabel == null ||
!matchSingleFieldIndex(indexLabel, pkey)) {
continue;
}
if (indexLabel.indexType().isRange()) {
return true;
}
}
return false;
Comment thread
LegendPei marked this conversation as resolved.
}

static IndexLabel indexLabelOrNull(HugeGraph graph, Id id) {
try {
return graph.indexLabel(id);
} catch (IllegalArgumentException e) {
return null;
}
}

private static boolean matchSingleFieldIndex(IndexLabel indexLabel,
PropertyKey pkey) {
return indexLabel.indexFields().size() == 1 &&
indexLabel.indexField().equals(pkey.id());
}

private static boolean hasOnlyRangePredicates(HasContainer has) {
List<P<Object>> predicates = new ArrayList<>();
collectPredicates(predicates, ImmutableList.of(has.getPredicate()));
for (P<Object> pred : predicates) {
BiPredicate<?, ?> bp = pred.getBiPredicate();
if (bp != Compare.gt && bp != Compare.gte &&
bp != Compare.lt && bp != Compare.lte) {
return false;
}
}
return true;
}

public static void extractHasContainer(HugeVertexStep<?> newStep,
Traversal.Admin<?, ?> traversal) {
Step<?, ?> step = newStep;
Expand Down Expand Up @@ -248,6 +468,9 @@ static boolean canExtractHasContainer(HugeGraph graph,
} catch (NotFoundException e) {
return false;
}
if (hasNullPredicate(has)) {
return false;
}
if (!pkey.dataType().isText()) {
return true;
}
Expand Down Expand Up @@ -775,6 +998,10 @@ private static void updatePredicateValue(P<?> predicate, PropertyKey pkey) {
List<P<Object>> leafPredicates = new ArrayList<>();
collectPredicates(leafPredicates, ImmutableList.of(predicate));
for (P<Object> pred : leafPredicates) {
if (pred.getBiPredicate() == Compare.neq &&
pred.getValue() == null) {
continue;
}
Object value = validPropertyValue(pred.getValue(), pkey);
pred.setValue(value);
}
Expand Down
Loading
Loading