Search in sources :

Example 1 with Relation

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

the class ConditionQueryFlatten method optimizeRelations.

private static Relations optimizeRelations(Relations relations) {
    // Optimize and-relations in one query
    // e.g. (age>1 and age>2) -> (age>2)
    Set<Object> keys = relations.stream().map(Relation::key).collect(Collectors.toSet());
    // No duplicated keys
    if (keys.size() == relations.size()) {
        return relations;
    }
    for (Object key : keys) {
        // Get same key relations
        Relations rs = new Relations();
        for (Relation relation : relations) {
            if (relation.key().equals(key)) {
                rs.add(relation);
            }
        }
        // Same key relation number is 1, needn't merge
        if (rs.size() == 1) {
            continue;
        }
        // Relations in rs having same key might need merge
        relations.removeAll(rs);
        rs = mergeRelations(rs);
        // Conditions are mutually exclusive(e.g. age>10 and age==9)
        if (rs.isEmpty()) {
            return null;
        }
        relations.addAll(rs);
    }
    return relations;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation)

Example 2 with Relation

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

the class ConditionQueryFlatten method newQueryFromRelations.

private static ConditionQuery newQueryFromRelations(ConditionQuery query, Relations relations) {
    ConditionQuery cq = query.copyAndResetUnshared();
    cq.resetConditions();
    for (Relation relation : relations) {
        cq.query(relation);
    }
    return cq;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation)

Example 3 with Relation

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

the class ConditionQueryFlatten method mergeRelations.

/**
 * Reduce and merge relations linked with 'AND' for same key
 * @param relations linked with 'AND' having same key, may contains 'in',
 *                 'not in', '>', '<', '>=', '<=', '==', '!='
 * @return merged relations
 */
private static Relations mergeRelations(Relations relations) {
    Relations result = new Relations();
    boolean isNum = false;
    Relation gt = null;
    Relation gte = null;
    Relation eq = null;
    Relation lt = null;
    Relation lte = null;
    for (Relation relation : relations) {
        switch(relation.relation()) {
            case GT:
                isNum = true;
                if (gt == null || compare(relation, gt) > 0) {
                    gt = relation;
                }
                break;
            case GTE:
                isNum = true;
                if (gte == null || compare(relation, gte) > 0) {
                    gte = relation;
                }
                break;
            case EQ:
                if (eq == null) {
                    eq = relation;
                    if (eq.value() instanceof Number) {
                        isNum = true;
                    }
                    break;
                } else if (!relation.value().equals(eq.value())) {
                    return Relations.of();
                }
                break;
            case LT:
                isNum = true;
                if (lt == null || compare(lt, relation) > 0) {
                    lt = relation;
                }
                break;
            case LTE:
                isNum = true;
                if (lte == null || compare(lte, relation) > 0) {
                    lte = relation;
                }
                break;
            default:
                // NEQ, IN, NOT_IN, CONTAINS, CONTAINS_KEY, SCAN
                result.add(relation);
                break;
        }
    }
    if (!isNum) {
        // Not number, only may have equal relation
        if (eq != null) {
            result.add(eq);
        }
    } else {
        // At most have 1 eq, 1 lt, 1 lte, 1 gt, 1 gte
        result.addAll(calcValidRange(gte, gt, eq, lte, lt));
    }
    return result;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation)

Example 4 with Relation

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

the class ConditionQueryFlatten method flattenAndOr.

private static Set<Relations> flattenAndOr(Condition condition) {
    Set<Relations> result = InsertionOrderUtil.newSet();
    switch(condition.type()) {
        case RELATION:
            Relation relation = (Relation) condition;
            result.add(Relations.of(relation));
            break;
        case AND:
            Condition.And and = (Condition.And) condition;
            result = and(flattenAndOr(and.left()), flattenAndOr(and.right()));
            break;
        case OR:
            Condition.Or or = (Condition.Or) condition;
            result = or(flattenAndOr(or.left()), flattenAndOr(or.right()));
            break;
        default:
            throw new AssertionError(String.format("Wrong condition type: '%s'", condition.type()));
    }
    return result;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation)

Example 5 with Relation

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

the class RocksDBTable method queryByCond.

protected BackendColumnIterator queryByCond(Session session, ConditionQuery query) {
    if (query.containsScanRelation()) {
        E.checkArgument(query.relations().size() == 1, "Invalid scan with multi conditions: %s", query);
        Relation scan = query.relations().iterator().next();
        Shard shard = (Shard) scan.value();
        return this.queryByRange(session, shard, query.page());
    }
    throw new NotSupportException("query: %s", query);
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Shard(com.baidu.hugegraph.backend.store.Shard)

Aggregations

Relation (com.baidu.hugegraph.backend.query.Condition.Relation)18 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)7 Id (com.baidu.hugegraph.backend.id.Id)5 Condition (com.baidu.hugegraph.backend.query.Condition)5 ArrayList (java.util.ArrayList)4 BackendException (com.baidu.hugegraph.backend.BackendException)3 Shard (com.baidu.hugegraph.backend.store.Shard)3 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)3 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)2 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)2 Clause (com.datastax.driver.core.querybuilder.Clause)2 Date (java.util.Date)2 Test (org.junit.Test)2 PageState (com.baidu.hugegraph.backend.page.PageState)1 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)1 Query (com.baidu.hugegraph.backend.query.Query)1 Order (com.baidu.hugegraph.backend.query.Query.Order)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 BackendTable (com.baidu.hugegraph.backend.store.BackendTable)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1