Search in sources :

Example 16 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SchemaTableTree method removeObsoleteHasContainers.

/**
 * remove "has" containers that are not valid anymore
 *
 * @param schemaTableTree the current table tree
 */
private void removeObsoleteHasContainers(final SchemaTableTree schemaTableTree) {
    Set<HasContainer> toRemove = new HashSet<>();
    for (HasContainer hasContainer : schemaTableTree.hasContainers) {
        if (hasContainer.getKey().equals(label.getAccessor())) {
            toRemove.add(hasContainer);
        }
        if (Existence.NULL.equals(hasContainer.getBiPredicate())) {
            // we checked that a non existing property was null, that's fine
            if (!this.getFilteredAllTables().get(schemaTableTree.getSchemaTable().toString()).containsKey(hasContainer.getKey())) {
                toRemove.add(hasContainer);
            }
        }
    }
    schemaTableTree.hasContainers.removeAll(toRemove);
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 17 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SchemaTableTree method bulkWithJoin.

private String bulkWithJoin(SqlgGraph sqlgGraph) {
    StringBuilder sb = new StringBuilder();
    List<HasContainer> bulkHasContainers = this.hasContainers.stream().filter(h -> SqlgUtil.isBulkWithinAndOut(sqlgGraph, h)).collect(Collectors.toList());
    for (HasContainer hasContainer : bulkHasContainers) {
        P<List<Object>> predicate = (P<List<Object>>) hasContainer.getPredicate();
        Collection<Object> withInList = predicate.getValue();
        Set<Object> withInOuts = new HashSet<>(withInList);
        Map<String, PropertyType> columns = new HashMap<>();
        Object next = withInOuts.iterator().next();
        if (next instanceof RecordId) {
            next = ((RecordId) next).getId();
        }
        if (hasContainer.getBiPredicate() == Contains.within) {
            columns.put(WITHIN, PropertyType.from(next));
        } else if (hasContainer.getBiPredicate() == Contains.without) {
            columns.put(WITHOUT, PropertyType.from(next));
        } else {
            throw new UnsupportedOperationException("Only Contains.within and Contains.without is supported!");
        }
        if (hasContainer.getBiPredicate() == Contains.within) {
            sb.append(" INNER JOIN\n\t");
        } else {
            // left join and in the where clause add a IS NULL, to find the values not in the right hand table
            sb.append(" LEFT JOIN\n\t");
        }
        sb.append("(VALUES ");
        boolean first = true;
        for (Object withInOutValue : withInOuts) {
            if (!first) {
                sb.append(", ");
            }
            first = false;
            if (withInOutValue instanceof RecordId) {
                withInOutValue = ((RecordId) withInOutValue).getId();
            }
            sb.append("(");
            PropertyType propertyType = PropertyType.from(withInOutValue);
            sb.append(sqlgGraph.getSqlDialect().valueToValuesString(propertyType, withInOutValue));
            sb.append(")");
        }
        sb.append(") as tmp");
        sb.append(this.rootSchemaTableTree().tmpTableAliasCounter);
        sb.append("(");
        if (hasContainer.getBiPredicate() == Contains.within) {
            sb.append(WITHIN);
        } else {
            sb.append(WITHOUT);
        }
        sb.append(") ");
        sb.append(" on ");
        sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.getSchemaTable().getSchema()));
        sb.append(".");
        sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.getSchemaTable().getTable()));
        sb.append(".");
        if (hasContainer.getKey().equals(T.id.getAccessor())) {
            sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
        } else {
            sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey()));
        }
        if (hasContainer.getBiPredicate() == Contains.within) {
            sb.append(" = tmp");
            sb.append(this.rootSchemaTableTree().tmpTableAliasCounter++);
            sb.append(".within");
        } else {
            sb.append(" = tmp");
            sb.append(this.rootSchemaTableTree().tmpTableAliasCounter++);
            sb.append(".without");
        }
    }
    return sb.toString();
}
Also used : org.apache.tinkerpop.gremlin.process.traversal(org.apache.tinkerpop.gremlin.process.traversal) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) java.util(java.util) SelectOneStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep) SqlgUtil(org.umlg.sqlg.util.SqlgUtil) FullText(org.umlg.sqlg.predicate.FullText) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) SecureRandom(java.security.SecureRandom) SQLException(java.sql.SQLException) Pair(org.apache.commons.lang3.tuple.Pair) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) ResultSet(java.sql.ResultSet) org.apache.tinkerpop.gremlin.structure(org.apache.tinkerpop.gremlin.structure) PropertyType(org.umlg.sqlg.structure.PropertyType) EDGE_PREFIX(org.umlg.sqlg.structure.topology.Topology.EDGE_PREFIX) Splitter(com.google.common.base.Splitter) Triple(org.apache.commons.lang3.tuple.Triple) VERTEX_PREFIX(org.umlg.sqlg.structure.topology.Topology.VERTEX_PREFIX) org.umlg.sqlg.structure(org.umlg.sqlg.structure) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Schema(org.umlg.sqlg.structure.topology.Schema) Predicate(java.util.function.Predicate) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) T.label(org.apache.tinkerpop.gremlin.structure.T.label) Collectors(java.util.stream.Collectors) Topology(org.umlg.sqlg.structure.topology.Topology) Existence(org.umlg.sqlg.predicate.Existence) Preconditions(com.google.common.base.Preconditions) TokenTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.TokenTraversal) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) org.umlg.sqlg.strategy(org.umlg.sqlg.strategy) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) PropertyType(org.umlg.sqlg.structure.PropertyType)

Example 18 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SchemaTableTree method toWhereClause.

private String toWhereClause(SqlgGraph sqlgGraph, MutableBoolean printedWhere) {
    final StringBuilder result = new StringBuilder();
    if (sqlgGraph.getSqlDialect().supportsBulkWithinOut()) {
        for (HasContainer hasContainer : this.hasContainers) {
            if (!SqlgUtil.isBulkWithin(sqlgGraph, hasContainer)) {
                if (!printedWhere.booleanValue()) {
                    printedWhere.setTrue();
                    result.append("\nWHERE\n\t(");
                } else {
                    result.append(" AND (");
                }
                WhereClause whereClause = WhereClause.from(hasContainer.getPredicate());
                result.append(" ").append(whereClause.toSql(sqlgGraph, this, hasContainer)).append(")");
            }
        }
    } else {
        for (HasContainer hasContainer : this.getHasContainers()) {
            if (!printedWhere.booleanValue()) {
                printedWhere.setTrue();
                result.append("\nWHERE\n\t(");
            } else {
                result.append(" AND (");
            }
            WhereClause whereClause = WhereClause.from(hasContainer.getPredicate());
            result.append(" ").append(whereClause.toSql(sqlgGraph, this, hasContainer)).append(")");
            if (sqlgGraph.getSqlDialect().isMariaDb()) {
                result.append(" COLLATE latin1_general_cs");
            }
        }
    }
    for (AndOrHasContainer andOrHasContainer : this.andOrHasContainers) {
        if (!printedWhere.booleanValue()) {
            printedWhere.setTrue();
            result.append("\nWHERE ");
        } else {
            result.append(" AND ");
        }
        andOrHasContainer.toSql(sqlgGraph, this, result);
    }
    return result.toString();
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 19 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SqlgHasStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal);
    for (HasStep<?> hasStep : hasSteps) {
        SqlgHasStep sqlgHasStep = new SqlgHasStep(hasStep.getTraversal(), hasStep.getHasContainers().toArray(new HasContainer[] {}));
        for (String label : hasStep.getLabels()) {
            sqlgHasStep.addLabel(label);
        }
        TraversalHelper.replaceStep(hasStep, sqlgHasStep, hasStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) SqlgHasStep(org.umlg.sqlg.step.SqlgHasStep) SqlgHasStep(org.umlg.sqlg.step.SqlgHasStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 20 with HasContainer

use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.

the class SqlgWhereStrategy method apply.

@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
    // The query will read from the cache if this is for a cached vertex
    if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
        sqlgGraph.tx().flush();
    }
    List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
    ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
    // get all steps per label
    Map<String, Object> stepsByLabel = new HashMap<>();
    stepIterator = steps.listIterator();
    Step<?, ?> previous = null;
    int idx = 0;
    while (stepIterator.hasNext()) {
        Step<?, ?> step = stepIterator.next();
        captureLabels(step, stepsByLabel);
        if (step instanceof WherePredicateStep<?>) {
            WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
            if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
                Object referTo = previous;
                if (wps.getStartKey().isPresent()) {
                    referTo = stepsByLabel.get(wps.getStartKey().get());
                }
                if (referTo instanceof SqlgGraphStep<?, ?>) {
                    SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
                    if (sgs.getReplacedSteps().size() > 0) {
                        referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
                    }
                }
                if (referTo instanceof ReplacedStep<?, ?>) {
                    ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
                    rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
                    traversal.removeStep(idx);
                }
            }
        }
        previous = step;
        idx++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Aggregations

HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)56 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)13 P (org.apache.tinkerpop.gremlin.process.traversal.P)9 Collectors (java.util.stream.Collectors)7 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)6 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)6 FullText (org.umlg.sqlg.predicate.FullText)6 PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)6 java.util (java.util)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 BiPredicate (java.util.function.BiPredicate)5 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 JSONObject (org.json.JSONObject)5 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep)4 BasicVertexCentricQueryBuilder (org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 HasContainerHolder (org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 ConnectiveP (org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP)3