use of org.apache.calcite.sql.type.SqlTypeName 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.sql.type.SqlTypeName in project calcite by apache.
the class RexProgramTest method testSimplifyCastLiteral.
@Test
public void testSimplifyCastLiteral() {
final List<RexLiteral> literals = new ArrayList<>();
literals.add(rexBuilder.makeExactLiteral(BigDecimal.ONE, typeFactory.createSqlType(SqlTypeName.INTEGER)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(2), typeFactory.createSqlType(SqlTypeName.BIGINT)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(3), typeFactory.createSqlType(SqlTypeName.SMALLINT)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(4), typeFactory.createSqlType(SqlTypeName.TINYINT)));
literals.add(rexBuilder.makeExactLiteral(new BigDecimal("1234"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 4, 0)));
literals.add(rexBuilder.makeExactLiteral(new BigDecimal("123.45"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 5, 2)));
literals.add(rexBuilder.makeApproxLiteral(new BigDecimal("3.1415"), typeFactory.createSqlType(SqlTypeName.REAL)));
literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.E), typeFactory.createSqlType(SqlTypeName.FLOAT)));
literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.PI), typeFactory.createSqlType(SqlTypeName.DOUBLE)));
literals.add(rexBuilder.makeLiteral(true));
literals.add(rexBuilder.makeLiteral(false));
literals.add(rexBuilder.makeLiteral("hello world"));
literals.add(rexBuilder.makeLiteral("1969-07-20 12:34:56"));
literals.add(rexBuilder.makeLiteral("1969-07-20"));
literals.add(rexBuilder.makeLiteral("12:34:45"));
literals.add((RexLiteral) rexBuilder.makeLiteral(new ByteString(new byte[] { 1, 2, -34, 0, -128 }), typeFactory.createSqlType(SqlTypeName.BINARY, 5), false));
literals.add(rexBuilder.makeDateLiteral(new DateString(1974, 8, 9)));
literals.add(rexBuilder.makeTimeLiteral(new TimeString(1, 23, 45), 0));
literals.add(rexBuilder.makeTimestampLiteral(new TimestampString(1974, 8, 9, 1, 23, 45), 0));
final Multimap<SqlTypeName, RexLiteral> map = LinkedHashMultimap.create();
for (RexLiteral literal : literals) {
map.put(literal.getTypeName(), literal);
}
final List<RelDataType> types = new ArrayList<>();
types.add(typeFactory.createSqlType(SqlTypeName.INTEGER));
types.add(typeFactory.createSqlType(SqlTypeName.BIGINT));
types.add(typeFactory.createSqlType(SqlTypeName.SMALLINT));
types.add(typeFactory.createSqlType(SqlTypeName.TINYINT));
types.add(typeFactory.createSqlType(SqlTypeName.REAL));
types.add(typeFactory.createSqlType(SqlTypeName.FLOAT));
types.add(typeFactory.createSqlType(SqlTypeName.DOUBLE));
types.add(typeFactory.createSqlType(SqlTypeName.BOOLEAN));
types.add(typeFactory.createSqlType(SqlTypeName.VARCHAR, 10));
types.add(typeFactory.createSqlType(SqlTypeName.CHAR, 5));
types.add(typeFactory.createSqlType(SqlTypeName.VARBINARY, 60));
types.add(typeFactory.createSqlType(SqlTypeName.BINARY, 3));
types.add(typeFactory.createSqlType(SqlTypeName.TIMESTAMP));
types.add(typeFactory.createSqlType(SqlTypeName.TIME));
types.add(typeFactory.createSqlType(SqlTypeName.DATE));
for (RelDataType fromType : types) {
for (RelDataType toType : types) {
if (SqlTypeAssignmentRules.instance(false).canCastFrom(toType.getSqlTypeName(), fromType.getSqlTypeName())) {
for (RexLiteral literal : map.get(fromType.getSqlTypeName())) {
final RexNode cast = rexBuilder.makeCast(toType, literal);
if (cast instanceof RexLiteral) {
assertThat(cast.getType(), is(toType));
// makeCast already simplified
continue;
}
final RexNode simplified = simplify.simplify(cast);
boolean expectedSimplify = literal.getTypeName() != toType.getSqlTypeName() || (literal.getTypeName() == SqlTypeName.CHAR && ((NlsString) literal.getValue()).getValue().length() > toType.getPrecision()) || (literal.getTypeName() == SqlTypeName.BINARY && ((ByteString) literal.getValue()).length() > toType.getPrecision());
boolean couldSimplify = !cast.equals(simplified);
final String reason = (expectedSimplify ? "expected to simplify, but could not: " : "simplified, but did not expect to: ") + cast + " --> " + simplified;
assertThat(reason, couldSimplify, is(expectedSimplify));
}
}
}
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.
the class FindLimit0Visitor method getDirectScanRelIfFullySchemaed.
/**
* If all field types of the given node are {@link #TYPES recognized types} and honored by execution, then this
* method returns the tree: DrillDirectScanRel(field types). Otherwise, the method returns null.
*
* @param rel calcite logical rel tree
* @return drill logical rel tree
*/
public static DrillRel getDirectScanRelIfFullySchemaed(RelNode rel) {
final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
final List<TypeProtos.MajorType> columnTypes = Lists.newArrayList();
for (final RelDataTypeField field : fieldList) {
final SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
if (!TYPES.contains(sqlTypeName)) {
return null;
} else {
final TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().setMode(field.getType().isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED).setMinorType(TypeInferenceUtils.getDrillTypeFromCalciteType(sqlTypeName));
if (TypeInferenceUtils.isScalarStringType(sqlTypeName)) {
builder.setPrecision(field.getType().getPrecision());
}
columnTypes.add(builder.build());
}
}
final RelTraitSet traits = rel.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
final RelDataTypeReader reader = new RelDataTypeReader(rel.getRowType().getFieldNames(), columnTypes);
return new DrillDirectScanRel(rel.getCluster(), traits, new DirectGroupScan(reader, ScanStats.ZERO_RECORD_TABLE), rel.getRowType());
}
use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.
the class RelDataTypeHolder method getField.
public RelDataTypeField getField(RelDataTypeFactory typeFactory, String fieldName) {
/* First check if this field name exists in our field list */
for (RelDataTypeField f : fields) {
if (fieldName.equalsIgnoreCase(f.getName())) {
return f;
}
}
/* This field does not exist in our field list add it */
final SqlTypeName typeName = DynamicRecordType.isDynamicStarColName(fieldName) ? SqlTypeName.DYNAMIC_STAR : SqlTypeName.ANY;
// This field does not exist in our field list add it
RelDataTypeField newField = new RelDataTypeFieldImpl(fieldName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(typeName), true));
/* Add the name to our list of field names */
fields.add(newField);
return newField;
}
use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.
the class RecordDataType method getRowType.
/**
* This method constructs a {@link org.apache.calcite.rel.type.RelDataType} based on the
* {@link org.apache.drill.exec.store.RecordDataType}'s field sql types and field names.
*
* @param factory helps construct a {@link org.apache.calcite.rel.type.RelDataType}
* @return the constructed type
*/
public final RelDataType getRowType(RelDataTypeFactory factory) {
final List<SqlTypeName> types = getFieldSqlTypeNames();
final List<String> names = getFieldNames();
final List<RelDataType> fields = Lists.newArrayList();
for (final SqlTypeName typeName : types) {
switch(typeName) {
case VARCHAR:
fields.add(factory.createSqlType(typeName, Integer.MAX_VALUE));
break;
default:
fields.add(factory.createSqlType(typeName));
}
}
return factory.createStructType(fields, names);
}
Aggregations