Java Code Examples for org.apache.flink.table.types.logical.DecimalType#MAX_PRECISION

The following examples show how to use org.apache.flink.table.types.logical.DecimalType#MAX_PRECISION . 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: LogicalTypeGeneralization.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example 2
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal division operation.
 */
public static DecimalType findDivisionDecimalType(int precision1, int scale1, int precision2, int scale2) {
	// adopted from https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql
	int scale = Math.max(6, scale1 + precision2 + 1);
	int precision = precision1 - scale1 + scale2 + scale;
	if (precision > DecimalType.MAX_PRECISION) {
		scale = Math.max(6, DecimalType.MAX_PRECISION - (precision - scale));
		precision = DecimalType.MAX_PRECISION;
	}
	return new DecimalType(false, precision, scale);
}
 
Example 3
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example 4
Source File: FlinkCalciteSqlValidator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void validateLiteral(SqlLiteral literal) {
	if (literal.getTypeName() == DECIMAL) {
		final BigDecimal decimal = literal.getValueAs(BigDecimal.class);
		if (decimal.precision() > DecimalType.MAX_PRECISION) {
			throw newValidationError(
				literal,
				Static.RESOURCE.numberLiteralOutOfRange(decimal.toString()));
		}
	}
	super.validateLiteral(literal);
}