/** * Creates a {@link RoundDecimalExpression} with a default scale of 0 used for rounding. * */ public static Expression create(Expression expr) throws SQLException { return create(expr, 0); }
private static Expression convertToRoundExpressionIfNeeded(PDataType fromDataType, PDataType targetDataType, List<Expression> expressions) throws SQLException { Expression firstChildExpr = expressions.get(0); if(fromDataType == targetDataType) { return firstChildExpr; } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PLong.INSTANCE)) { return RoundDecimalExpression.create(expressions); } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PDate.INSTANCE)) { return RoundTimestampExpression.create(expressions); } else if(fromDataType.isCastableTo(targetDataType)) { return firstChildExpr; } else { throw TypeMismatchException.newException(fromDataType, targetDataType, firstChildExpr.toString()); } }
public static Expression getRoundExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); if(firstChildDataType.isCoercibleTo(PDate.INSTANCE)) { return RoundDateExpression.create(children); } else if (firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return RoundTimestampExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return RoundDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
@Test public void testRoundDecimalExpressionScaleParamValidation() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(1.23898, PDecimal.INSTANCE); LiteralExpression scale = LiteralExpression.newConstant("3", PVarchar.INSTANCE); List<Expression> childExpressions = new ArrayList<Expression>(2); childExpressions.add(decimalLiteral); childExpressions.add(scale); try { RoundDecimalExpression.create(childExpressions); fail("Evaluation should have failed because only an INTEGER is allowed for second param in a RoundDecimalExpression"); } catch(IllegalDataException e) { } }
@Test public void testRoundDecimalExpressionNoop() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(5, PInteger.INSTANCE); Expression roundDecimalExpression = RoundDecimalExpression.create(decimalLiteral, 3); assertEquals(roundDecimalExpression, decimalLiteral); }
@Test public void testRoundDecimalExpressionKeyRangeCoverage() throws Exception { KeyPart baseKeyPart = getDecimalKeyPart(); for(int scale : SCALES) { ScalarFunction roundDecimalExpression = (ScalarFunction) RoundDecimalExpression.create(DUMMY_DECIMAL, scale); KeyPart keyPart = roundDecimalExpression.newKeyPart(baseKeyPart); verifyKeyPart(RoundingType.ROUND, scale, keyPart); } }
/** * Creates a {@link RoundDecimalExpression} with a default scale of 0 used for rounding. * */ public static Expression create(Expression expr) throws SQLException { return create(expr, 0); }
/** * Creates a {@link RoundDecimalExpression} with a default scale of 0 used for rounding. * */ public static Expression create(Expression expr) throws SQLException { return create(expr, 0); }
/** * Returns a rounding expression of this type that will round the given decimal key at the * given scale. * @param key the byte key for the Decimal to round * @param scale the scale to round the decimal to * @return the expression containing the above parameters */ public Expression getExpression(byte[] key, int scale) throws SQLException { LiteralExpression decimalLiteral = LiteralExpression.newConstant(PDecimal.INSTANCE.toObject(key), PDecimal.INSTANCE); switch(this) { case ROUND: return RoundDecimalExpression.create(decimalLiteral, scale); case FLOOR: return FloorDecimalExpression.create(decimalLiteral, scale); case CEIL: return CeilDecimalExpression.create(decimalLiteral, scale); default: throw new AssertionError("Unknown RoundingType"); } } }
@Test public void testRoundNegativePrecisionDecimalExpression() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(444.44, PDecimal.INSTANCE); Expression roundDecimalExpression = RoundDecimalExpression.create(decimalLiteral, -2); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); roundDecimalExpression.evaluate(null, ptr); Object result = roundDecimalExpression.getDataType().toObject(ptr); assertTrue(result instanceof BigDecimal); BigDecimal resultDecimal = (BigDecimal)result; assertEquals(0, BigDecimal.valueOf(400).compareTo(resultDecimal)); }
@Test public void testRoundDecimalExpressionKeyRangeSimple() throws Exception { KeyPart baseKeyPart = getDecimalKeyPart(); ScalarFunction roundDecimalExpression = (ScalarFunction)RoundDecimalExpression.create(DUMMY_DECIMAL, 3); byte[] upperBound = PDecimal.INSTANCE.toBytes(new BigDecimal("1.2385")); byte[] lowerBound = PDecimal.INSTANCE.toBytes(new BigDecimal("1.2375")); KeyRange expectedKeyRange = KeyRange.getKeyRange(lowerBound, upperBound); KeyPart keyPart = roundDecimalExpression.newKeyPart(baseKeyPart); assertEquals(expectedKeyRange, keyPart.getKeyRange(CompareOp.EQUAL, LiteralExpression.newConstant(new BigDecimal("1.238"), PDecimal.INSTANCE))); }
@Test public void testRoundDecimalExpression() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(1.23898, PDecimal.INSTANCE); Expression roundDecimalExpression = RoundDecimalExpression.create(decimalLiteral, 3); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); roundDecimalExpression.evaluate(null, ptr); Object result = roundDecimalExpression.getDataType().toObject(ptr); assertTrue(result instanceof BigDecimal); BigDecimal resultDecimal = (BigDecimal)result; assertEquals(BigDecimal.valueOf(1.239), resultDecimal); }
private static Expression convertToRoundExpressionIfNeeded(PDataType fromDataType, PDataType targetDataType, List<Expression> expressions) throws SQLException { Expression firstChildExpr = expressions.get(0); if(fromDataType == targetDataType) { return firstChildExpr; } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PLong.INSTANCE)) { return RoundDecimalExpression.create(expressions); } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PDate.INSTANCE)) { return RoundTimestampExpression.create(expressions); } else if(fromDataType.isCastableTo(targetDataType)) { return firstChildExpr; } else { throw TypeMismatchException.newException(fromDataType, targetDataType, firstChildExpr.toString()); } }
private static Expression convertToRoundExpressionIfNeeded(PDataType fromDataType, PDataType targetDataType, List<Expression> expressions) throws SQLException { Expression firstChildExpr = expressions.get(0); if(fromDataType == targetDataType) { return firstChildExpr; } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PLong.INSTANCE)) { return RoundDecimalExpression.create(expressions); } else if((fromDataType == PDecimal.INSTANCE || fromDataType == PTimestamp.INSTANCE || fromDataType == PUnsignedTimestamp.INSTANCE) && targetDataType.isCoercibleTo( PDate.INSTANCE)) { return RoundTimestampExpression.create(expressions); } else if(fromDataType.isCastableTo(targetDataType)) { return firstChildExpr; } else { throw TypeMismatchException.newException(fromDataType, targetDataType, firstChildExpr.toString()); } }
public static Expression getRoundExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); if(firstChildDataType.isCoercibleTo(PDate.INSTANCE)) { return RoundDateExpression.create(children); } else if (firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return RoundTimestampExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return RoundDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
public static Expression getRoundExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); if(firstChildDataType.isCoercibleTo(PDate.INSTANCE)) { return RoundDateExpression.create(children); } else if (firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return RoundTimestampExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return RoundDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }