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

The following examples show how to use org.apache.flink.api.common.typeinfo.Types#SQL_TIMESTAMP . 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: CsvFormatterTest.java    From Alink with Apache License 2.0 6 votes vote down vote up
@Test
public void testFormatter() throws Exception {
    TypeInformation[] types = new TypeInformation[]{Types.STRING, Types.DOUBLE, Types.LONG,
        Types.BOOLEAN, Types.SQL_TIMESTAMP};

    Row row = Row.of("string", 1.0, 1L, true, new java.sql.Timestamp(System.currentTimeMillis()));
    CsvFormatter formatter = new CsvFormatter(types, ",", '"');
    CsvParser parser = new CsvParser(types, ",", '"');
    String text = formatter.format(row);
    Row parsed = parser.parse(text).f1;

    Assert.assertEquals(parsed.getArity(), row.getArity());
    for (int i = 0; i < parsed.getArity(); i++) {
        Assert.assertEquals(parsed.getField(i), row.getField(i));
    }
}
 
Example 2
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableSchema() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final TableSchema actualTableSchema = executor.getTableSchema(sessionId, "TableNumber2");

	final TableSchema expectedTableSchema = new TableSchema(
		new String[]{"IntegerField2", "StringField2", "TimestampField2"},
		new TypeInformation[]{Types.INT, Types.STRING, Types.SQL_TIMESTAMP});

	assertEquals(expectedTableSchema, actualTableSchema);
	executor.closeSession(sessionId);
}
 
Example 3
Source File: TripRecord.java    From pravega-samples with Apache License 2.0 6 votes vote down vote up
public static TypeInformation[] getTypeInformation() {
    TypeInformation<?>[] typeInfo = new TypeInformation[] {
            Types.INT,
            Types.INT,
            Types.SQL_TIMESTAMP,
            Types.SQL_TIMESTAMP,
            Types.INT,
            Types.FLOAT,
            Types.INT,
            Types.INT,

            Types.STRING,
            Types.STRING,
            Types.STRING,
            Types.STRING,
            Types.STRING,
            Types.STRING
    };
    return typeInfo;
}
 
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: 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 6
Source File: AbstractStreamSqlJob.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private static TableSchema removeTimeAttributes(TableSchema schema) {
  final TableSchema.Builder builder = TableSchema.builder();
  for (int i = 0; i < schema.getFieldCount(); i++) {
    final TypeInformation<?> type = schema.getFieldTypes()[i];
    final TypeInformation<?> convertedType;
    if (FlinkTypeFactory.isTimeIndicatorType(type)) {
      convertedType = Types.SQL_TIMESTAMP;
    } else {
      convertedType = type;
    }
    builder.field(schema.getFieldNames()[i], convertedType);
  }
  return builder.build();
}
 
Example 7
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 8
Source File: TaxiRideTableSource.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
@Override
public TableSchema getTableSchema() {
	TypeInformation<?>[] types = new TypeInformation[] {
			Types.LONG,
			Types.LONG,
			Types.LONG,
			Types.BOOLEAN,
			Types.FLOAT,
			Types.FLOAT,
			Types.FLOAT,
			Types.FLOAT,
			Types.SHORT,
			Types.SQL_TIMESTAMP
	};

	String[] names = new String[]{
			"rideId",
			"taxiId",
			"driverId",
			"isStart",
			"startLon",
			"startLat",
			"endLon",
			"endLat",
			"passengerCnt",
			"eventTime"
	};

	return new TableSchema(names, types);
}
 
Example 9
Source File: StreamSQLTestProgram.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TableSchema getTableSchema() {
	return new TableSchema(
		new String[] {"key", "rowtime", "payload"},
		new TypeInformation[] {Types.INT, Types.SQL_TIMESTAMP, Types.STRING});
}
 
Example 10
Source File: TimeUtil.java    From sylph with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<?> getResultType(Class<?>[] signature)
{
    return signature.length == 2 ? Types.STRING : Types.SQL_TIMESTAMP;
}
 
Example 11
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 12
Source File: StreamSQLTestProgram.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TableSchema getTableSchema() {
	return new TableSchema(
		new String[] {"key", "rowtime", "payload"},
		new TypeInformation[] {Types.INT, Types.SQL_TIMESTAMP, Types.STRING});
}
 
Example 13
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 14
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 15
Source File: MaxWithRetractAggFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected TypeInformation<Timestamp> getValueTypeInfo() {
	return Types.SQL_TIMESTAMP;
}
 
Example 16
Source File: MinWithRetractAggFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected TypeInformation<Timestamp> getValueTypeInfo() {
	return Types.SQL_TIMESTAMP;
}
 
Example 17
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 18
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 19
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 20
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);
		}
	}
}