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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations