Search in sources :

Example 1 with Aggregate

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

the class CassandraTable method queryNumber.

@Override
public Number queryNumber(CassandraSessionPool.Session session, Query query) {
    Aggregate aggregate = query.aggregateNotNull();
    Iterator<Number> results = this.query(query, statement -> {
        // Set request timeout to a large value
        int timeout = session.aggregateTimeout();
        statement.setReadTimeoutMillis(timeout * 1000);
        return session.query(statement);
    }, (q, rs) -> {
        Row row = rs.one();
        if (row == null) {
            return IteratorUtils.of(aggregate.defaultValue());
        }
        return IteratorUtils.of(row.getLong(0));
    });
    return aggregate.reduce(results);
}
Also used : Row(com.datastax.driver.core.Row) Aggregate(com.baidu.hugegraph.backend.query.Aggregate)

Example 2 with Aggregate

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

the class CassandraTable method query2Select.

protected List<Select> query2Select(String table, Query query) {
    // Build query
    Selection selection = QueryBuilder.select();
    // Set aggregate
    Aggregate aggregate = query.aggregate();
    if (aggregate != null) {
        if (aggregate.countAll()) {
            selection.countAll();
        } else {
            selection.fcall(aggregate.func().string(), aggregate.column());
        }
    }
    // Set table
    Select select = selection.from(table);
    // NOTE: Cassandra does not support query.offset()
    if (query.offset() != 0) {
        LOG.debug("Query offset is not supported on Cassandra store " + "currently, it will be replaced by [0, offset + limit)");
    }
    // Set order-by
    for (Map.Entry<HugeKeys, Order> order : query.orders().entrySet()) {
        String name = formatKey(order.getKey());
        if (order.getValue() == Order.ASC) {
            select.orderBy(QueryBuilder.asc(name));
        } else {
            assert order.getValue() == Order.DESC;
            select.orderBy(QueryBuilder.desc(name));
        }
    }
    // Is query by id?
    List<Select> ids = this.queryId2Select(query, select);
    if (query.conditionsSize() == 0) {
        // Query only by id
        this.setPageState(query, ids);
        LOG.debug("Query only by id(s): {}", ids);
        return ids;
    } else {
        List<Select> conds = new ArrayList<>(ids.size());
        for (Select id : ids) {
            // Query by condition
            conds.addAll(this.queryCondition2Select(query, id));
        }
        this.setPageState(query, conds);
        LOG.debug("Query by conditions: {}", conds);
        return conds;
    }
}
Also used : Order(com.baidu.hugegraph.backend.query.Query.Order) Selection(com.datastax.driver.core.querybuilder.Select.Selection) Select(com.datastax.driver.core.querybuilder.Select) ArrayList(java.util.ArrayList) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with Aggregate

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

the class InMemoryDBTable method queryNumber.

@Override
public Number queryNumber(BackendSession session, Query query) {
    Aggregate aggregate = query.aggregateNotNull();
    if (aggregate.func() != AggregateFunc.COUNT) {
        throw new NotSupportException(aggregate.toString());
    }
    assert aggregate.func() == AggregateFunc.COUNT;
    Iterator<BackendEntry> results = this.query(session, query);
    long total = 0L;
    while (results.hasNext()) {
        total += this.sizeOfBackendEntry(results.next());
    }
    return total;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Aggregate(com.baidu.hugegraph.backend.query.Aggregate)

Example 4 with Aggregate

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

the class GraphTransaction method queryNumber.

@Override
public Number queryNumber(Query query) {
    E.checkArgument(!this.hasUpdate(), "It's not allowed to query number when " + "there are uncommitted records.");
    if (!(query instanceof ConditionQuery)) {
        return super.queryNumber(query);
    }
    Aggregate aggregate = query.aggregateNotNull();
    QueryList<Number> queries = this.optimizeQueries(query, q -> {
        boolean indexQuery = q.getClass() == IdQuery.class;
        OptimizedType optimized = ((ConditionQuery) query).optimized();
        Number result;
        if (!indexQuery) {
            result = super.queryNumber(q);
        } else {
            E.checkArgument(aggregate.func() == AggregateFunc.COUNT, "The %s operator on index is not supported now", aggregate.func().string());
            if (this.optimizeAggrByIndex && optimized == OptimizedType.INDEX) {
                // The ids size means results count (assume no left index)
                result = q.idsSize();
            } else {
                assert optimized == OptimizedType.INDEX_FILTER || optimized == OptimizedType.INDEX;
                assert q.resultType().isVertex() || q.resultType().isEdge();
                result = IteratorUtils.count(q.resultType().isVertex() ? this.queryVertices(q) : this.queryEdges(q));
            }
        }
        return new QueryResults<>(IteratorUtils.of(result), q);
    });
    QueryResults<Number> results = queries.empty() ? QueryResults.empty() : queries.fetch(this.pageSize);
    return aggregate.reduce(results.iterator());
}
Also used : OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) QueryResults(com.baidu.hugegraph.backend.query.QueryResults)

Example 5 with Aggregate

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

the class MysqlTable method queryNumber.

@Override
public Number queryNumber(Session session, Query query) {
    Aggregate aggregate = query.aggregateNotNull();
    Iterator<Number> results = this.query(session, query, (q, rs) -> {
        try {
            if (!rs.resultSet().next()) {
                return IteratorUtils.of(aggregate.defaultValue());
            }
            return IteratorUtils.of(rs.resultSet().getLong(1));
        } catch (SQLException e) {
            throw new BackendException(e);
        } finally {
            rs.close();
        }
    });
    return aggregate.reduce(results);
}
Also used : SQLException(java.sql.SQLException) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

Aggregate (com.baidu.hugegraph.backend.query.Aggregate)6 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 OptimizedType (com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType)1 Order (com.baidu.hugegraph.backend.query.Query.Order)1 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)1 TextBackendEntry (com.baidu.hugegraph.backend.serializer.TextBackendEntry)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)1 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)1 Row (com.datastax.driver.core.Row)1 Select (com.datastax.driver.core.querybuilder.Select)1 Selection (com.datastax.driver.core.querybuilder.Select.Selection)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1