use of org.apache.flink.sql.parser.hive.impl.ParseException in project flink by apache.
the class HiveDDLUtils method convertDataTypes.
private static SqlTypeNameSpec convertDataTypes(SqlTypeNameSpec nameSpec) throws ParseException {
if (nameSpec instanceof SqlBasicTypeNameSpec) {
SqlBasicTypeNameSpec basicNameSpec = (SqlBasicTypeNameSpec) nameSpec;
if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.TIMESTAMP.name())) {
if (basicNameSpec.getPrecision() < 0) {
nameSpec = new SqlBasicTypeNameSpec(SqlTypeName.TIMESTAMP, 9, basicNameSpec.getScale(), basicNameSpec.getCharSetName(), basicNameSpec.getParserPos());
}
} else if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.BINARY.name())) {
if (basicNameSpec.getPrecision() < 0) {
nameSpec = new SqlBasicTypeNameSpec(SqlTypeName.VARBINARY, Integer.MAX_VALUE, basicNameSpec.getScale(), basicNameSpec.getCharSetName(), basicNameSpec.getParserPos());
}
} else if (basicNameSpec.getTypeName().getSimple().equalsIgnoreCase(SqlTypeName.VARCHAR.name())) {
if (basicNameSpec.getPrecision() < 0) {
throw new ParseException("VARCHAR precision is mandatory");
}
}
} else if (nameSpec instanceof ExtendedSqlCollectionTypeNameSpec) {
ExtendedSqlCollectionTypeNameSpec collectionNameSpec = (ExtendedSqlCollectionTypeNameSpec) nameSpec;
SqlTypeNameSpec elementNameSpec = collectionNameSpec.getElementTypeName();
SqlTypeNameSpec convertedElementNameSpec = convertDataTypes(elementNameSpec);
if (convertedElementNameSpec != elementNameSpec) {
nameSpec = new ExtendedSqlCollectionTypeNameSpec(convertedElementNameSpec, collectionNameSpec.elementNullable(), collectionNameSpec.getCollectionTypeName(), collectionNameSpec.unparseAsStandard(), collectionNameSpec.getParserPos());
}
} else if (nameSpec instanceof SqlMapTypeNameSpec) {
SqlMapTypeNameSpec mapNameSpec = (SqlMapTypeNameSpec) nameSpec;
SqlDataTypeSpec keyTypeSpec = mapNameSpec.getKeyType();
SqlDataTypeSpec valTypeSpec = mapNameSpec.getValType();
SqlDataTypeSpec convertedKeyTypeSpec = convertDataTypes(keyTypeSpec);
SqlDataTypeSpec convertedValTypeSpec = convertDataTypes(valTypeSpec);
if (keyTypeSpec != convertedKeyTypeSpec || valTypeSpec != convertedValTypeSpec) {
nameSpec = new SqlMapTypeNameSpec(convertedKeyTypeSpec, convertedValTypeSpec, nameSpec.getParserPos());
}
} else if (nameSpec instanceof ExtendedSqlRowTypeNameSpec) {
ExtendedSqlRowTypeNameSpec rowNameSpec = (ExtendedSqlRowTypeNameSpec) nameSpec;
List<SqlDataTypeSpec> fieldTypeSpecs = rowNameSpec.getFieldTypes();
List<SqlDataTypeSpec> convertedFieldTypeSpecs = new ArrayList<>(fieldTypeSpecs.size());
boolean updated = false;
for (SqlDataTypeSpec fieldTypeSpec : fieldTypeSpecs) {
SqlDataTypeSpec convertedFieldTypeSpec = convertDataTypes(fieldTypeSpec);
if (fieldTypeSpec != convertedFieldTypeSpec) {
updated = true;
}
convertedFieldTypeSpecs.add(convertedFieldTypeSpec);
}
if (updated) {
nameSpec = new ExtendedSqlRowTypeNameSpec(nameSpec.getParserPos(), rowNameSpec.getFieldNames(), convertedFieldTypeSpecs, rowNameSpec.getComments(), rowNameSpec.unparseAsStandard());
}
}
return nameSpec;
}
Aggregations