Search in sources :

Example 6 with Aggregate

use of com.baidu.hugegraph.backend.query.Aggregate 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

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