Search in sources :

Example 31 with SqlTypeName

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);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 32 with SqlTypeName

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);
    }
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 33 with SqlTypeName

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);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TimeUnit(org.apache.calcite.avatica.util.TimeUnit) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) ArrayUtils.asList(org.apache.ignite.internal.util.ArrayUtils.asList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 34 with 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());
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RelDataType(org.apache.calcite.rel.type.RelDataType) DataType(com.datastax.oss.driver.api.core.type.DataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 35 with SqlTypeName

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);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)184 RelDataType (org.apache.calcite.rel.type.RelDataType)62 Test (org.junit.jupiter.api.Test)39 List (java.util.List)31 BigDecimal (java.math.BigDecimal)30 ArrayList (java.util.ArrayList)30 ImmutableList (com.google.common.collect.ImmutableList)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 Map (java.util.Map)25 RexNode (org.apache.calcite.rex.RexNode)25 NlsString (org.apache.calcite.util.NlsString)21 DateString (org.apache.calcite.util.DateString)18 TimeString (org.apache.calcite.util.TimeString)18 TimestampString (org.apache.calcite.util.TimestampString)18 ByteString (org.apache.calcite.avatica.util.ByteString)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)16 SqlKind (org.apache.calcite.sql.SqlKind)15 Calendar (java.util.Calendar)14 Objects (java.util.Objects)13 Util (org.apache.calcite.util.Util)13