Java Code Examples for org.apache.flink.api.common.typeinfo.Types#BIG_DEC

The following examples show how to use org.apache.flink.api.common.typeinfo.Types#BIG_DEC . 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: JsonRowSerializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Optional<SerializationRuntimeConverter> createConverterForSimpleType(TypeInformation<?> simpleTypeInfo) {
	if (simpleTypeInfo == Types.VOID) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().nullNode());
	} else if (simpleTypeInfo == Types.BOOLEAN) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().booleanNode((Boolean) object));
	} else if (simpleTypeInfo == Types.STRING) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().textNode((String) object));
	} else if (simpleTypeInfo == Types.INT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Integer) object));
	} else if (simpleTypeInfo == Types.LONG) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Long) object));
	} else if (simpleTypeInfo == Types.DOUBLE) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Double) object));
	} else if (simpleTypeInfo == Types.FLOAT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Float) object));
	} else if (simpleTypeInfo == Types.SHORT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Short) object));
	} else if (simpleTypeInfo == Types.BYTE) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Byte) object));
	} else if (simpleTypeInfo == Types.BIG_DEC) {
		return Optional.of(createBigDecimalConverter());
	} else if (simpleTypeInfo == Types.BIG_INT) {
		return Optional.of(createBigIntegerConverter());
	} else if (simpleTypeInfo == Types.SQL_DATE) {
		return Optional.of(createDateConverter());
	} else if (simpleTypeInfo == Types.SQL_TIME) {
		return Optional.of(createTimeConverter());
	} else if (simpleTypeInfo == Types.SQL_TIMESTAMP) {
		return Optional.of(createTimestampConverter());
	} else {
		return Optional.empty();
	}
}
 
Example 2
Source File: JsonRowDeserializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Optional<DeserializationRuntimeConverter> createConverterForSimpleType(TypeInformation<?> simpleTypeInfo) {
	if (simpleTypeInfo == Types.VOID) {
		return Optional.of((mapper, jsonNode) -> null);
	} else if (simpleTypeInfo == Types.BOOLEAN) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asBoolean());
	} else if (simpleTypeInfo == Types.STRING) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asText());
	} else if (simpleTypeInfo == Types.INT) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asInt());
	} else if (simpleTypeInfo == Types.LONG) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asLong());
	} else if (simpleTypeInfo == Types.DOUBLE) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asDouble());
	} else if (simpleTypeInfo == Types.FLOAT) {
		return Optional.of((mapper, jsonNode) -> Float.parseFloat(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.SHORT) {
		return Optional.of((mapper, jsonNode) -> Short.parseShort(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.BYTE) {
		return Optional.of((mapper, jsonNode) -> Byte.parseByte(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.BIG_DEC) {
		return Optional.of((mapper, jsonNode) -> jsonNode.decimalValue());
	} else if (simpleTypeInfo == Types.BIG_INT) {
		return Optional.of((mapper, jsonNode) -> jsonNode.bigIntegerValue());
	} else if (simpleTypeInfo == Types.SQL_DATE) {
		return Optional.of(createDateConverter());
	} else if (simpleTypeInfo == Types.SQL_TIME) {
		return Optional.of(createTimeConverter());
	} else if (simpleTypeInfo == Types.SQL_TIMESTAMP) {
		return Optional.of(createTimestampConverter());
	} else {
		return Optional.empty();
	}
}
 
Example 3
Source File: JsonRowSerializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Optional<SerializationRuntimeConverter> createConverterForSimpleType(TypeInformation<?> simpleTypeInfo) {
	if (simpleTypeInfo == Types.VOID) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().nullNode());
	} else if (simpleTypeInfo == Types.BOOLEAN) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().booleanNode((Boolean) object));
	} else if (simpleTypeInfo == Types.STRING) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().textNode((String) object));
	} else if (simpleTypeInfo == Types.INT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Integer) object));
	} else if (simpleTypeInfo == Types.LONG) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Long) object));
	} else if (simpleTypeInfo == Types.DOUBLE) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Double) object));
	} else if (simpleTypeInfo == Types.FLOAT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Float) object));
	} else if (simpleTypeInfo == Types.SHORT) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Short) object));
	} else if (simpleTypeInfo == Types.BYTE) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().numberNode((Byte) object));
	} else if (simpleTypeInfo == Types.BIG_DEC) {
		return Optional.of(createBigDecimalConverter());
	} else if (simpleTypeInfo == Types.BIG_INT) {
		return Optional.of(createBigIntegerConverter());
	} else if (simpleTypeInfo == Types.SQL_DATE) {
		return Optional.of(this::convertDate);
	} else if (simpleTypeInfo == Types.SQL_TIME) {
		return Optional.of(this::convertTime);
	} else if (simpleTypeInfo == Types.SQL_TIMESTAMP) {
		return Optional.of(this::convertTimestamp);
	} else if (simpleTypeInfo == Types.LOCAL_DATE) {
		return Optional.of(this::convertLocalDate);
	} else if (simpleTypeInfo == Types.LOCAL_TIME) {
		return Optional.of(this::convertLocalTime);
	} else if (simpleTypeInfo == Types.LOCAL_DATE_TIME) {
		return Optional.of(this::convertLocalDateTime);
	} else {
		return Optional.empty();
	}
}
 
Example 4
Source File: JsonRowDeserializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Optional<DeserializationRuntimeConverter> createConverterForSimpleType(TypeInformation<?> simpleTypeInfo) {
	if (simpleTypeInfo == Types.VOID) {
		return Optional.of((mapper, jsonNode) -> null);
	} else if (simpleTypeInfo == Types.BOOLEAN) {
		return Optional.of(this::convertToBoolean);
	} else if (simpleTypeInfo == Types.STRING) {
		return Optional.of((mapper, jsonNode) -> jsonNode.asText());
	} else if (simpleTypeInfo == Types.INT) {
		return Optional.of(this::convertToInt);
	} else if (simpleTypeInfo == Types.LONG) {
		return Optional.of(this::convertToLong);
	} else if (simpleTypeInfo == Types.DOUBLE) {
		return Optional.of(this::convertToDouble);
	} else if (simpleTypeInfo == Types.FLOAT) {
		return Optional.of((mapper, jsonNode) -> Float.parseFloat(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.SHORT) {
		return Optional.of((mapper, jsonNode) -> Short.parseShort(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.BYTE) {
		return Optional.of((mapper, jsonNode) -> Byte.parseByte(jsonNode.asText().trim()));
	} else if (simpleTypeInfo == Types.BIG_DEC) {
		return Optional.of(this::convertToBigDecimal);
	} else if (simpleTypeInfo == Types.BIG_INT) {
		return Optional.of(this::convertToBigInteger);
	} else if (simpleTypeInfo == Types.SQL_DATE) {
		return Optional.of(this::convertToDate);
	} else if (simpleTypeInfo == Types.SQL_TIME) {
		return Optional.of(this::convertToTime);
	} else if (simpleTypeInfo == Types.SQL_TIMESTAMP) {
		return Optional.of(this::convertToTimestamp);
	} else if (simpleTypeInfo == Types.LOCAL_DATE) {
		return Optional.of(this::convertToLocalDate);
	} else if (simpleTypeInfo == Types.LOCAL_TIME) {
		return Optional.of(this::convertToLocalTime);
	} else if (simpleTypeInfo == Types.LOCAL_DATE_TIME) {
		return Optional.of(this::convertToLocalDateTime);
	} else {
		return Optional.empty();
	}
}
 
Example 5
Source File: TypeStringUtils.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private TypeInformation<?> convertType() {
	final TypeInformation<?> typeInfo;
	switch (token().literal) {
		case VARCHAR:
		case STRING:
			return Types.STRING;
		case BOOLEAN:
			return Types.BOOLEAN;
		case TINYINT:
		case BYTE:
			return Types.BYTE;
		case SMALLINT:
		case SHORT:
			return Types.SHORT;
		case INT:
			return Types.INT;
		case BIGINT:
		case LONG:
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case DECIMAL:
			return Types.BIG_DEC;
		case DATE:
		case SQL_DATE:
			return Types.SQL_DATE;
		case TIMESTAMP:
		case SQL_TIMESTAMP:
			return Types.SQL_TIMESTAMP;
		case TIME:
		case SQL_TIME:
			return Types.SQL_TIME;
		case ROW:
			return convertRow();
		case ANY:
			return convertAny();
		case POJO:
			return convertPojo();
		case MAP:
			return convertMap();
		case MULTISET:
			return convertMultiset();
		case PRIMITIVE_ARRAY:
			return convertPrimitiveArray();
		case OBJECT_ARRAY:
			return convertObjectArray();
		default:
			throw parsingError("Unsupported type: " + token().literal);
	}
}
 
Example 6
Source File: AvroSchemaConverter.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}
 
Example 7
Source File: JsonRowSerializationSchema.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private JsonNode convert(ContainerNode<?> container, JsonNode reuse, TypeInformation<?> info, Object object) {
	if (info == Types.VOID || object == null) {
		return container.nullNode();
	} else if (info == Types.BOOLEAN) {
		return container.booleanNode((Boolean) object);
	} else if (info == Types.STRING) {
		return container.textNode((String) object);
	} else if (info == Types.BIG_DEC) {
		// convert decimal if necessary
		if (object instanceof BigDecimal) {
			return container.numberNode((BigDecimal) object);
		}
		return container.numberNode(BigDecimal.valueOf(((Number) object).doubleValue()));
	} else if (info == Types.BIG_INT) {
		// convert integer if necessary
		if (object instanceof BigInteger) {
			return container.numberNode((BigInteger) object);
		}
		return container.numberNode(BigInteger.valueOf(((Number) object).longValue()));
	} else if (info == Types.SQL_DATE) {
		return container.textNode(object.toString());
	} else if (info == Types.SQL_TIME) {
		final Time time = (Time) object;
		// strip milliseconds if possible
		if (time.getTime() % 1000 > 0) {
			return container.textNode(timeFormatWithMillis.format(time));
		}
		return container.textNode(timeFormat.format(time));
	} else if (info == Types.SQL_TIMESTAMP) {
		return container.textNode(timestampFormat.format((Timestamp) object));
	} else if (info instanceof RowTypeInfo) {
		if (reuse != null && reuse instanceof ObjectNode) {
			return convertRow((ObjectNode) reuse, (RowTypeInfo) info, (Row) object);
		} else {
			return convertRow(null, (RowTypeInfo) info, (Row) object);
		}
	} else if (info instanceof ObjectArrayTypeInfo) {
		if (reuse != null && reuse instanceof ArrayNode) {
			return convertObjectArray((ArrayNode) reuse, ((ObjectArrayTypeInfo) info).getComponentInfo(), (Object[]) object);
		} else {
			return convertObjectArray(null, ((ObjectArrayTypeInfo) info).getComponentInfo(), (Object[]) object);
		}
	} else if (info instanceof BasicArrayTypeInfo) {
		if (reuse != null && reuse instanceof ArrayNode) {
			return convertObjectArray((ArrayNode) reuse, ((BasicArrayTypeInfo) info).getComponentInfo(), (Object[]) object);
		} else {
			return convertObjectArray(null, ((BasicArrayTypeInfo) info).getComponentInfo(), (Object[]) object);
		}
	} else if (info instanceof PrimitiveArrayTypeInfo && ((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return container.binaryNode((byte[]) object);
	} else {
		// for types that were specified without JSON schema
		// e.g. POJOs
		try {
			return mapper.valueToTree(object);
		} catch (IllegalArgumentException e) {
			throw new IllegalStateException("Unsupported type information '" + info + "' for object: " + object, e);
		}
	}
}
 
Example 8
Source File: JsonRowDeserializationSchema.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private Object convert(JsonNode node, TypeInformation<?> info) {
	if (info == Types.VOID || node.isNull()) {
		return null;
	} else if (info == Types.BOOLEAN) {
		return node.asBoolean();
	} else if (info == Types.STRING) {
		return node.asText();
	} else if (info == Types.BIG_DEC) {
		return node.decimalValue();
	} else if (info == Types.BIG_INT) {
		return node.bigIntegerValue();
	} else if (info == Types.SQL_DATE) {
		return Date.valueOf(node.asText());
	} else if (info == Types.SQL_TIME) {
		// according to RFC 3339 every full-time must have a timezone;
		// until we have full timezone support, we only support UTC;
		// users can parse their time as string as a workaround
		final String time = node.asText();
		if (time.indexOf('Z') < 0 || time.indexOf('.') >= 0) {
			throw new IllegalStateException(
				"Invalid time format. Only a time in UTC timezone without milliseconds is supported yet. " +
					"Format: HH:mm:ss'Z'");
		}
		return Time.valueOf(time.substring(0, time.length() - 1));
	} else if (info == Types.SQL_TIMESTAMP) {
		// according to RFC 3339 every date-time must have a timezone;
		// until we have full timezone support, we only support UTC;
		// users can parse their time as string as a workaround
		final String timestamp = node.asText();
		if (timestamp.indexOf('Z') < 0) {
			throw new IllegalStateException(
				"Invalid timestamp format. Only a timestamp in UTC timezone is supported yet. " +
					"Format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		}
		return Timestamp.valueOf(timestamp.substring(0, timestamp.length() - 1).replace('T', ' '));
	} else if (info instanceof RowTypeInfo) {
		return convertRow(node, (RowTypeInfo) info);
	} else if (info instanceof ObjectArrayTypeInfo) {
		return convertObjectArray(node, ((ObjectArrayTypeInfo) info).getComponentInfo());
	} else if (info instanceof BasicArrayTypeInfo) {
		return convertObjectArray(node, ((BasicArrayTypeInfo) info).getComponentInfo());
	} else if (info instanceof PrimitiveArrayTypeInfo &&
			((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return convertByteArray(node);
	} else {
		// for types that were specified without JSON schema
		// e.g. POJOs
		try {
			return objectMapper.treeToValue(node, info.getTypeClass());
		} catch (JsonProcessingException e) {
			throw new IllegalStateException("Unsupported type information '" + info + "' for node: " + node);
		}
	}
}
 
Example 9
Source File: TypeStringUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private TypeInformation<?> convertType() {
	final TypeInformation<?> typeInfo;
	switch (token().literal) {
		case VARCHAR:
		case STRING:
			return Types.STRING;
		case BOOLEAN:
			return Types.BOOLEAN;
		case TINYINT:
		case BYTE:
			return Types.BYTE;
		case SMALLINT:
		case SHORT:
			return Types.SHORT;
		case INT:
			return Types.INT;
		case BIGINT:
		case LONG:
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case DECIMAL:
			return Types.BIG_DEC;
		case DATE:
		case SQL_DATE:
			return Types.SQL_DATE;
		case TIMESTAMP:
		case SQL_TIMESTAMP:
			return Types.SQL_TIMESTAMP;
		case TIME:
		case SQL_TIME:
			return Types.SQL_TIME;
		case ROW:
			return convertRow();
		case ANY:
			return convertAny();
		case POJO:
			return convertPojo();
		case MAP:
			return convertMap();
		case MULTISET:
			return convertMultiset();
		case PRIMITIVE_ARRAY:
			return convertPrimitiveArray();
		case OBJECT_ARRAY:
			return convertObjectArray();
		default:
			throw parsingError("Unsupported type: " + token().literal);
	}
}
 
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;
	}

	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 11
Source File: AvroSchemaConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}
 
Example 12
Source File: TypeStringUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private TypeInformation<?> convertType() {
	final TypeInformation<?> typeInfo;
	switch (token().literal) {
		case VARCHAR:
		case STRING:
			return Types.STRING;
		case BOOLEAN:
			return Types.BOOLEAN;
		case TINYINT:
		case BYTE:
			return Types.BYTE;
		case SMALLINT:
		case SHORT:
			return Types.SHORT;
		case INT:
			return Types.INT;
		case BIGINT:
		case LONG:
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case DECIMAL:
			return Types.BIG_DEC;
		case DATE:
		case SQL_DATE:
			return Types.SQL_DATE;
		case TIMESTAMP:
		case SQL_TIMESTAMP:
			return Types.SQL_TIMESTAMP;
		case TIME:
		case SQL_TIME:
			return Types.SQL_TIME;
		case ROW:
			return convertRow();
		case ANY:
			return convertAny();
		case POJO:
			return convertPojo();
		case MAP:
			return convertMap();
		case MULTISET:
			return convertMultiset();
		case PRIMITIVE_ARRAY:
			return convertPrimitiveArray();
		case OBJECT_ARRAY:
			return convertObjectArray();
		default:
			throw parsingError("Unsupported type: " + token().literal);
	}
}
 
Example 13
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 14
Source File: AvroSchemaConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final org.apache.avro.LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}