Search in sources :

Example 1 with ExtendedSqlRowTypeNameSpec

use of org.apache.flink.sql.parser.type.ExtendedSqlRowTypeNameSpec 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;
}
Also used : SqlBasicTypeNameSpec(org.apache.calcite.sql.SqlBasicTypeNameSpec) SqlMapTypeNameSpec(org.apache.flink.sql.parser.type.SqlMapTypeNameSpec) ExtendedSqlCollectionTypeNameSpec(org.apache.flink.sql.parser.type.ExtendedSqlCollectionTypeNameSpec) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) ArrayList(java.util.ArrayList) List(java.util.List) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ParseException(org.apache.flink.sql.parser.hive.impl.ParseException) SqlTypeNameSpec(org.apache.calcite.sql.SqlTypeNameSpec) ExtendedSqlRowTypeNameSpec(org.apache.flink.sql.parser.type.ExtendedSqlRowTypeNameSpec)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 SqlBasicTypeNameSpec (org.apache.calcite.sql.SqlBasicTypeNameSpec)1 SqlDataTypeSpec (org.apache.calcite.sql.SqlDataTypeSpec)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlTypeNameSpec (org.apache.calcite.sql.SqlTypeNameSpec)1 ParseException (org.apache.flink.sql.parser.hive.impl.ParseException)1 ExtendedSqlCollectionTypeNameSpec (org.apache.flink.sql.parser.type.ExtendedSqlCollectionTypeNameSpec)1 ExtendedSqlRowTypeNameSpec (org.apache.flink.sql.parser.type.ExtendedSqlRowTypeNameSpec)1 SqlMapTypeNameSpec (org.apache.flink.sql.parser.type.SqlMapTypeNameSpec)1