Java Code Examples for org.apache.flink.table.api.DataTypes#INTERVAL

The following examples show how to use org.apache.flink.table.api.DataTypes#INTERVAL . 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: ValueDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static DataType convertToYearMonthIntervalType(int years) {
	return DataTypes.INTERVAL(DataTypes.YEAR(yearPrecision(years)), DataTypes.MONTH());
}
 
Example 2
Source File: ValueDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static DataType convertToDayTimeIntervalType(long days, int nanos) {
	return DataTypes.INTERVAL(
		DataTypes.DAY(dayPrecision(days)),
		DataTypes.SECOND(fractionalSecondPrecision(nanos)));
}
 
Example 3
Source File: StrategyUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a data type for the given data type and expected root.
 *
 * <p>This method is aligned with {@link LogicalTypeCasts#supportsImplicitCast(LogicalType, LogicalType)}.
 *
 * <p>The "fallback" data type for each root represents the default data type for a NULL literal. NULL
 * literals will receive the smallest precision possible for having little impact when finding a common
 * type. The output of this method needs to be checked again if an implicit cast is supported.
 */
private static @Nullable DataType findDataTypeOfRoot(
		DataType actualDataType,
		LogicalTypeRoot expectedRoot) {
	final LogicalType actualType = actualDataType.getLogicalType();
	if (hasRoot(actualType, expectedRoot)) {
		return actualDataType;
	}
	switch (expectedRoot) {
		case CHAR:
			return DataTypes.CHAR(CharType.DEFAULT_LENGTH);
		case VARCHAR:
			if (hasRoot(actualType, CHAR)) {
				return DataTypes.VARCHAR(getLength(actualType));
			}
			return DataTypes.VARCHAR(VarCharType.DEFAULT_LENGTH);
		case BOOLEAN:
			return DataTypes.BOOLEAN();
		case BINARY:
			return DataTypes.BINARY(BinaryType.DEFAULT_LENGTH);
		case VARBINARY:
			if (hasRoot(actualType, BINARY)) {
				return DataTypes.VARBINARY(getLength(actualType));
			}
			return DataTypes.VARBINARY(VarBinaryType.DEFAULT_LENGTH);
		case DECIMAL:
			if (hasFamily(actualType, EXACT_NUMERIC)) {
				return DataTypes.DECIMAL(getPrecision(actualType), getScale(actualType));
			} else if (hasFamily(actualType, APPROXIMATE_NUMERIC)) {
				final int precision = getPrecision(actualType);
				// we don't know where the precision occurs (before or after the dot)
				return DataTypes.DECIMAL(precision * 2, precision);
			}
			return DataTypes.DECIMAL(DecimalType.MIN_PRECISION, DecimalType.MIN_SCALE);
		case TINYINT:
			return DataTypes.TINYINT();
		case SMALLINT:
			return DataTypes.SMALLINT();
		case INTEGER:
			return DataTypes.INT();
		case BIGINT:
			return DataTypes.BIGINT();
		case FLOAT:
			return DataTypes.FLOAT();
		case DOUBLE:
			return DataTypes.DOUBLE();
		case DATE:
			return DataTypes.DATE();
		case TIME_WITHOUT_TIME_ZONE:
			if (hasRoot(actualType, TIMESTAMP_WITHOUT_TIME_ZONE)) {
				return DataTypes.TIME(getPrecision(actualType));
			}
			return DataTypes.TIME();
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return DataTypes.TIMESTAMP();
		case TIMESTAMP_WITH_TIME_ZONE:
			return DataTypes.TIMESTAMP_WITH_TIME_ZONE();
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			return DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE();
		case INTERVAL_YEAR_MONTH:
			return DataTypes.INTERVAL(DataTypes.MONTH());
		case INTERVAL_DAY_TIME:
			return DataTypes.INTERVAL(DataTypes.SECOND());
		case NULL:
			return DataTypes.NULL();
		case ARRAY:
		case MULTISET:
		case MAP:
		case ROW:
		case DISTINCT_TYPE:
		case STRUCTURED_TYPE:
		case RAW:
		case SYMBOL:
		case UNRESOLVED:
		default:
			return null;
	}
}
 
Example 4
Source File: DataTypeExtractor.java    From flink with Apache License 2.0 4 votes vote down vote up
private @Nullable DataType extractPredefinedType(DataTypeTemplate template, Type type) {
	final Class<?> clazz = toClass(type);
	// all predefined types are representable as classes
	if (clazz == null) {
		return null;
	}

	// DECIMAL
	if (clazz == BigDecimal.class) {
		if (template.defaultDecimalPrecision != null && template.defaultDecimalScale != null) {
			return DataTypes.DECIMAL(template.defaultDecimalPrecision, template.defaultDecimalScale);
		} else if (template.defaultDecimalPrecision != null) {
			return DataTypes.DECIMAL(template.defaultDecimalPrecision, 0);
		}
		throw extractionError("Values of '%s' need fixed precision and scale.", BigDecimal.class.getName());
	}

	// TIME
	else if (clazz == java.sql.Time.class || clazz == java.time.LocalTime.class) {
		if (template.defaultSecondPrecision != null) {
			return DataTypes.TIME(template.defaultSecondPrecision)
				.bridgedTo(clazz);
		}
	}

	// TIMESTAMP
	else if (clazz == java.sql.Timestamp.class || clazz == java.time.LocalDateTime.class) {
		if (template.defaultSecondPrecision != null) {
			return DataTypes.TIMESTAMP(template.defaultSecondPrecision)
				.bridgedTo(clazz);
		}
	}

	// TIMESTAMP WITH TIME ZONE
	else if (clazz == java.time.OffsetDateTime.class) {
		if (template.defaultSecondPrecision != null) {
			return DataTypes.TIMESTAMP_WITH_TIME_ZONE(template.defaultSecondPrecision);
		}
	}

	// TIMESTAMP WITH LOCAL TIME ZONE
	else if (clazz == java.time.Instant.class) {
		if (template.defaultSecondPrecision != null) {
			return DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE(template.defaultSecondPrecision);
		}
	}

	// INTERVAL SECOND
	else if (clazz == java.time.Duration.class) {
		if (template.defaultSecondPrecision != null) {
			return DataTypes.INTERVAL(DataTypes.SECOND(template.defaultSecondPrecision));
		}
	}

	// INTERVAL YEAR TO MONTH
	else if (clazz == java.time.Period.class) {
		if (template.defaultYearPrecision != null && template.defaultYearPrecision == 0) {
			return DataTypes.INTERVAL(DataTypes.MONTH());
		} else if (template.defaultYearPrecision != null) {
			return DataTypes.INTERVAL(DataTypes.YEAR(template.defaultYearPrecision), DataTypes.MONTH());
		}
	}

	return ClassDataTypeConverter.extractDataType(clazz).orElse(null);
}
 
Example 5
Source File: ValueDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static DataType convertToYearMonthIntervalType(int years) {
	return DataTypes.INTERVAL(DataTypes.YEAR(yearPrecision(years)), DataTypes.MONTH());
}
 
Example 6
Source File: ValueDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static DataType convertToDayTimeIntervalType(long days, int nanos) {
	return DataTypes.INTERVAL(
		DataTypes.DAY(dayPrecision(days)),
		DataTypes.SECOND(fractionalSecondPrecision(nanos)));
}