Search in sources :

Example 6 with SqlDataTypeSpec

use of org.apache.calcite.sql.SqlDataTypeSpec in project hazelcast by hazelcast.

the class HazelcastTypeCoercion method coerceNode.

private boolean coerceNode(SqlValidatorScope scope, SqlNode node, RelDataType targetType, Consumer<SqlNode> replaceFn) {
    // Just update the inferred type if casting is not needed
    if (!requiresCast(scope, node, targetType)) {
        updateInferredType(node, targetType);
        return false;
    }
    SqlDataTypeSpec targetTypeSpec;
    if (targetType instanceof HazelcastIntegerType) {
        targetTypeSpec = new SqlDataTypeSpec(new HazelcastIntegerTypeNameSpec((HazelcastIntegerType) targetType), SqlParserPos.ZERO);
    } else if (targetType.getSqlTypeName() == ANY) {
        // without this the subsequent call to UnsupportedOperationVerifier will fail with "we do not support ANY"
        targetTypeSpec = new SqlDataTypeSpec(new SqlUserDefinedTypeNameSpec("OBJECT", SqlParserPos.ZERO), SqlParserPos.ZERO).withNullable(targetType.isNullable());
    } else if (targetType.getFamily() == HazelcastJsonType.FAMILY) {
        targetTypeSpec = HazelcastJsonType.TYPE_SPEC;
    } else {
        targetTypeSpec = SqlTypeUtil.convertTypeToSpec(targetType);
    }
    SqlNode cast = cast(node, targetTypeSpec);
    replaceFn.accept(cast);
    validator.deriveType(scope, cast);
    return true;
}
Also used : SqlUserDefinedTypeNameSpec(org.apache.calcite.sql.SqlUserDefinedTypeNameSpec) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlDataTypeSpec

use of org.apache.calcite.sql.SqlDataTypeSpec in project calcite by apache.

the class SqlTypeUtil method convertTypeToSpec.

/**
 * Converts an instance of RelDataType to an instance of SqlDataTypeSpec.
 *
 * @param type type descriptor
 * @return corresponding parse representation
 */
public static SqlDataTypeSpec convertTypeToSpec(RelDataType type) {
    SqlTypeName typeName = type.getSqlTypeName();
    // interval types, multiset types, etc
    assert typeName != null;
    SqlIdentifier typeIdentifier = new SqlIdentifier(typeName.name(), SqlParserPos.ZERO);
    String charSetName = null;
    if (inCharFamily(type)) {
        charSetName = type.getCharset().name();
    // TODO jvs 28-Dec-2004:  collation
    }
    if (typeName.allowsScale()) {
        return new SqlDataTypeSpec(typeIdentifier, type.getPrecision(), type.getScale(), charSetName, null, SqlParserPos.ZERO);
    } else if (typeName.allowsPrec()) {
        return new SqlDataTypeSpec(typeIdentifier, type.getPrecision(), -1, charSetName, null, SqlParserPos.ZERO);
    } else {
        return new SqlDataTypeSpec(typeIdentifier, -1, -1, charSetName, null, SqlParserPos.ZERO);
    }
}
Also used : SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 8 with SqlDataTypeSpec

use of org.apache.calcite.sql.SqlDataTypeSpec 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)

Example 9 with SqlDataTypeSpec

use of org.apache.calcite.sql.SqlDataTypeSpec in project calcite by apache.

the class SqlValidatorUtil method getExtendedColumns.

/**
 * Gets a list of extended columns with field indices to the underlying table.
 */
public static List<RelDataTypeField> getExtendedColumns(RelDataTypeFactory typeFactory, SqlValidatorTable table, SqlNodeList extendedColumns) {
    final ImmutableList.Builder<RelDataTypeField> extendedFields = ImmutableList.builder();
    final ExtensibleTable extTable = table.unwrap(ExtensibleTable.class);
    int extendedFieldOffset = extTable == null ? table.getRowType().getFieldCount() : extTable.getExtendedColumnOffset();
    for (final Pair<SqlIdentifier, SqlDataTypeSpec> pair : pairs(extendedColumns)) {
        final SqlIdentifier identifier = pair.left;
        final SqlDataTypeSpec type = pair.right;
        extendedFields.add(new RelDataTypeFieldImpl(identifier.toString(), extendedFieldOffset++, type.deriveType(typeFactory)));
    }
    return extendedFields.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ImmutableList(com.google.common.collect.ImmutableList) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable)

Aggregations

SqlDataTypeSpec (org.apache.calcite.sql.SqlDataTypeSpec)9 RelDataType (org.apache.calcite.rel.type.RelDataType)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlNode (org.apache.calcite.sql.SqlNode)3 RexNode (org.apache.calcite.rex.RexNode)2 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)2 SqlTypeNameSpec (org.apache.calcite.sql.SqlTypeNameSpec)2 ImmutableList (com.google.common.collect.ImmutableList)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)1 RexLiteral (org.apache.calcite.rex.RexLiteral)1 ExtensibleTable (org.apache.calcite.schema.ExtensibleTable)1 SqlBasicTypeNameSpec (org.apache.calcite.sql.SqlBasicTypeNameSpec)1