Java Code Examples for org.apache.flink.table.types.CollectionDataType#getElementDataType()

The following examples show how to use org.apache.flink.table.types.CollectionDataType#getElementDataType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataTypePrecisionFixer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public DataType visit(CollectionDataType collectionDataType) {
	DataType elementType = collectionDataType.getElementDataType();
	switch (logicalType.getTypeRoot()) {
		case ARRAY:
			ArrayType arrayType = (ArrayType) logicalType;
			DataType newArrayElementType = elementType
				.accept(new DataTypePrecisionFixer(arrayType.getElementType()));
			return DataTypes
				.ARRAY(newArrayElementType)
				.bridgedTo(collectionDataType.getConversionClass());

		case MULTISET:
			MultisetType multisetType = (MultisetType) logicalType;
			DataType newMultisetElementType = elementType
				.accept(new DataTypePrecisionFixer(multisetType.getElementType()));
			return DataTypes
				.MULTISET(newMultisetElementType)
				.bridgedTo(collectionDataType.getConversionClass());

		default:
			throw new UnsupportedOperationException("Unsupported logical type : " + logicalType);
	}
}
 
Example 2
Source File: ValuesOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private Optional<ResolvedExpression> convertArrayToExpectedType(
		ResolvedExpression sourceExpression,
		CollectionDataType targetDataType,
		ExpressionResolver.PostResolverFactory postResolverFactory) {
	DataType elementTargetDataType = targetDataType.getElementDataType();
	List<ResolvedExpression> resolvedChildren = sourceExpression.getResolvedChildren();
	ResolvedExpression[] castedChildren = new ResolvedExpression[resolvedChildren.size()];
	for (int i = 0; i < resolvedChildren.size(); i++) {
		Optional<ResolvedExpression> castedChild = convertToExpectedType(
			resolvedChildren.get(i),
			elementTargetDataType,
			postResolverFactory);
		if (castedChild.isPresent()) {
			castedChildren[i] = castedChild.get();
		} else {
			return Optional.empty();
		}
	}
	return Optional.of(postResolverFactory.array(targetDataType, castedChildren));
}
 
Example 3
Source File: SchemaUtils.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
private static Schema sqlType2AvroSchema(DataType flinkType, boolean nullable, String recordName, String namespace) throws IncompatibleSchemaException {
    SchemaBuilder.TypeBuilder<Schema> builder = SchemaBuilder.builder();
    LogicalTypeRoot type = flinkType.getLogicalType().getTypeRoot();
    Schema schema = null;

    if (flinkType instanceof AtomicDataType) {
        switch (type) {
            case BOOLEAN:
                schema = builder.booleanType();
                break;
            case TINYINT:
            case SMALLINT:
            case INTEGER:
                schema = builder.intType();
                break;
            case BIGINT:
                schema = builder.longType();
                break;
            case DATE:
                schema = LogicalTypes.date().addToSchema(builder.intType());
                break;
            case TIMESTAMP_WITHOUT_TIME_ZONE:
                schema = LogicalTypes.timestampMicros().addToSchema(builder.longType());
                break;
            case FLOAT:
                schema = builder.floatType();
                break;
            case DOUBLE:
                schema = builder.doubleType();
                break;
            case VARCHAR:
                schema = builder.stringType();
                break;
            case BINARY:
            case VARBINARY:
                schema = builder.bytesType();
                break;
            case DECIMAL:
                DecimalType dt = (DecimalType) flinkType.getLogicalType();
                LogicalTypes.Decimal avroType = LogicalTypes.decimal(dt.getPrecision(), dt.getScale());
                int fixedSize = minBytesForPrecision[dt.getPrecision()];
                // Need to avoid naming conflict for the fixed fields
                String name;
                if (namespace.equals("")) {
                    name = recordName + ".fixed";
                } else {
                    name = namespace + recordName + ".fixed";
                }
                schema = avroType.addToSchema(SchemaBuilder.fixed(name).size(fixedSize));
                break;
            default:
                throw new IncompatibleSchemaException(String.format("Unsupported type %s", flinkType.toString()), null);
        }
    } else if (flinkType instanceof CollectionDataType) {
        if (type == LogicalTypeRoot.ARRAY) {
            CollectionDataType cdt = (CollectionDataType) flinkType;
            DataType elementType = cdt.getElementDataType();
            schema = builder.array().items(sqlType2AvroSchema(elementType, elementType.getLogicalType().isNullable(), recordName, namespace));
        } else {
            throw new IncompatibleSchemaException("Pulsar only support collection as array", null);
        }
    } else if (flinkType instanceof KeyValueDataType) {
        KeyValueDataType kvType = (KeyValueDataType) flinkType;
        DataType keyType = kvType.getKeyDataType();
        DataType valueType = kvType.getValueDataType();
        if (!(keyType instanceof AtomicDataType) || keyType.getLogicalType().getTypeRoot() != LogicalTypeRoot.VARCHAR) {
            throw new IncompatibleSchemaException("Pulsar only support string key map", null);
        }
        schema = builder.map().values(sqlType2AvroSchema(valueType, valueType.getLogicalType().isNullable(), recordName, namespace));
    } else if (flinkType instanceof FieldsDataType) {
        FieldsDataType fieldsDataType = (FieldsDataType) flinkType;
        String childNamespace = namespace.equals("") ? recordName : namespace + "." + recordName;
        SchemaBuilder.FieldAssembler<Schema> fieldsAssembler = builder.record(recordName).namespace(namespace).fields();
        RowType rowType = (RowType) fieldsDataType.getLogicalType();

        for (String fieldName : rowType.getFieldNames()) {
            DataType ftype = fieldsDataType.getFieldDataTypes().get(fieldName);
            Schema fieldAvroSchema = sqlType2AvroSchema(ftype, ftype.getLogicalType().isNullable(), fieldName, childNamespace);
            fieldsAssembler.name(fieldName).type(fieldAvroSchema).noDefault();
        }
        schema = fieldsAssembler.endRecord();
    } else {
        throw new IncompatibleSchemaException(String.format("Unexpected type %s", flinkType.toString()), null);
    }

    if (nullable) {
        return Schema.createUnion(schema, NULL_SCHEMA);
    } else {
        return schema;
    }
}