Search in sources :

Example 1 with JavaTypeFactoryExtImpl

use of org.apache.calcite.adapter.geode.util.JavaTypeFactoryExtImpl in project calcite by apache.

the class GeodeTable method query.

/**
 * Executes an OQL query on the underlying table.
 *
 * <p>Called by the {@link GeodeQueryable} which in turn is
 * called via the generated code.
 *
 * @param clientCache Geode client cache
 * @param fields      List of fields to project
 * @param predicates  A list of predicates which should be used in the query
 * @return Enumerator of results
 */
public Enumerable<Object> query(final ClientCache clientCache, final List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, final List<Map.Entry<String, String>> aggregateFunctions, final List<String> groupByFields, List<String> predicates, List<String> orderByFields, String limit) {
    final RelDataTypeFactory typeFactory = new JavaTypeFactoryExtImpl();
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    for (Map.Entry<String, Class> field : fields) {
        SqlTypeName typeName = typeFactory.createJavaType(field.getValue()).getSqlTypeName();
        fieldInfo.add(field.getKey(), typeFactory.createSqlType(typeName)).nullable(true);
    }
    final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
    ImmutableMap<String, String> aggFuncMap = ImmutableMap.of();
    if (!aggregateFunctions.isEmpty()) {
        ImmutableMap.Builder<String, String> aggFuncMapBuilder = ImmutableMap.builder();
        for (Map.Entry<String, String> e : aggregateFunctions) {
            aggFuncMapBuilder.put(e.getKey(), e.getValue());
        }
        aggFuncMap = aggFuncMapBuilder.build();
    }
    // Construct the list of fields to project
    Builder<String> selectBuilder = ImmutableList.builder();
    if (!groupByFields.isEmpty()) {
        for (String groupByField : groupByFields) {
            selectBuilder.add(groupByField + " AS " + groupByField);
        }
        if (!aggFuncMap.isEmpty()) {
            for (Map.Entry<String, String> e : aggFuncMap.entrySet()) {
                selectBuilder.add(e.getValue() + " AS " + e.getKey());
            }
        }
    } else {
        if (selectFields.isEmpty()) {
            if (!aggFuncMap.isEmpty()) {
                for (Map.Entry<String, String> e : aggFuncMap.entrySet()) {
                    selectBuilder.add(e.getValue() + " AS " + e.getKey());
                }
            } else {
                selectBuilder.add("*");
            }
        } else {
            for (Map.Entry<String, String> field : selectFields) {
                selectBuilder.add(field.getKey() + " AS " + field.getValue());
            }
        }
    }
    final String oqlSelectStatement = Util.toString(selectBuilder.build(), " ", ", ", "");
    // Combine all predicates conjunctively
    String whereClause = "";
    if (!predicates.isEmpty()) {
        whereClause = " WHERE ";
        whereClause += Util.toString(predicates, "", " AND ", "");
    }
    // Build and issue the query and return an Enumerator over the results
    StringBuilder queryBuilder = new StringBuilder("SELECT ");
    queryBuilder.append(oqlSelectStatement);
    queryBuilder.append(" FROM /" + regionName);
    queryBuilder.append(whereClause);
    if (!groupByFields.isEmpty()) {
        queryBuilder.append(Util.toString(groupByFields, " GROUP BY ", ", ", ""));
    }
    if (!orderByFields.isEmpty()) {
        queryBuilder.append(Util.toString(orderByFields, " ORDER BY ", ", ", ""));
    }
    if (limit != null) {
        queryBuilder.append(" LIMIT " + limit);
    }
    final String oqlQuery = queryBuilder.toString();
    LOGGER.info("OQL: " + oqlQuery);
    return new AbstractEnumerable<Object>() {

        public Enumerator<Object> enumerator() {
            SelectResults results = null;
            QueryService queryService = clientCache.getQueryService();
            try {
                results = (SelectResults) queryService.newQuery(oqlQuery).execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new GeodeEnumerator(results, resultRowType);
        }
    };
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ImmutableMap(com.google.common.collect.ImmutableMap) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) SelectResults(org.apache.geode.cache.query.SelectResults) JavaTypeFactoryExtImpl(org.apache.calcite.adapter.geode.util.JavaTypeFactoryExtImpl) QueryService(org.apache.geode.cache.query.QueryService) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 JavaTypeFactoryExtImpl (org.apache.calcite.adapter.geode.util.JavaTypeFactoryExtImpl)1 AbstractEnumerable (org.apache.calcite.linq4j.AbstractEnumerable)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelProtoDataType (org.apache.calcite.rel.type.RelProtoDataType)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 QueryService (org.apache.geode.cache.query.QueryService)1 SelectResults (org.apache.geode.cache.query.SelectResults)1