@Override public int getMaxNumericPrecision() { return RelDataTypeSystem.DEFAULT.getMaxNumericPrecision(); }
@Test public void testIllegalDecimalType() { RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem(); RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem); DataType dataType = DataType.getType("decimal(40, 10)"); RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true); Assert.assertTrue(relDataType instanceof BasicSqlType); Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL); Assert.assertTrue(typeSystem.getMaxNumericPrecision() < 40); Assert.assertEquals(relDataType.getPrecision(), typeSystem.getMaxNumericPrecision()); Assert.assertEquals(relDataType.getScale(), 10); Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale()); } }
@Test public void testLegalDecimalType() { RelDataTypeSystem typeSystem = new KylinRelDataTypeSystem(); RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(typeSystem); DataType dataType = DataType.getType("decimal(30, 10)"); RelDataType relDataType = OLAPTable.createSqlType(typeFactory, dataType, true); Assert.assertTrue(relDataType instanceof BasicSqlType); Assert.assertEquals(relDataType.getSqlTypeName(), SqlTypeName.DECIMAL); Assert.assertEquals(relDataType.getPrecision(), 30); Assert.assertTrue(relDataType.getPrecision() <= typeSystem.getMaxNumericPrecision()); Assert.assertEquals(relDataType.getScale(), 10); Assert.assertTrue(relDataType.getScale() <= typeSystem.getMaxNumericScale()); }
@Override public int getMaxNumericPrecision() { return RelDataTypeSystem.DEFAULT.getMaxNumericPrecision(); }
public int getMaxNumericPrecision() { return typeSystem.getMaxNumericPrecision(); }
public int getMaxNumericPrecision() { return typeSystem.getMaxNumericPrecision(); }
public static int getMaxPrecisionForPrimitiveType(PrimitiveTypeName type) { switch(type) { case INT32: return 9; case INT64: return 18; case FIXED_LEN_BYTE_ARRAY: return DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(); default: throw new UnsupportedOperationException(String.format( "Specified PrimitiveTypeName %s cannot be used to determine max precision", type)); } }
@Override public void computeScalePrecision(int leftPrecision, int leftScale, int rightPrecision, int rightScale) { // compute the output scale and precision here outputScale = Math.max(leftScale, rightScale); int leftIntegerDigits = leftPrecision - leftScale; outputPrecision = DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(); if (outputScale + leftIntegerDigits > outputPrecision) { outputScale = outputPrecision - leftIntegerDigits; } // Output precision should at least be greater or equal to the input precision outputPrecision = Math.min(outputPrecision, Math.max(leftPrecision, rightPrecision)); } }
/** * Calculates a power of ten, as a long value */ protected long powerOfTen(int scale) { assert scale >= 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return BigInteger.TEN.pow(scale).longValue(); }
/** * Calculates a power of ten, as a long value */ protected long powerOfTen(int scale) { assert scale >= 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return BigInteger.TEN.pow(scale).longValue(); }
@Override public void add() { nonNullCount.value = 1; java.math.BigDecimal currentValue = org.apache.drill.exec.util.DecimalUtility .getBigDecimalFromDrillBuf(in.buffer, in.start, in.end - in.start, in.scale); value.obj = ((java.math.BigDecimal) value.obj).add(currentValue); if (outputScale.value == Integer.MIN_VALUE) { outputScale.value = in.scale; } org.apache.drill.exec.util.DecimalUtility.checkValueOverflow((java.math.BigDecimal) value.obj, org.apache.drill.exec.planner.types.DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(), outputScale.value); }
@Override public void add() { nonNullCount.value = 1; java.math.BigDecimal currentValue = org.apache.drill.exec.util.DecimalUtility .getBigDecimalFromDrillBuf(in.buffer, in.start, in.end - in.start, in.scale); value.obj = ((java.math.BigDecimal) value.obj).add(currentValue); if (outputScale.value == Integer.MIN_VALUE) { outputScale.value = in.scale; } org.apache.drill.exec.util.DecimalUtility.checkValueOverflow((java.math.BigDecimal) value.obj, org.apache.drill.exec.planner.types.DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(), outputScale.value); }
@Override public void output() { if (nonNullCount.value > 0) { out.isSet = 1; out.start = 0; out.scale = outputScale.value; out.precision = org.apache.drill.exec.planner.types.DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(); value.obj = ((java.math.BigDecimal) value.obj).setScale(out.scale, java.math.BigDecimal.ROUND_HALF_UP); byte[] bytes = ((java.math.BigDecimal) value.obj).unscaledValue().toByteArray(); int len = bytes.length; out.buffer = buffer.reallocIfNeeded(len); out.buffer.setBytes(0, bytes); out.end = len; } else { out.isSet = 0; } }
@Override public void add() { sout: { if (in.isSet == 0) { // processing nullable input and the value is null, so don't do anything... break sout; } nonNullCount.value = 1; java.math.BigDecimal currentValue = org.apache.drill.exec.util.DecimalUtility .getBigDecimalFromDrillBuf(in.buffer, in.start, in.end - in.start, in.scale); value.obj = ((java.math.BigDecimal) value.obj).add(currentValue); if (outputScale.value == Integer.MIN_VALUE) { outputScale.value = in.scale; } org.apache.drill.exec.util.DecimalUtility.checkValueOverflow((java.math.BigDecimal) value.obj, org.apache.drill.exec.planner.types.DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(), outputScale.value); } // end of sout block }
/** * Makes an exact numeric literal to be used for scaling * * @param scale a scale from one to max precision - 1 * @return 10^scale as an exact numeric value */ protected RexNode makeScaleFactor(int scale) { assert scale > 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return makeExactLiteral(powerOfTen(scale)); }
/** * Makes an exact numeric literal to be used for scaling * * @param scale a scale from one to max precision - 1 * @return 10^scale as an exact numeric value */ protected RexNode makeScaleFactor(int scale) { assert scale > 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return makeExactLiteral(powerOfTen(scale)); }
/** * Makes an exact numeric value to be used for rounding. * * @param scale a scale from 1 to max precision - 1 * @return 10^scale / 2 as an exact numeric value */ protected RexNode makeRoundFactor(int scale) { assert scale > 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return makeExactLiteral(powerOfTen(scale) / 2); }
/** * Makes an exact numeric value to be used for rounding. * * @param scale a scale from 1 to max precision - 1 * @return 10^scale / 2 as an exact numeric value */ protected RexNode makeRoundFactor(int scale) { assert scale > 0; assert scale < builder.getTypeFactory().getTypeSystem().getMaxNumericPrecision(); return makeExactLiteral(powerOfTen(scale) / 2); }
@Override public TypeProtos.MajorType getType(List<LogicalExpression> logicalExpressions, FunctionAttributes attributes) { int scale = 0; // Get the max scale and precision from the inputs for (LogicalExpression e : logicalExpressions) { scale = Math.max(scale, e.getMajorType().getScale()); } return TypeProtos.MajorType.newBuilder() .setMinorType(TypeProtos.MinorType.VARDECIMAL) .setScale(Math.min(Math.max(6, scale), DRILL_REL_DATATYPE_SYSTEM.getMaxNumericScale())) .setPrecision(DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision()) .setMode(TypeProtos.DataMode.OPTIONAL) .build(); } }
@Override public TypeProtos.MajorType getType(List<LogicalExpression> logicalExpressions, FunctionAttributes attributes) { int scale = 0; // Get the max scale and precision from the inputs for (LogicalExpression e : logicalExpressions) { scale = Math.max(scale, e.getMajorType().getScale()); } return TypeProtos.MajorType.newBuilder() .setMinorType(TypeProtos.MinorType.VARDECIMAL) .setScale(scale) .setPrecision(DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision()) .setMode(TypeProtos.DataMode.OPTIONAL) .build(); } }