Search in sources :

Example 1 with SelectOneStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep in project sqlg by pietermartin.

the class Emit method evaluateElementValueTraversal.

/**
 * @param pathSize Indicates the head object path size.
 *                 For SqlgVertexStepCompile they are objects that are already on the path before the step is executed.
 */
public void evaluateElementValueTraversal(int pathSize, Traverser.Admin<E> traverser) {
    if (this.comparatorValues == null) {
        this.comparatorValues = new ArrayList<>();
    }
    // loop in reverse, from end to start.
    for (int i = this.sqlgComparatorHolders.size() - 1; i >= 0; i--) {
        SqlgElement sqlgElement;
        SqlgComparatorHolder comparatorHolder = this.sqlgComparatorHolders.get(i);
        if (comparatorHolder.hasComparators()) {
            if (comparatorHolder.hasPrecedingSelectOneLabel()) {
                // Go get the element to compare against.
                String precedingLabel = comparatorHolder.getPrecedingSelectOneLabel();
                sqlgElement = traverser.path().get(precedingLabel);
            } else {
                sqlgElement = (SqlgElement) traverser.path().objects().get(i + pathSize);
            }
            for (Pair<Traversal.Admin<?, ?>, Comparator<?>> traversalComparator : comparatorHolder.getComparators()) {
                Traversal.Admin<?, ?> traversal = traversalComparator.getValue0();
                Comparator comparator = traversalComparator.getValue1();
                if (traversal.getSteps().size() == 1 && traversal.getSteps().get(0) instanceof SelectOneStep) {
                    // xxxxx.select("a").order().by(select("a").by("name"), Order.decr)
                    SelectOneStep selectOneStep = (SelectOneStep) traversal.getSteps().get(0);
                    Preconditions.checkState(selectOneStep.getScopeKeys().size() == 1, "toOrderByClause expects the selectOneStep to have one scopeKey!");
                    Preconditions.checkState(selectOneStep.getLocalChildren().size() == 1, "toOrderByClause expects the selectOneStep to have one traversal!");
                    Preconditions.checkState(selectOneStep.getLocalChildren().get(0) instanceof ElementValueTraversal || selectOneStep.getLocalChildren().get(0) instanceof TokenTraversal, "toOrderByClause expects the selectOneStep's traversal to be a ElementValueTraversal or a TokenTraversal!");
                    String selectKey = (String) selectOneStep.getScopeKeys().iterator().next();
                    SqlgElement sqlgElementSelect = traverser.path().get(selectKey);
                    Traversal.Admin<?, ?> t = (Traversal.Admin<?, ?>) selectOneStep.getLocalChildren().get(0);
                    if (t instanceof ElementValueTraversal) {
                        ElementValueTraversal elementValueTraversal = (ElementValueTraversal) t;
                        this.comparatorValues.add(Pair.with(sqlgElementSelect.value(elementValueTraversal.getPropertyKey()), comparator));
                    } else {
                        TokenTraversal tokenTraversal = (TokenTraversal) t;
                        this.comparatorValues.add(Pair.with(tokenTraversal.getToken().apply(sqlgElementSelect), comparator));
                    }
                } else if (traversal instanceof IdentityTraversal) {
                    // This is for Order.shuffle, Order.shuffle can not be used in Collections.sort(), it violates the sort contract.
                    // Basically its a crap comparator.
                    this.comparatorValues.add(Pair.with(new Random().nextInt(), Order.incr));
                } else if (traversal instanceof ElementValueTraversal) {
                    ElementValueTraversal elementValueTraversal = (ElementValueTraversal) traversal;
                    this.comparatorValues.add(Pair.with(sqlgElement.value(elementValueTraversal.getPropertyKey()), comparator));
                } else if (traversal instanceof TokenTraversal) {
                    TokenTraversal tokenTraversal = (TokenTraversal) traversal;
                    this.comparatorValues.add(Pair.with(tokenTraversal.getToken().apply(sqlgElement), comparator));
                } else {
                    throw new IllegalStateException("Unhandled traversal " + traversal.getClass().getName());
                }
            }
        }
    }
}
Also used : SqlgElement(org.umlg.sqlg.structure.SqlgElement) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) IdentityTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) SelectOneStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) IdentityTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal)

Example 2 with SelectOneStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep in project sqlg by pietermartin.

the class ReplacedStepTree method orderByHasSelectOneStepAndForLabelNotInTree.

/**
 * This happens when a SqlgVertexStep has a SelectOne step where the label is for an element on the path
 * that is before the current optimized steps.
 * @return
 */
public boolean orderByHasSelectOneStepAndForLabelNotInTree() {
    Set<String> labels = new HashSet<>();
    for (ReplacedStep<?, ?> replacedStep : linearPathToLeafNode()) {
        for (String label : labels) {
            labels.add(SqlgUtil.originalLabel(label));
        }
        for (Pair<Traversal.Admin<?, ?>, Comparator<?>> objects : replacedStep.getSqlgComparatorHolder().getComparators()) {
            Traversal.Admin<?, ?> traversal = objects.getValue0();
            if (traversal.getSteps().size() == 1 && traversal.getSteps().get(0) instanceof SelectOneStep) {
                // xxxxx.select("a").order().by(select("a").by("name"), Order.decr)
                SelectOneStep selectOneStep = (SelectOneStep) traversal.getSteps().get(0);
                Preconditions.checkState(selectOneStep.getScopeKeys().size() == 1, "toOrderByClause expects the selectOneStep to have one scopeKey!");
                Preconditions.checkState(selectOneStep.getLocalChildren().size() == 1, "toOrderByClause expects the selectOneStep to have one traversal!");
                Preconditions.checkState(selectOneStep.getLocalChildren().get(0) instanceof ElementValueTraversal || selectOneStep.getLocalChildren().get(0) instanceof TokenTraversal, "toOrderByClause expects the selectOneStep's traversal to be a ElementValueTraversal or a TokenTraversal!");
                String selectKey = (String) selectOneStep.getScopeKeys().iterator().next();
                if (!labels.contains(selectKey)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) SelectOneStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal)

Aggregations

Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)2 ElementValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal)2 TokenTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal)2 SelectOneStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep)2 IdentityTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.IdentityTraversal)1 SqlgElement (org.umlg.sqlg.structure.SqlgElement)1