Search in sources :

Example 11 with HasContainer

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

the class BaseStrategy method optimizeHas.

private List<HasContainer> optimizeHas(ReplacedStep<?, ?> replacedStep, List<HasContainer> hasContainers) {
    List<HasContainer> result = new ArrayList<>();
    for (HasContainer hasContainer : hasContainers) {
        if (hasContainerKeyNotIdOrLabel(hasContainer) && SUPPORTED_BI_PREDICATE.contains(hasContainer.getBiPredicate())) {
            replacedStep.addHasContainer(hasContainer);
            result.add(hasContainer);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AndOrHasContainer(org.umlg.sqlg.sql.parse.AndOrHasContainer)

Example 12 with HasContainer

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

the class AndOrHasContainer method toSql.

private void toSql(SqlgGraph sqlgGraph, SchemaTableTree schemaTableTree, StringBuilder result, int depth) {
    if (!this.hasContainers.isEmpty()) {
        boolean first = true;
        for (HasContainer h : this.hasContainers) {
            if (!SqlgUtil.isBulkWithin(sqlgGraph, h)) {
                if (first) {
                    first = false;
                    result.append("(");
                } else {
                    result.append(" AND ");
                }
                String k = h.getKey();
                WhereClause whereClause = WhereClause.from(h.getPredicate());
                // check if property exists
                String bool = null;
                if (!k.equals(T.id.getAccessor())) {
                    Map<String, PropertyType> pts = sqlgGraph.getTopology().getTableFor(schemaTableTree.getSchemaTable());
                    if (pts != null && !pts.containsKey(k)) {
                        // verify if we have a value
                        Multimap<String, Object> keyValueMap = LinkedListMultimap.create();
                        whereClause.putKeyValueMap(h, keyValueMap);
                        // we do
                        if (keyValueMap.size() > 0) {
                            bool = "? is null";
                        } else {
                            if (Existence.NULL.equals(h.getBiPredicate())) {
                                bool = "1=1";
                            } else {
                                bool = "1=0";
                            }
                        }
                    }
                }
                if (bool != null) {
                    result.append(bool);
                } else {
                    result.append(whereClause.toSql(sqlgGraph, schemaTableTree, h));
                }
            }
        }
        if (!first) {
            result.append(")");
        }
    }
    int count = 1;
    if (!this.andOrHasContainers.isEmpty()) {
        result.append("\n");
        for (int i = 0; i < depth; i++) {
            result.append("\t");
        }
        result.append("(");
    }
    for (AndOrHasContainer andOrHasContainer : this.andOrHasContainers) {
        andOrHasContainer.toSql(sqlgGraph, schemaTableTree, result, depth + 1);
        if (count++ < this.andOrHasContainers.size()) {
            switch(this.type) {
                case AND:
                    result.append(" AND ");
                    break;
                case OR:
                    result.append(" OR ");
                    break;
                case NONE:
                    break;
            }
        }
    }
    if (!this.andOrHasContainers.isEmpty()) {
        result.append("\n");
        for (int i = 0; i < depth - 1; i++) {
            result.append("\t");
        }
        result.append(")");
    }
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) PropertyType(org.umlg.sqlg.structure.PropertyType)

Example 13 with HasContainer

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

the class ReplacedStep method addIdHasContainer.

/**
 * Each id is for a specific label, add the label to the {@link ReplacedStep#labelHasContainers}
 *
 * @param hasContainer A hasContainer with {@link T#id} as key for this step. Copied from the original step.
 */
public void addIdHasContainer(HasContainer hasContainer) {
    Preconditions.checkState(BaseStrategy.SUPPORTED_ID_BI_PREDICATE.contains(hasContainer.getBiPredicate()), "Only " + BaseStrategy.SUPPORTED_ID_BI_PREDICATE.toString() + " is supported, found " + hasContainer.getBiPredicate().getClass().toString());
    Object rawId = hasContainer.getValue();
    if (rawId instanceof Collection) {
        Collection<Object> ids = (Collection<Object>) rawId;
        Set<String> idLabels = new HashSet<>();
        for (Object id : ids) {
            if (id instanceof RecordId) {
                RecordId recordId = (RecordId) id;
                idLabels.add(recordId.getSchemaTable().toString());
            } else if (id instanceof Element) {
                SqlgElement sqlgElement = (SqlgElement) id;
                RecordId recordId = (RecordId) sqlgElement.id();
                idLabels.add(recordId.getSchemaTable().toString());
            } else if (id instanceof String) {
                RecordId recordId = RecordId.from(id);
                idLabels.add(recordId.getSchemaTable().toString());
            } else {
                throw new IllegalStateException("id must be an Element or a RecordId, found " + id.getClass().toString());
            }
        }
        if (hasContainer.getBiPredicate() == Contains.without) {
            // without indicates that the label needs to be queried along with the rest, 'or' logic rather than 'and'.
            if (!this.labelHasContainers.isEmpty()) {
                Object previousHasContainerLabels = this.labelHasContainers.get(this.labelHasContainers.size() - 1).getValue();
                List<String> mergedLabels;
                if (previousHasContainerLabels instanceof Collection) {
                    Collection<String> labels = (Collection<String>) previousHasContainerLabels;
                    mergedLabels = new ArrayList<>(labels);
                } else {
                    String label = (String) previousHasContainerLabels;
                    mergedLabels = new ArrayList<>();
                    mergedLabels.add(label);
                }
                mergedLabels.addAll(idLabels);
                this.labelHasContainers.set(this.labelHasContainers.size() - 1, new HasContainer(T.label.getAccessor(), P.within(mergedLabels)));
            }
        } else {
            this.labelHasContainers.add(new HasContainer(T.label.getAccessor(), P.within(idLabels)));
        }
    } else {
        RecordId recordId;
        if (rawId instanceof RecordId) {
            recordId = (RecordId) rawId;
        } else if (rawId instanceof Element) {
            SqlgElement sqlgElement = (SqlgElement) rawId;
            recordId = (RecordId) sqlgElement.id();
        } else if (rawId instanceof String) {
            recordId = RecordId.from(rawId);
        } else {
            throw new IllegalStateException("id must be an Element or a RecordId, found " + id.getClass().toString());
        }
        if (hasContainer.getBiPredicate() == Compare.neq) {
            if (!this.labelHasContainers.isEmpty()) {
                Object previousHasContainerLabels = this.labelHasContainers.get(this.labelHasContainers.size() - 1).getValue();
                List<String> mergedLabels;
                if (previousHasContainerLabels instanceof Collection) {
                    Collection<String> labels = (Collection<String>) previousHasContainerLabels;
                    mergedLabels = new ArrayList<>(labels);
                } else {
                    String label = (String) previousHasContainerLabels;
                    mergedLabels = new ArrayList<>();
                    mergedLabels.add(label);
                }
                mergedLabels.add(recordId.getSchemaTable().toString());
                this.labelHasContainers.set(this.labelHasContainers.size() - 1, new HasContainer(T.label.getAccessor(), P.within(mergedLabels)));
            }
        } else {
            this.labelHasContainers.add(new HasContainer(T.label.getAccessor(), P.eq(recordId.getSchemaTable().toString())));
        }
    }
    this.idHasContainers.add(hasContainer);
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 14 with HasContainer

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

the class ReplacedStep method addIdHasContainers.

private void addIdHasContainers(SchemaTableTree schemaTableTree1, List<Multimap<BiPredicate, RecordId>> biPredicateRecordIds) {
    if (biPredicateRecordIds != null) {
        for (Multimap<BiPredicate, RecordId> biPredicateRecordId : biPredicateRecordIds) {
            for (BiPredicate biPredicate : biPredicateRecordId.keySet()) {
                Collection<RecordId> recordIds = biPredicateRecordId.get(biPredicate);
                HasContainer idHasContainer;
                // id hasContainers are only optimized for BaseStrategy.SUPPORTED_ID_BI_PREDICATE within, without, eq, neq
                if (biPredicate == Contains.without || biPredicate == Contains.within) {
                    idHasContainer = new HasContainer(T.id.getAccessor(), P.test(biPredicate, recordIds));
                    schemaTableTree1.getHasContainers().add(idHasContainer);
                } else {
                    Preconditions.checkState(biPredicate == Compare.eq || biPredicate == Compare.neq);
                    for (RecordId recordId : recordIds) {
                        idHasContainer = new HasContainer(T.id.getAccessor(), P.test(biPredicate, recordId));
                        schemaTableTree1.getHasContainers().add(idHasContainer);
                    }
                }
            }
        }
    }
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) BiPredicate(java.util.function.BiPredicate)

Example 15 with HasContainer

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

the class ReplacedStep method collectSchemaTableTrees.

private void collectSchemaTableTrees(SqlgGraph sqlgGraph, int replacedStepDepth, Set<SchemaTableTree> result, Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupedIds, String table) {
    SchemaTable schemaTable = SchemaTable.from(sqlgGraph, table);
    List<HasContainer> schemaTableTreeHasContainers = new ArrayList<>(this.hasContainers);
    if (groupedIds != null) {
        List<Multimap<BiPredicate, RecordId>> biPredicateRecordIds = groupedIds.get(schemaTable.withOutPrefix());
        if (biPredicateRecordIds != null) {
            for (Multimap<BiPredicate, RecordId> biPredicateRecordId : biPredicateRecordIds) {
                for (BiPredicate biPredicate : biPredicateRecordId.keySet()) {
                    Collection<RecordId> recordIds = biPredicateRecordId.get(biPredicate);
                    HasContainer idHasContainer;
                    // id hasContainers are only optimized for BaseStrategy.SUPPORTED_ID_BI_PREDICATE within, without, eq, neq
                    if (biPredicate == Contains.without || biPredicate == Contains.within) {
                        idHasContainer = new HasContainer(T.id.getAccessor(), P.test(biPredicate, recordIds));
                        schemaTableTreeHasContainers.add(idHasContainer);
                    } else {
                        Preconditions.checkState(biPredicate == Compare.eq || biPredicate == Compare.neq);
                        for (RecordId recordId : recordIds) {
                            idHasContainer = new HasContainer(T.id.getAccessor(), P.test(biPredicate, recordId));
                            schemaTableTreeHasContainers.add(idHasContainer);
                        }
                    }
                }
            }
        }
    }
    SchemaTableTree schemaTableTree = new SchemaTableTree(sqlgGraph, schemaTable, 0, schemaTableTreeHasContainers, this.andOrHasContainers, this.sqlgComparatorHolder, this.sqlgComparatorHolder.getComparators(), this.sqlgRangeHolder, SchemaTableTree.STEP_TYPE.GRAPH_STEP, ReplacedStep.this.emit, ReplacedStep.this.untilFirst, ReplacedStep.this.leftJoin, ReplacedStep.this.drop, replacedStepDepth, ReplacedStep.this.labels);
    result.add(schemaTableTree);
}
Also used : Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) BiPredicate(java.util.function.BiPredicate)

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