Search in sources :

Example 1 with Column

use of com.alibaba.innodb.java.reader.schema.Column in project calcite by apache.

the class InnodbSchema method getRelDataType.

RelProtoDataType getRelDataType(String tableName) {
    // 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();
    if (!tableReaderFactory.existTableDef(tableName)) {
        throw new RuntimeException("Table definition " + tableName + " not found");
    }
    TableDef tableDef = tableReaderFactory.getTableDef(tableName);
    for (Column column : tableDef.getColumnList()) {
        final SqlTypeName sqlTypeName = COLUMN_TYPE_TO_SQL_TYPE.lookup(column.getType());
        final int precision;
        final int scale;
        switch(column.getType()) {
            case ColumnType.TIMESTAMP:
            case ColumnType.TIME:
            case ColumnType.DATETIME:
                precision = column.getPrecision();
                scale = 0;
                break;
            default:
                precision = column.getPrecision();
                scale = column.getScale();
                break;
        }
        if (sqlTypeName.allowsPrecScale(true, true) && column.getPrecision() >= 0 && column.getScale() >= 0) {
            fieldInfo.add(column.getName(), sqlTypeName, precision, scale);
        } else if (sqlTypeName.allowsPrecNoScale() && precision >= 0) {
            fieldInfo.add(column.getName(), sqlTypeName, precision);
        } else {
            assert sqlTypeName.allowsNoPrecNoScale();
            fieldInfo.add(column.getName(), sqlTypeName);
        }
        fieldInfo.nullable(column.isNullable());
    }
    return RelDataTypeImpl.proto(fieldInfo.build());
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Column(com.alibaba.innodb.java.reader.schema.Column) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TableDef(com.alibaba.innodb.java.reader.schema.TableDef)

Aggregations

Column (com.alibaba.innodb.java.reader.schema.Column)1 TableDef (com.alibaba.innodb.java.reader.schema.TableDef)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 SqlTypeFactoryImpl (org.apache.calcite.sql.type.SqlTypeFactoryImpl)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1