use of com.alibaba.innodb.java.reader.schema.TableDef 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());
}
Aggregations