use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class CloneSchema method createCloneTable.
public static <T> Table createCloneTable(final JavaTypeFactory typeFactory, final RelProtoDataType protoRowType, final List<RelCollation> collations, final List<ColumnMetaData.Rep> repList, final Enumerable<T> source) {
final Type elementType;
if (source instanceof QueryableTable) {
elementType = ((QueryableTable) source).getElementType();
} else if (protoRowType.apply(typeFactory).getFieldCount() == 1) {
if (repList != null) {
elementType = repList.get(0).clazz;
} else {
elementType = Object.class;
}
} else {
elementType = Object[].class;
}
return new ArrayTable(elementType, protoRowType, Suppliers.memoize(new Supplier<ArrayTable.Content>() {
public ArrayTable.Content get() {
final ColumnLoader loader = new ColumnLoader<>(typeFactory, source, protoRowType, repList);
final List<RelCollation> collation2 = collations.isEmpty() && loader.sortField >= 0 ? RelCollations.createSingleton(loader.sortField) : collations;
return new ArrayTable.Content(loader.representationValues, loader.size(), collation2);
}
}));
}
use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class Smalls method str.
public static TranslatableTable str(Object o, Object p) {
assertThat(RexLiteral.validConstant(o, Litmus.THROW), is(true));
assertThat(RexLiteral.validConstant(p, Litmus.THROW), is(true));
return new ViewTable(Object.class, new RelProtoDataType() {
public RelDataType apply(RelDataTypeFactory typeFactory) {
return typeFactory.builder().add("c", SqlTypeName.VARCHAR, 100).build();
}
}, "values " + CalciteSqlDialect.DEFAULT.quoteStringLiteral(o.toString()) + ", " + CalciteSqlDialect.DEFAULT.quoteStringLiteral(p.toString()), ImmutableList.<String>of(), Arrays.asList("view"));
}
use of org.apache.calcite.rel.type.RelProtoDataType 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);
}
};
}
Aggregations