Search in sources :

Example 46 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class HugeVertexStep method constructEdgesQuery.

protected ConditionQuery constructEdgesQuery(Traverser.Admin<Vertex> traverser) {
    HugeGraph graph = TraversalUtil.getGraph(this);
    // Query for edge with conditions(else conditions for vertex)
    boolean withEdgeCond = this.withEdgeCondition();
    boolean withVertexCond = this.withVertexCondition();
    Id vertex = (Id) traverser.get().id();
    Directions direction = Directions.convert(this.getDirection());
    Id[] edgeLabels = graph.mapElName2Id(this.getEdgeLabels());
    LOG.debug("HugeVertexStep.edges(): vertex={}, direction={}, " + "edgeLabels={}, has={}", vertex, direction, edgeLabels, this.hasContainers);
    ConditionQuery query = GraphTransaction.constructEdgesQuery(vertex, direction, edgeLabels);
    // Query by sort-keys
    if (withEdgeCond && edgeLabels.length == 1) {
        TraversalUtil.fillConditionQuery(query, this.hasContainers, graph);
        if (!GraphTransaction.matchPartialEdgeSortKeys(query, graph)) {
            // Can't query by sysprop and by index (HugeGraph-749)
            query.resetUserpropConditions();
        } else if (GraphTransaction.matchFullEdgeSortKeys(query, graph)) {
            // All sysprop conditions are in sort-keys
            withEdgeCond = false;
        } else {
            // Partial sysprop conditions are in sort-keys
            assert query.userpropKeys().size() > 0;
        }
    }
    // Query by has(id)
    if (query.idsSize() > 0) {
        // Ignore conditions if query by edge id in has-containers
        // FIXME: should check that the edge id matches the `vertex`
        query.resetConditions();
        LOG.warn("It's not recommended to query by has(id)");
    }
    /*
         * Unset limit when needed to filter property after store query
         * like query: outE().has(k,v).limit(n)
         * NOTE: outE().limit(m).has(k,v).limit(n) will also be unset limit,
         * Can't unset limit if query by paging due to page position will be
         * exceeded when reaching the limit in tinkerpop layer
         */
    if (withEdgeCond || withVertexCond) {
        com.baidu.hugegraph.util.E.checkArgument(!this.queryInfo().paging(), "Can't query by paging " + "and filtering");
        this.queryInfo().limit(Query.NO_LIMIT);
    }
    query = this.injectQueryInfo(query);
    return query;
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id)

Example 47 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class HugeTraverser method fillFilterBySortKeys.

private void fillFilterBySortKeys(Query query, Id[] edgeLabels, Map<Id, Object> properties) {
    if (properties == null || properties.isEmpty()) {
        return;
    }
    E.checkArgument(edgeLabels.length == 1, "The properties filter condition can be set " + "only if just set one edge label");
    this.fillFilterByProperties(query, properties);
    ConditionQuery condQuery = (ConditionQuery) query;
    if (!GraphTransaction.matchFullEdgeSortKeys(condQuery, this.graph())) {
        Id label = condQuery.condition(HugeKeys.LABEL);
        E.checkArgument(false, "The properties %s does not match " + "sort keys of edge label '%s'", this.graph().mapPkId2Name(properties.keySet()), this.graph().edgeLabel(label).name());
    }
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Id(com.baidu.hugegraph.backend.id.Id)

Example 48 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class HugeVariables method createVariableQuery.

private ConditionQuery createVariableQuery(String name) {
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    VertexLabel vl = this.variableVertexLabel();
    query.eq(HugeKeys.LABEL, vl.id());
    if (name != null) {
        PropertyKey pkey = this.params.graph().propertyKey(Hidden.hide(VARIABLE_KEY));
        query.query(Condition.eq(pkey.id(), name));
    }
    query.showHidden(true);
    return query;
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 49 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class ScyllaDBTablesWithMV method isQueryBySpecifiedKey.

private static boolean isQueryBySpecifiedKey(Query query, HugeKeys key) {
    Collection<Condition> conditions = query.conditions();
    if (query instanceof ConditionQuery && !conditions.isEmpty()) {
        ConditionQuery cq = (ConditionQuery) query;
        Object value = cq.condition(key);
        return value != null && cq.allSysprop() && conditions.size() == 1 && cq.containsRelation(key, Condition.RelationType.EQ);
    }
    return false;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery)

Example 50 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class MysqlTable method query2Select.

protected List<StringBuilder> query2Select(String table, Query query) {
    // Build query
    StringBuilder select = new StringBuilder(64);
    select.append("SELECT ");
    // Set aggregate
    Aggregate aggregate = query.aggregate();
    if (aggregate != null) {
        select.append(aggregate.toString());
    } else {
        select.append("*");
    }
    // Set table
    select.append(" FROM ").append(table);
    // Is query by id?
    List<StringBuilder> ids = this.queryId2Select(query, select);
    List<StringBuilder> selections;
    if (query.conditionsSize() == 0) {
        // Query only by id
        LOG.debug("Query only by id(s): {}", ids);
        selections = ids;
    } else {
        ConditionQuery condQuery = (ConditionQuery) query;
        if (condQuery.containsScanRelation()) {
            assert ids.size() == 1;
            return ImmutableList.of(queryByRange(condQuery, ids.get(0)));
        }
        selections = new ArrayList<>(ids.size());
        for (StringBuilder selection : ids) {
            // Query by condition
            selections.addAll(this.queryCondition2Select(query, selection));
        }
        LOG.debug("Query by conditions: {}", selections);
    }
    // Set page, order-by and limit
    for (StringBuilder selection : selections) {
        boolean hasOrder = !query.orders().isEmpty();
        if (hasOrder) {
            this.wrapOrderBy(selection, query);
        }
        if (query.paging()) {
            this.wrapPage(selection, query, false);
            wrapLimit(selection, query);
        } else {
            if (aggregate == null && !hasOrder) {
                select.append(this.orderByKeys());
            }
            if (!query.noLimit() || query.offset() > 0L) {
                this.wrapOffset(selection, query);
            }
        }
    }
    return selections;
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Aggregate(com.baidu.hugegraph.backend.query.Aggregate)

Aggregations

ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)59 Id (com.baidu.hugegraph.backend.id.Id)19 Test (org.junit.Test)19 Condition (com.baidu.hugegraph.backend.query.Condition)17 HugeGraph (com.baidu.hugegraph.HugeGraph)13 ArrayList (java.util.ArrayList)12 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)8 Collection (java.util.Collection)8 Query (com.baidu.hugegraph.backend.query.Query)7 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)7 Map (java.util.Map)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)6 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)5 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)5 HugeType (com.baidu.hugegraph.type.HugeType)5 ImmutableMap (com.google.common.collect.ImmutableMap)5