use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class CassandraTable method query.
/**
* Executes a CQL query on the underlying table.
*
* @param session Cassandra session
* @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 Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
// Build the type of the resulting row based on the provided fields
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
final RelDataType rowType = getRowType(typeFactory);
Function1<String, Void> addField = new Function1<String, Void>() {
public Void apply(String fieldName) {
SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
return null;
}
};
if (selectFields.isEmpty()) {
for (Map.Entry<String, Class> field : fields) {
addField.apply(field.getKey());
}
} else {
for (Map.Entry<String, String> field : selectFields) {
addField.apply(field.getKey());
}
}
final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
// Construct the list of fields to project
final String selectString;
if (selectFields.isEmpty()) {
selectString = "*";
} else {
selectString = Util.toString(new Iterable<String>() {
public Iterator<String> iterator() {
final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
return new Iterator<String>() {
@Override
public boolean hasNext() {
return selectIterator.hasNext();
}
@Override
public String next() {
Map.Entry<String, String> entry = selectIterator.next();
return entry.getKey() + " AS " + entry.getValue();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, "", ", ", "");
}
// 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(selectString);
queryBuilder.append(" FROM \"" + columnFamily + "\"");
queryBuilder.append(whereClause);
if (!order.isEmpty()) {
queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
}
int limit = offset;
if (fetch >= 0) {
limit += fetch;
}
if (limit > 0) {
queryBuilder.append(" LIMIT " + limit);
}
queryBuilder.append(" ALLOW FILTERING");
final String query = queryBuilder.toString();
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final ResultSet results = session.execute(query);
// Skip results until we get to the right offset
int skip = 0;
Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
while (skip < offset && enumerator.moveNext()) {
skip++;
}
return enumerator;
}
};
}
use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class CassandraSchema method getRelDataType.
RelProtoDataType getRelDataType(String columnFamily, boolean view) {
List<ColumnMetadata> columns;
if (view) {
columns = getKeyspace().getMaterializedView(columnFamily).getColumns();
} else {
columns = getKeyspace().getTable(columnFamily).getColumns();
}
// Temporary type factory, just for the duration of this method. Allowable
// because we're creating a proto-type, not a type; before being used, the
// proto-type will be copied into a real type factory.
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
for (ColumnMetadata column : columns) {
final String columnName = column.getName();
final DataType type = column.getType();
// TODO: This mapping of types can be done much better
SqlTypeName typeName = SqlTypeName.ANY;
if (type == DataType.uuid() || type == DataType.timeuuid()) {
// We currently rely on this in CassandraFilter to detect UUID columns.
// That is, these fixed length literals should be unquoted in CQL.
typeName = SqlTypeName.CHAR;
} else if (type == DataType.ascii() || type == DataType.text() || type == DataType.varchar()) {
typeName = SqlTypeName.VARCHAR;
} else if (type == DataType.cint() || type == DataType.varint()) {
typeName = SqlTypeName.INTEGER;
} else if (type == DataType.bigint()) {
typeName = SqlTypeName.BIGINT;
} else if (type == DataType.cdouble() || type == DataType.cfloat() || type == DataType.decimal()) {
typeName = SqlTypeName.DOUBLE;
}
fieldInfo.add(columnName, typeFactory.createSqlType(typeName)).nullable(true);
}
return RelDataTypeImpl.proto(fieldInfo.build());
}
use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class CsvStreamTableFactory method create.
public CsvTable create(SchemaPlus schema, String name, Map<String, Object> operand, RelDataType rowType) {
String fileName = (String) operand.get("file");
File file = new File(fileName);
final File base = (File) operand.get(ModelHandler.ExtraOperand.BASE_DIRECTORY.camelName);
if (base != null && !file.isAbsolute()) {
file = new File(base, fileName);
}
final Source source = Sources.of(file);
final RelProtoDataType protoRowType = rowType != null ? RelDataTypeImpl.proto(rowType) : null;
return new CsvStreamScannableTable(source, protoRowType);
}
use of org.apache.calcite.rel.type.RelProtoDataType in project calcite by apache.
the class CsvTableFactory method create.
public CsvTable create(SchemaPlus schema, String name, Map<String, Object> operand, RelDataType rowType) {
String fileName = (String) operand.get("file");
final File base = (File) operand.get(ModelHandler.ExtraOperand.BASE_DIRECTORY.camelName);
final Source source = Sources.file(base, fileName);
final RelProtoDataType protoRowType = rowType != null ? RelDataTypeImpl.proto(rowType) : null;
return new CsvScannableTable(source, protoRowType);
}
use of org.apache.calcite.rel.type.RelProtoDataType in project hive by apache.
the class DataSketchesFunctions method buildCalciteFns.
private void buildCalciteFns() {
for (SketchDescriptor sd : sketchClasses.values()) {
registerAsHiveFunction(sd.fnMap.get(SKETCH_TO_ESTIMATE));
registerAsHiveFunction(sd.fnMap.get(GET_QUANTILE));
registerAsHiveFunction(sd.fnMap.get(GET_CDF));
registerAsHiveFunction(sd.fnMap.get(GET_N));
registerAsHiveFunction(sd.fnMap.get(GET_RANK));
// Mergability is exposed to Calcite; which enables to use it during rollup.
RelProtoDataType sketchType = RelDataTypeImpl.proto(SqlTypeName.BINARY, true);
SketchFunctionDescriptor sketchSFD = sd.fnMap.get(DATA_TO_SKETCH);
SketchFunctionDescriptor unionSFD = sd.fnMap.get(UNION_SKETCH);
if (sketchSFD == null || unionSFD == null) {
continue;
}
HiveMergeableAggregate unionFn = new HiveMergeableAggregate(unionSFD.name, SqlKind.OTHER_FUNCTION, ReturnTypes.explicit(sketchType), InferTypes.ANY_NULLABLE, OperandTypes.family(), null);
HiveMergeableAggregate sketchFn = new HiveMergeableAggregate(sketchSFD.name, SqlKind.OTHER_FUNCTION, ReturnTypes.explicit(sketchType), InferTypes.ANY_NULLABLE, OperandTypes.family(), unionFn);
unionSFD.setCalciteFunction(unionFn);
sketchSFD.setCalciteFunction(sketchFn);
}
}
Aggregations