Java Code Examples for org.apache.flink.table.types.logical.TimestampType#getPrecision()

The following examples show how to use org.apache.flink.table.types.logical.TimestampType#getPrecision() . 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: HiveTypeUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypeInfo visit(TimestampType timestampType) {
	if (checkPrecision && timestampType.getPrecision() != 9) {
		throw new CatalogException("HiveCatalog currently only supports timestamp of precision 9");
	}
	return TypeInfoFactory.timestampTypeInfo;
}
 
Example 2
Source File: ArrowUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ArrowType visit(TimestampType timestampType) {
	if (timestampType.getPrecision() == 0) {
		return new ArrowType.Timestamp(TimeUnit.SECOND, null);
	} else if (timestampType.getPrecision() >= 1 && timestampType.getPrecision() <= 3) {
		return new ArrowType.Timestamp(TimeUnit.MILLISECOND, null);
	} else if (timestampType.getPrecision() >= 4 && timestampType.getPrecision() <= 6) {
		return new ArrowType.Timestamp(TimeUnit.MICROSECOND, null);
	} else {
		return new ArrowType.Timestamp(TimeUnit.NANOSECOND, null);
	}
}
 
Example 3
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Integer visit(TimestampType timestampType) {
	return timestampType.getPrecision();
}
 
Example 4
Source File: LogicalTypeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public LogicalType visit(TimestampType timestampType) {
	return new TimestampType(
		timestampType.isNullable(),
		timestampType.getPrecision());
}
 
Example 5
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 6
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Integer visit(TimestampType timestampType) {
	return timestampType.getPrecision();
}
 
Example 7
Source File: LogicalTypeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public LogicalType visit(TimestampType timestampType) {
	return new TimestampType(
		timestampType.isNullable(),
		timestampType.getPrecision());
}
 
Example 8
Source File: CatalogTableSchemaResolver.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve the computed column's type for the given schema.
 *
 * @param tableSchema Table schema to derive table field names and data types
 * @return the resolved TableSchema
 */
public TableSchema resolve(TableSchema tableSchema) {
	final String rowtime;
	if (!tableSchema.getWatermarkSpecs().isEmpty()) {
		// TODO: [FLINK-14473] we only support top-level rowtime attribute right now
		rowtime = tableSchema.getWatermarkSpecs().get(0).getRowtimeAttribute();
		if (rowtime.contains(".")) {
			throw new ValidationException(
					String.format("Nested field '%s' as rowtime attribute is not supported right now.", rowtime));
		}
	} else {
		rowtime = null;
	}

	String[] fieldNames = tableSchema.getFieldNames();
	DataType[] fieldTypes = tableSchema.getFieldDataTypes();

	TableSchema.Builder builder = TableSchema.builder();
	for (int i = 0; i < tableSchema.getFieldCount(); ++i) {
		TableColumn tableColumn = tableSchema.getTableColumns().get(i);
		DataType fieldType = fieldTypes[i];

		if (tableColumn.isGenerated()) {
			fieldType = resolveExpressionDataType(tableColumn.getExpr().get(), tableSchema);
			if (isProctime(fieldType)) {
				if (fieldNames[i].equals(rowtime)) {
					throw new TableException("Watermark can not be defined for a processing time attribute column.");
				}
			}
		}

		if (isStreamingMode && fieldNames[i].equals(rowtime)) {
			TimestampType originalType = (TimestampType) fieldType.getLogicalType();
			LogicalType rowtimeType = new TimestampType(
					originalType.isNullable(),
					TimestampKind.ROWTIME,
					originalType.getPrecision());
			fieldType = TypeConversions.fromLogicalToDataType(rowtimeType);
		}

		if (tableColumn.isGenerated()) {
			builder.field(fieldNames[i], fieldType, tableColumn.getExpr().get());
		} else {
			builder.field(fieldNames[i], fieldType);
		}
	}

	tableSchema.getWatermarkSpecs().forEach(builder::watermark);
	tableSchema.getPrimaryKey().ifPresent(
			pk -> builder.primaryKey(pk.getName(), pk.getColumns().toArray(new String[0])));
	return builder.build();
}
 
Example 9
Source File: SortCodeGeneratorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private Object[] generateValues(LogicalType type) {

		Random rnd = new Random();

		int seedNum = RECORD_NUM / 5;
		Object[] seeds = new Object[seedNum];
		seeds[0] = null;
		seeds[1] = value1(type, rnd);
		seeds[2] = value2(type, rnd);
		seeds[3] = value3(type, rnd);
		for (int i = 4; i < seeds.length; i++) {
			switch (type.getTypeRoot()) {
				case BOOLEAN:
					seeds[i] = rnd.nextBoolean();
					break;
				case TINYINT:
					seeds[i] = (byte) rnd.nextLong();
					break;
				case SMALLINT:
					seeds[i] = (short) rnd.nextLong();
					break;
				case INTEGER:
					seeds[i] = rnd.nextInt();
					break;
				case BIGINT:
					seeds[i] = rnd.nextLong();
					break;
				case FLOAT:
					seeds[i] = rnd.nextFloat() * rnd.nextLong();
					break;
				case DOUBLE:
					seeds[i] = rnd.nextDouble() * rnd.nextLong();
					break;
				case VARCHAR:
					seeds[i] = StringData.fromString(RandomStringUtils.random(rnd.nextInt(20)));
					break;
				case DECIMAL:
					DecimalType decimalType = (DecimalType) type;
					BigDecimal decimal = new BigDecimal(
							rnd.nextInt()).divide(
							new BigDecimal(ThreadLocalRandom.current().nextInt(1, 256)),
							ThreadLocalRandom.current().nextInt(1, 30), BigDecimal.ROUND_HALF_EVEN);
					seeds[i] = DecimalData.fromBigDecimal(decimal, decimalType.getPrecision(), decimalType.getScale());
					break;
				case TIMESTAMP_WITHOUT_TIME_ZONE:
					TimestampType timestampType = (TimestampType) type;
					if (timestampType.getPrecision() <= 3) {
						seeds[i] = TimestampData.fromEpochMillis(rnd.nextLong());
					} else {
						seeds[i] = TimestampData.fromEpochMillis(rnd.nextLong(), rnd.nextInt(1000000));
					}
					break;
				case ARRAY:
				case VARBINARY:
					byte[] bytes = new byte[rnd.nextInt(16) + 1];
					rnd.nextBytes(bytes);
					seeds[i] = type instanceof VarBinaryType ? bytes : BinaryArrayData.fromPrimitiveArray(bytes);
					break;
				case ROW:
					RowType rowType = (RowType) type;
					if (rowType.getFields().get(0).getType().getTypeRoot() == INTEGER) {
						seeds[i] = GenericRowData.of(rnd.nextInt());
					} else {
						seeds[i] = GenericRowData.of(GenericRowData.of(rnd.nextInt()));
					}
					break;
				case RAW:
					seeds[i] = RawValueData.fromObject(rnd.nextInt());
					break;
				default:
					throw new RuntimeException("Not support!");
			}
		}

		// result values
		Object[] results = new Object[RECORD_NUM];
		for (int i = 0; i < RECORD_NUM; i++) {
			results[i] = seeds[rnd.nextInt(seedNum)];
		}
		return results;
	}
 
Example 10
Source File: ParquetRowDataWriter.java    From flink with Apache License 2.0 4 votes vote down vote up
private FieldWriter createWriter(LogicalType t, Type type) {
	if (type.isPrimitive()) {
		switch (t.getTypeRoot()) {
			case CHAR:
			case VARCHAR:
				return new StringWriter();
			case BOOLEAN:
				return new BooleanWriter();
			case BINARY:
			case VARBINARY:
				return new BinaryWriter();
			case DECIMAL:
				DecimalType decimalType = (DecimalType) t;
				return createDecimalWriter(decimalType.getPrecision(), decimalType.getScale());
			case TINYINT:
				return new ByteWriter();
			case SMALLINT:
				return new ShortWriter();
			case DATE:
			case TIME_WITHOUT_TIME_ZONE:
			case INTEGER:
				return new IntWriter();
			case BIGINT:
				return new LongWriter();
			case FLOAT:
				return new FloatWriter();
			case DOUBLE:
				return new DoubleWriter();
			case TIMESTAMP_WITHOUT_TIME_ZONE:
				TimestampType timestampType = (TimestampType) t;
				return new TimestampWriter(timestampType.getPrecision());
			case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
				LocalZonedTimestampType localZonedTimestampType = (LocalZonedTimestampType) t;
				return new TimestampWriter(localZonedTimestampType.getPrecision());
			default:
				throw new UnsupportedOperationException("Unsupported type: " + type);
		}
	} else {
		throw new IllegalArgumentException("Unsupported  data type: " + t);
	}
}
 
Example 11
Source File: PythonTypeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeSerializer visit(TimestampType timestampType) {
	return new TimestampSerializer(timestampType.getPrecision());
}
 
Example 12
Source File: PythonTypeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeSerializer visit(TimestampType timestampType) {
	return new TimestampDataSerializer(timestampType.getPrecision());
}