use of org.apache.calcite.sql.type.SqlTypeName in project streamline by hortonworks.
the class StreamlineSqlTypeFactoryImpl method toSql.
@Override
public RelDataType toSql(RelDataType type) {
if (type instanceof JavaType) {
JavaType javaType = (JavaType) type;
SqlTypeName sqlTypeName = JavaToSqlTypeConversionRules.instance().lookup(javaType.getJavaClass());
if (sqlTypeName == null) {
sqlTypeName = SqlTypeName.ANY;
}
return createTypeWithNullability(createSqlType(sqlTypeName), type.isNullable());
}
return super.toSql(type);
}
use of org.apache.calcite.sql.type.SqlTypeName in project coral by linkedin.
the class RelDataTypeToTrinoTypeStringConverterTest method testPrimitiveRelDataType.
@Test
public void testPrimitiveRelDataType() {
Multimap<String, SqlTypeName> trinoTypeCastStringToSqlTypeNameMap = new ImmutableMultimap.Builder<String, SqlTypeName>().put(CHAR, SqlTypeName.CHAR).put(VARCHAR, SqlTypeName.VARCHAR).put(INTEGER, SqlTypeName.INTEGER).put(SMALLINT, SqlTypeName.SMALLINT).put(TINYINT, SqlTypeName.TINYINT).put(BIGINT, SqlTypeName.BIGINT).put(DOUBLE, SqlTypeName.DOUBLE).put(REAL, SqlTypeName.REAL).put(REAL, SqlTypeName.FLOAT).put(BOOLEAN, SqlTypeName.BOOLEAN).put(DATE, SqlTypeName.DATE).put(TIMESTAMP, SqlTypeName.TIMESTAMP).put(TIME, SqlTypeName.TIME).put(VARBINARY, SqlTypeName.BINARY).put(VARBINARY, SqlTypeName.VARBINARY).build();
for (Map.Entry<String, SqlTypeName> entry : trinoTypeCastStringToSqlTypeNameMap.entries()) {
String expectedTrinoTypeCastString = entry.getKey();
SqlTypeName sqlTypeName = entry.getValue();
RelDataType relDataType = new BasicSqlType(RelDataTypeSystem.DEFAULT, sqlTypeName);
String trinoTypeCastString = RelDataTypeToTrinoTypeStringConverter.buildTrinoTypeString(relDataType);
assertEquals(trinoTypeCastString, expectedTrinoTypeCastString);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project ignite-3 by apache.
the class RelJson method toType.
RelDataType toType(RelDataTypeFactory typeFactory, Object o) {
if (o instanceof List) {
List<Map<String, Object>> jsonList = (List<Map<String, Object>>) o;
RelDataTypeFactory.Builder builder = typeFactory.builder();
for (Map<String, Object> jsonMap : jsonList) {
builder.add((String) jsonMap.get("name"), toType(typeFactory, jsonMap));
}
return builder.build();
} else if (o instanceof Map) {
Map<String, Object> map = (Map<String, Object>) o;
String clazz = (String) map.get("class");
if (clazz != null) {
RelDataType type = typeFactory.createJavaType(classForName(clazz, false));
if (Boolean.TRUE == map.get("nullable")) {
type = typeFactory.createTypeWithNullability(type, true);
}
return type;
}
Object fields = map.get("fields");
if (fields != null) {
return toType(typeFactory, fields);
} else {
SqlTypeName sqlTypeName = toEnum(map.get("type"));
Integer precision = (Integer) map.get("precision");
Integer scale = (Integer) map.get("scale");
RelDataType type;
if (SqlTypeName.INTERVAL_TYPES.contains(sqlTypeName)) {
TimeUnit startUnit = sqlTypeName.getStartUnit();
TimeUnit endUnit = sqlTypeName.getEndUnit();
type = typeFactory.createSqlIntervalType(new SqlIntervalQualifier(startUnit, endUnit, SqlParserPos.ZERO));
} else if (sqlTypeName == SqlTypeName.ARRAY) {
type = typeFactory.createArrayType(toType(typeFactory, map.get("elementType")), -1);
} else if (precision == null) {
type = typeFactory.createSqlType(sqlTypeName);
} else if (scale == null) {
type = typeFactory.createSqlType(sqlTypeName, precision);
} else {
type = typeFactory.createSqlType(sqlTypeName, precision, scale);
}
if (Boolean.TRUE == map.get("nullable")) {
type = typeFactory.createTypeWithNullability(type, true);
}
return type;
}
} else {
SqlTypeName sqlTypeName = toEnum(o);
return typeFactory.createSqlType(sqlTypeName);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class CassandraSchema method getRelDataType.
RelProtoDataType getRelDataType(String columnFamily, boolean view) {
Map<CqlIdentifier, ColumnMetadata> columns;
CqlIdentifier tableName = CqlIdentifier.fromInternal(columnFamily);
if (view) {
Optional<ViewMetadata> optionalViewMetadata = getKeyspace().getView(tableName);
if (optionalViewMetadata.isPresent()) {
columns = optionalViewMetadata.get().getColumns();
} else {
throw new IllegalStateException("Unknown view " + tableName + " in keyspace " + keyspace);
}
} else {
Optional<TableMetadata> optionalTableMetadata = getKeyspace().getTable(tableName);
if (optionalTableMetadata.isPresent()) {
columns = optionalTableMetadata.get().getColumns();
} else {
throw new IllegalStateException("Unknown table " + tableName + " in keyspace " + keyspace);
}
}
// 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.values()) {
final DataType dataType = column.getType();
final String columnName = column.getName().asInternal();
if (dataType instanceof ListType) {
SqlTypeName arrayInnerType = CQL_TO_SQL_TYPE.lookup(((ListType) dataType).getElementType());
fieldInfo.add(columnName, typeFactory.createArrayType(typeFactory.createSqlType(arrayInnerType), -1)).nullable(true);
} else if (dataType instanceof SetType) {
SqlTypeName multiSetInnerType = CQL_TO_SQL_TYPE.lookup(((SetType) dataType).getElementType());
fieldInfo.add(columnName, typeFactory.createMultisetType(typeFactory.createSqlType(multiSetInnerType), -1)).nullable(true);
} else if (dataType instanceof MapType) {
MapType columnType = (MapType) dataType;
SqlTypeName keyType = CQL_TO_SQL_TYPE.lookup(columnType.getKeyType());
SqlTypeName valueType = CQL_TO_SQL_TYPE.lookup(columnType.getValueType());
fieldInfo.add(columnName, typeFactory.createMapType(typeFactory.createSqlType(keyType), typeFactory.createSqlType(valueType))).nullable(true);
} else if (dataType instanceof TupleType) {
List<DataType> typeArgs = ((TupleType) dataType).getComponentTypes();
List<Map.Entry<String, RelDataType>> typesList = IntStream.range(0, typeArgs.size()).mapToObj(i -> new Pair<>(// 1 indexed (as ARRAY)
Integer.toString(i + 1), typeFactory.createSqlType(CQL_TO_SQL_TYPE.lookup(typeArgs.get(i))))).collect(Collectors.toList());
fieldInfo.add(columnName, typeFactory.createStructType(typesList)).nullable(true);
} else {
SqlTypeName typeName = CQL_TO_SQL_TYPE.lookup(dataType);
fieldInfo.add(columnName, typeName).nullable(true);
}
}
return RelDataTypeImpl.proto(fieldInfo.build());
}
use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.
the class JdbcSchema method parseTypeString.
/**
* Given "INTEGER", returns BasicSqlType(INTEGER).
* Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10).
* Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
*/
private static RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
int precision = -1;
int scale = -1;
int open = typeString.indexOf("(");
if (open >= 0) {
int close = typeString.indexOf(")", open);
if (close >= 0) {
String rest = typeString.substring(open + 1, close);
typeString = typeString.substring(0, open);
int comma = rest.indexOf(",");
if (comma >= 0) {
precision = Integer.parseInt(rest.substring(0, comma));
scale = Integer.parseInt(rest.substring(comma));
} else {
precision = Integer.parseInt(rest);
}
}
}
try {
final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
return typeName.allowsPrecScale(true, true) ? typeFactory.createSqlType(typeName, precision, scale) : typeName.allowsPrecScale(true, false) ? typeFactory.createSqlType(typeName, precision) : typeFactory.createSqlType(typeName);
} catch (IllegalArgumentException e) {
return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
}
}
Aggregations