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;
}
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);
}
}
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;
}
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();
}
Aggregations