org.apache.flink.table.types.KeyValueDataType Java Examples

The following examples show how to use org.apache.flink.table.types.KeyValueDataType. 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: ValuesOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private Optional<ResolvedExpression> convertMapToExpectedType(
		ResolvedExpression sourceExpression,
		KeyValueDataType targetDataType,
		ExpressionResolver.PostResolverFactory postResolverFactory) {
	DataType keyTargetDataType = targetDataType.getKeyDataType();
	DataType valueTargetDataType = targetDataType.getValueDataType();
	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),
			i % 2 == 0 ? keyTargetDataType : valueTargetDataType,
			postResolverFactory);
		if (castedChild.isPresent()) {
			castedChildren[i] = castedChild.get();
		} else {
			return Optional.empty();
		}
	}

	return Optional.of(postResolverFactory.map(targetDataType, castedChildren));
}
 
Example #2
Source File: DataTypeUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public DataType visit(KeyValueDataType keyValueDataType) {
	DataType newKeyType = keyValueDataType.getKeyDataType().accept(this);
	DataType newValueType = keyValueDataType.getValueDataType().accept(this);
	LogicalType logicalType = keyValueDataType.getLogicalType();
	LogicalType newLogicalType;
	if (logicalType instanceof MapType) {
		newLogicalType = new MapType(
			logicalType.isNullable(),
			newKeyType.getLogicalType(),
			newValueType.getLogicalType());
	} else {
		throw new UnsupportedOperationException("Unsupported logical type : " + logicalType);
	}
	return transformation.transform(new KeyValueDataType(newLogicalType, newKeyType, newValueType));
}
 
Example #3
Source File: LogicalTypeDataTypeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataType visit(MapType mapType) {
	return new KeyValueDataType(
		mapType,
		mapType.getKeyType().accept(this),
		mapType.getValueType().accept(this));
}
 
Example #4
Source File: LogicalTypeDataTypeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataType visit(MapType mapType) {
	return new KeyValueDataType(
		mapType,
		mapType.getKeyType().accept(this),
		mapType.getValueType().accept(this));
}
 
Example #5
Source File: FlinkTypeVisitor.java    From iceberg with Apache License 2.0 5 votes vote down vote up
static <T> T visit(DataType dataType, FlinkTypeVisitor<T> visitor) {
  if (dataType instanceof FieldsDataType) {
    FieldsDataType fieldsType = (FieldsDataType) dataType;
    Map<String, DataType> fields = fieldsType.getFieldDataTypes();
    List<T> fieldResults = Lists.newArrayList();

    Preconditions.checkArgument(dataType.getLogicalType() instanceof RowType, "The logical type must be RowType");
    List<RowType.RowField> rowFields = ((RowType) dataType.getLogicalType()).getFields();
    // Make sure that we're traveling in the same order as the RowFields because the implementation of
    // FlinkTypeVisitor#fields may depends on the visit order, please see FlinkTypeToType#fields.
    for (RowType.RowField rowField : rowFields) {
      String name = rowField.getName();
      fieldResults.add(visit(fields.get(name), visitor));
    }

    return visitor.fields(fieldsType, fieldResults);
  } else if (dataType instanceof CollectionDataType) {
    CollectionDataType collectionType = (CollectionDataType) dataType;
    return visitor.collection(collectionType,
        visit(collectionType.getElementDataType(), visitor));
  } else if (dataType instanceof KeyValueDataType) {
    KeyValueDataType mapType = (KeyValueDataType) dataType;
    return visitor.map(mapType,
        visit(mapType.getKeyDataType(), visitor),
        visit(mapType.getValueDataType(), visitor));
  } else if (dataType instanceof AtomicDataType) {
    AtomicDataType atomic = (AtomicDataType) dataType;
    return visitor.atomic(atomic);
  } else {
    throw new UnsupportedOperationException("Unsupported data type: " + dataType);
  }
}
 
Example #6
Source File: DataTypePrecisionFixer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataType visit(KeyValueDataType keyValueDataType) {
	DataType keyType = keyValueDataType.getKeyDataType();
	DataType valueType = keyValueDataType.getValueDataType();
	if (logicalType.getTypeRoot() == LogicalTypeRoot.MAP) {
		MapType mapType = (MapType) logicalType;
		DataType newKeyType = keyType.accept(new DataTypePrecisionFixer(mapType.getKeyType()));
		DataType newValueType = valueType.accept(new DataTypePrecisionFixer(mapType.getValueType()));
		return DataTypes
			.MAP(newKeyType, newValueType)
			.bridgedTo(keyValueDataType.getConversionClass());
	}
	throw new UnsupportedOperationException("Unsupported logical type : " + logicalType);
}
 
Example #7
Source File: TypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TypeInformation<?> fromDataTypeToTypeInfo(DataType dataType) {
	Class<?> clazz = dataType.getConversionClass();
	if (clazz.isPrimitive()) {
		final TypeInformation<?> foundTypeInfo = primitiveDataTypeTypeInfoMap.get(clazz.getName());
		if (foundTypeInfo != null) {
			return foundTypeInfo;
		}
	}
	LogicalType logicalType = fromDataTypeToLogicalType(dataType);
	switch (logicalType.getTypeRoot()) {
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			TimestampType timestampType = (TimestampType) logicalType;
			int precision = timestampType.getPrecision();
			if (timestampType.getKind() == TimestampKind.REGULAR) {
				return clazz == TimestampData.class ?
					new TimestampDataTypeInfo(precision) :
					(clazz == LocalDateTime.class ?
						((3 == precision) ?
							Types.LOCAL_DATE_TIME : new LegacyLocalDateTimeTypeInfo(precision)) :
						((3 == precision) ?
							Types.SQL_TIMESTAMP : new LegacyTimestampTypeInfo(precision)));
			} else {
				return TypeConversions.fromDataTypeToLegacyInfo(dataType);
			}
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			LocalZonedTimestampType lzTs = (LocalZonedTimestampType) logicalType;
			int precisionLzTs = lzTs.getPrecision();
			return clazz == TimestampData.class ?
				new TimestampDataTypeInfo(precisionLzTs) :
				(clazz == Instant.class ?
					((3 == precisionLzTs) ? Types.INSTANT : new LegacyInstantTypeInfo(precisionLzTs)) :
					TypeConversions.fromDataTypeToLegacyInfo(dataType));

		case DECIMAL:
			DecimalType decimalType = (DecimalType) logicalType;
			return clazz == DecimalData.class ?
					new DecimalDataTypeInfo(decimalType.getPrecision(), decimalType.getScale()) :
					new BigDecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
		case CHAR:
		case VARCHAR: // ignore precision
			return clazz == StringData.class ?
					StringDataTypeInfo.INSTANCE :
					BasicTypeInfo.STRING_TYPE_INFO;
		case BINARY:
		case VARBINARY: // ignore precision
			return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
		case INTERVAL_YEAR_MONTH:
			return TimeIntervalTypeInfo.INTERVAL_MONTHS;
		case INTERVAL_DAY_TIME:
			return TimeIntervalTypeInfo.INTERVAL_MILLIS;
		case ARRAY:
			if (dataType instanceof CollectionDataType &&
					!isPrimitive(((CollectionDataType) dataType).getElementDataType().getLogicalType())) {
				return ObjectArrayTypeInfo.getInfoFor(
						fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
			} else {
				return TypeConversions.fromDataTypeToLegacyInfo(dataType);
			}
		case MAP:
			KeyValueDataType mapType = (KeyValueDataType) dataType;
			return new MapTypeInfo(
					fromDataTypeToTypeInfo(mapType.getKeyDataType()),
					fromDataTypeToTypeInfo(mapType.getValueDataType()));
		case MULTISET:
			return MultisetTypeInfo.getInfoFor(
					fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
		case ROW:
			if (RowData.class.isAssignableFrom(dataType.getConversionClass())) {
				return RowDataTypeInfo.of((RowType) fromDataTypeToLogicalType(dataType));
			} else if (Row.class == dataType.getConversionClass()) {
				RowType logicalRowType = (RowType) logicalType;
				return new RowTypeInfo(
					dataType.getChildren()
						.stream()
						.map(TypeInfoDataTypeConverter::fromDataTypeToTypeInfo)
						.toArray(TypeInformation[]::new),
					logicalRowType.getFieldNames().toArray(new String[0]));
			} else {
				return TypeConversions.fromDataTypeToLegacyInfo(dataType);
			}
		case RAW:
			if (logicalType instanceof RawType) {
				final RawType<?> rawType = (RawType<?>) logicalType;
				return createWrapperTypeInfo(rawType);
			}
			return TypeConversions.fromDataTypeToLegacyInfo(dataType);
		default:
			return TypeConversions.fromDataTypeToLegacyInfo(dataType);
	}
}
 
Example #8
Source File: ValuesOperationFactory.java    From flink with Apache License 2.0 4 votes vote down vote up
private Optional<ResolvedExpression> convertToExpectedType(
		ResolvedExpression sourceExpression,
		DataType targetDataType,
		ExpressionResolver.PostResolverFactory postResolverFactory) {

	LogicalType sourceLogicalType = sourceExpression.getOutputDataType().getLogicalType();
	LogicalType targetLogicalType = targetDataType.getLogicalType();

	// if the expression is a literal try converting the literal in place instead of casting
	if (sourceExpression instanceof ValueLiteralExpression) {
		// Assign a type to a null literal
		if (hasRoot(sourceLogicalType, LogicalTypeRoot.NULL)) {
			return Optional.of(valueLiteral(null, targetDataType));
		}

		// Check if the source value class is a valid input conversion class of the target type
		// It may happen that a user wanted to use a secondary input conversion class as a value for
		// a different type than what we derived.
		//
		// Example: we interpreted 1L as BIGINT, but user wanted to interpret it as a TIMESTAMP
		// In this case long is a valid conversion class for TIMESTAMP, but a
		// cast from BIGINT to TIMESTAMP is an invalid operation.
		Optional<Object> value = ((ValueLiteralExpression) sourceExpression).getValueAs(Object.class);
		if (value.isPresent() && targetLogicalType.supportsInputConversion(value.get().getClass())) {
			ValueLiteralExpression convertedLiteral = valueLiteral(
				value.get(),
				targetDataType.notNull().bridgedTo(value.get().getClass()));
			if (targetLogicalType.isNullable()) {
				return Optional.of(postResolverFactory.cast(convertedLiteral, targetDataType));
			} else {
				return Optional.of(convertedLiteral);
			}
		}
	}

	if (sourceExpression instanceof CallExpression) {
		FunctionDefinition functionDefinition = ((CallExpression) sourceExpression).getFunctionDefinition();
		if (functionDefinition == BuiltInFunctionDefinitions.ROW &&
				hasRoot(targetLogicalType, LogicalTypeRoot.ROW)) {
			return convertRowToExpectedType(sourceExpression, (FieldsDataType) targetDataType, postResolverFactory);
		} else if (functionDefinition == BuiltInFunctionDefinitions.ARRAY &&
					hasRoot(targetLogicalType, LogicalTypeRoot.ARRAY)) {
			return convertArrayToExpectedType(
				sourceExpression,
				(CollectionDataType) targetDataType,
				postResolverFactory);
		} else if (functionDefinition == BuiltInFunctionDefinitions.MAP &&
					hasRoot(targetLogicalType, LogicalTypeRoot.MAP)) {
			return convertMapToExpectedType(
				sourceExpression,
				(KeyValueDataType) targetDataType,
				postResolverFactory);
		}
	}

	// We might not be able to cast to the expected type if the expected type was provided by the user
	// we ignore nullability constraints here, as we let users override what we expect there, e.g. they
	// might know that a certain function will not produce nullable values for a given input
	if (supportsExplicitCast(
			sourceLogicalType.copy(true),
			targetLogicalType.copy(true))) {
		return Optional.of(postResolverFactory.cast(sourceExpression, targetDataType));
	} else {
		return Optional.empty();
	}
}
 
Example #9
Source File: LegacyTypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToMapTypeInfo(KeyValueDataType dataType) {
	return Types.MAP(
		toLegacyTypeInfo(dataType.getKeyDataType()),
		toLegacyTypeInfo(dataType.getValueDataType()));
}
 
Example #10
Source File: LegacyTypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TypeInformation<?> toLegacyTypeInfo(DataType dataType) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (canConvertToTimeAttributeTypeInfo(dataType)) {
		return convertToTimeAttributeTypeInfo((TimestampType) dataType.getLogicalType());
	}

	// check in the map but relax the nullability constraint as every not null data type can be
	// stored in the corresponding nullable type information
	final TypeInformation<?> foundTypeInfo = dataTypeTypeInfoMap.get(dataType.nullable());
	if (foundTypeInfo != null) {
		return foundTypeInfo;
	}

	// we are relaxing the constraint for DECIMAL, CHAR, VARCHAR, TIMESTAMP_WITHOUT_TIME_ZONE to
	// support value literals in legacy planner
	LogicalType logicalType = dataType.getLogicalType();
	if (hasRoot(logicalType, LogicalTypeRoot.DECIMAL)) {
		return Types.BIG_DEC;
	}

	else if (hasRoot(logicalType, LogicalTypeRoot.CHAR)) {
		return Types.STRING;
	}

	else if (hasRoot(logicalType, LogicalTypeRoot.VARCHAR)) {
		return Types.STRING;
	}

	// relax the precision constraint as Timestamp can store the highest precision
	else if (hasRoot(logicalType, LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) &&
		dataType.getConversionClass() == Timestamp.class) {
		return Types.SQL_TIMESTAMP;
	}

	// relax the precision constraint as LocalDateTime can store the highest precision
	else if (hasRoot(logicalType, LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) &&
		dataType.getConversionClass() == LocalDateTime.class) {
		return Types.LOCAL_DATE_TIME;
	}

	// relax the precision constraint as LocalTime can store the highest precision
	else if (hasRoot(logicalType, LogicalTypeRoot.TIME_WITHOUT_TIME_ZONE) &&
		dataType.getConversionClass() == LocalTime.class) {
		return Types.LOCAL_TIME;
	}

	else if (canConvertToLegacyTypeInfo(dataType)) {
		return convertToLegacyTypeInfo(dataType);
	}

	else if (canConvertToRowTypeInfo(dataType)) {
		return convertToRowTypeInfo((FieldsDataType) dataType);
	}

	// this could also match for basic array type info but this is covered by legacy type info
	else if (canConvertToObjectArrayTypeInfo(dataType)) {
		return convertToObjectArrayTypeInfo((CollectionDataType) dataType);
	}

	else if (canConvertToMultisetTypeInfo(dataType)) {
		return convertToMultisetTypeInfo((CollectionDataType) dataType);
	}

	else if (canConvertToMapTypeInfo(dataType)) {
		return convertToMapTypeInfo((KeyValueDataType) dataType);
	}

	// makes the raw type accessible in the legacy planner
	else if (canConvertToRawTypeInfo(dataType)) {
		return convertToRawTypeInfo(dataType);
	}

	throw new TableException(
		String.format(
			"Unsupported conversion from data type '%s' (conversion class: %s) to type information. Only data types " +
				"that originated from type information fully support a reverse conversion.",
			dataType,
			dataType.getConversionClass().getName()));
}
 
Example #11
Source File: DataTypeDefaultVisitor.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public R visit(KeyValueDataType keyValueDataType) {
	return defaultMethod(keyValueDataType);
}
 
Example #12
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;
    }
}
 
Example #13
Source File: FlinkTypeVisitor.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public T map(KeyValueDataType type, T keyResult, T valueResult) {
  return null;
}
 
Example #14
Source File: LegacyTypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToMapTypeInfo(KeyValueDataType dataType) {
	return Types.MAP(
		toLegacyTypeInfo(dataType.getKeyDataType()),
		toLegacyTypeInfo(dataType.getValueDataType()));
}
 
Example #15
Source File: LegacyTypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TypeInformation<?> toLegacyTypeInfo(DataType dataType) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (canConvertToTimeAttributeTypeInfo(dataType)) {
		return convertToTimeAttributeTypeInfo((TimestampType) dataType.getLogicalType());
	}

	// check in the map but relax the nullability constraint as every not null data type can be
	// stored in the corresponding nullable type information
	final TypeInformation<?> foundTypeInfo = dataTypeTypeInfoMap.get(dataType.nullable());
	if (foundTypeInfo != null) {
		return foundTypeInfo;
	}

	// we are relaxing the constraint for DECIMAL, CHAR, VARCHAR, TIMESTAMP_WITHOUT_TIME_ZONE to
	// support value literals in legacy planner
	LogicalType logicalType = dataType.getLogicalType();
	if (hasRoot(logicalType, LogicalTypeRoot.DECIMAL)) {
		return Types.BIG_DEC;
	}

	else if (hasRoot(logicalType, LogicalTypeRoot.CHAR)) {
		return Types.STRING;
	}

	else if (hasRoot(logicalType, LogicalTypeRoot.VARCHAR)) {
		return Types.STRING;
	}

	else if (canConvertToTimestampTypeInfoLenient(dataType)) {
		return Types.SQL_TIMESTAMP;
	}

	else if (canConvertToLegacyTypeInfo(dataType)) {
		return convertToLegacyTypeInfo(dataType);
	}

	else if (canConvertToRowTypeInfo(dataType)) {
		return convertToRowTypeInfo((FieldsDataType) dataType);
	}

	// this could also match for basic array type info but this is covered by legacy type info
	else if (canConvertToObjectArrayTypeInfo(dataType)) {
		return convertToObjectArrayTypeInfo((CollectionDataType) dataType);
	}

	else if (canConvertToMultisetTypeInfo(dataType)) {
		return convertToMultisetTypeInfo((CollectionDataType) dataType);
	}

	else if (canConvertToMapTypeInfo(dataType)) {
		return convertToMapTypeInfo((KeyValueDataType) dataType);
	}

	// makes the any type accessible in the legacy planner
	else if (canConvertToAnyTypeInfo(dataType)) {
		return convertToAnyTypeInfo(dataType);
	}

	throw new TableException(
		String.format(
			"Unsupported conversion from data type '%s' (conversion class: %s) to type information. Only data types " +
				"that originated from type information fully support a reverse conversion.",
			dataType,
			dataType.getConversionClass().getName()));
}
 
Example #16
Source File: DataTypeDefaultVisitor.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public R visit(KeyValueDataType keyValueDataType) {
	return defaultMethod(keyValueDataType);
}
 
Example #17
Source File: TypeInfoDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TypeInformation<?> fromDataTypeToTypeInfo(DataType dataType) {
	Class<?> clazz = dataType.getConversionClass();
	if (clazz.isPrimitive()) {
		final TypeInformation<?> foundTypeInfo = primitiveDataTypeTypeInfoMap.get(clazz.getName());
		if (foundTypeInfo != null) {
			return foundTypeInfo;
		}
	}
	LogicalType logicalType = fromDataTypeToLogicalType(dataType);
	switch (logicalType.getTypeRoot()) {
		case DECIMAL:
			DecimalType decimalType = (DecimalType) logicalType;
			return clazz == Decimal.class ?
					new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()) :
					new BigDecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
		case CHAR:
		case VARCHAR: // ignore precision
			return clazz == BinaryString.class ?
					BinaryStringTypeInfo.INSTANCE :
					BasicTypeInfo.STRING_TYPE_INFO;
		case BINARY:
		case VARBINARY: // ignore precision
			return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
		case INTERVAL_YEAR_MONTH:
			return TimeIntervalTypeInfo.INTERVAL_MONTHS;
		case INTERVAL_DAY_TIME:
			return TimeIntervalTypeInfo.INTERVAL_MILLIS;
		case ARRAY:
			if (dataType instanceof CollectionDataType &&
					!isPrimitive(((CollectionDataType) dataType).getElementDataType().getLogicalType())) {
				return ObjectArrayTypeInfo.getInfoFor(
						fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
			} else {
				return TypeConversions.fromDataTypeToLegacyInfo(dataType);
			}
		case MAP:
			KeyValueDataType mapType = (KeyValueDataType) dataType;
			return new MapTypeInfo(
					fromDataTypeToTypeInfo(mapType.getKeyDataType()),
					fromDataTypeToTypeInfo(mapType.getValueDataType()));
		case MULTISET:
			return MultisetTypeInfo.getInfoFor(
					fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
		case ROW:
			if (BaseRow.class.isAssignableFrom(dataType.getConversionClass())) {
				return BaseRowTypeInfo.of((RowType) fromDataTypeToLogicalType(dataType));
			} else if (Row.class == dataType.getConversionClass()) {
				FieldsDataType rowType = (FieldsDataType) dataType;
				RowType logicalRowType = (RowType) logicalType;
				return new RowTypeInfo(
						logicalRowType.getFieldNames().stream()
								.map(name -> rowType.getFieldDataTypes().get(name))
								.map(TypeInfoDataTypeConverter::fromDataTypeToTypeInfo)
								.toArray(TypeInformation[]::new),
						logicalRowType.getFieldNames().toArray(new String[0]));
			} else {
				return TypeConversions.fromDataTypeToLegacyInfo(dataType);
			}
		default:
			return TypeConversions.fromDataTypeToLegacyInfo(dataType);
	}
}
 
Example #18
Source File: DataTypes.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Data type of an associative array that maps keys (including {@code NULL}) to values (including
 * {@code NULL}). A map cannot contain duplicate keys; each key can map to at most one value.
 *
 * <p>There is no restriction of key types; it is the responsibility of the user to ensure uniqueness.
 * The map type is an extension to the SQL standard.
 *
 * @see MapType
 */
public static DataType MAP(DataType keyDataType, DataType valueDataType) {
	Preconditions.checkNotNull(keyDataType, "Key data type must not be null.");
	Preconditions.checkNotNull(valueDataType, "Value data type must not be null.");
	return new KeyValueDataType(
		new MapType(keyDataType.getLogicalType(), valueDataType.getLogicalType()),
		keyDataType,
		valueDataType);
}
 
Example #19
Source File: DataTypes.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Data type of an associative array that maps keys (including {@code NULL}) to values (including
 * {@code NULL}). A map cannot contain duplicate keys; each key can map to at most one value.
 *
 * <p>There is no restriction of key types; it is the responsibility of the user to ensure uniqueness.
 * The map type is an extension to the SQL standard.
 *
 * @see MapType
 */
public static DataType MAP(DataType keyDataType, DataType valueDataType) {
	Preconditions.checkNotNull(keyDataType, "Key data type must not be null.");
	Preconditions.checkNotNull(valueDataType, "Value data type must not be null.");
	return new KeyValueDataType(
		new MapType(keyDataType.getLogicalType(), valueDataType.getLogicalType()),
		keyDataType,
		valueDataType);
}