/** * Creates a {@link CeilDecimalExpression} with a default scale of 0 used for rounding. * */ public static Expression create(Expression expr) throws SQLException { return create(expr, 0); }
public static Expression create(List<Expression> exprs) throws SQLException { Expression expr = exprs.get(0); if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } if (exprs.size() == 1) { Expression scaleExpr = LiteralExpression.newConstant(0, PInteger.INSTANCE, Determinism.ALWAYS); exprs = Lists.newArrayList(expr, scaleExpr); } return new CeilDecimalExpression(exprs); }
/** * {@inheritDoc } */ @Override protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(stepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(result); return KeyRange.getKeyRange(lowerRange, false, upperRange, true); }
public static Expression getCeilExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); if(firstChildDataType.isCoercibleTo(PDate.INSTANCE)) { return CeilDateExpression.create(children); } else if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE) { return CeilTimestampExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return CeilDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
/** * {@inheritDoc } */ @Override protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(stepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(result); return KeyRange.getKeyRange(lowerRange, false, upperRange, true); }
/** * Creates a {@link CeilDecimalExpression} with rounding scale given by @param scale. * */ public static Expression create(Expression expr, int scale) throws SQLException { if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } Expression scaleExpr = LiteralExpression.newConstant(scale, PInteger.INSTANCE, Determinism.ALWAYS); List<Expression> expressions = Lists.newArrayList(expr, scaleExpr); return new CeilDecimalExpression(expressions); }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { Expression e = rhs; PDataType rhsType = rhs.getDataType(); PDataType lhsType = lhs.getDataType(); if (rhsType == PDecimal.INSTANCE && lhsType != PDecimal.INSTANCE) { e = CeilDecimalExpression.create(rhs); } else if ((rhsType == PTimestamp.INSTANCE || rhsType == PUnsignedTimestamp.INSTANCE) && (lhsType != PTimestamp.INSTANCE && lhsType != PUnsignedTimestamp.INSTANCE)) { e = CeilTimestampExpression.create(rhs); } e = CoerceExpression.create(e, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
/** * {@inheritDoc } */ @Override protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(stepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(result); return KeyRange.getKeyRange(lowerRange, false, upperRange, true); }
public static Expression create(List<Expression> exprs) throws SQLException { Expression expr = exprs.get(0); if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } if (exprs.size() == 1) { Expression scaleExpr = LiteralExpression.newConstant(0, PInteger.INSTANCE, Determinism.ALWAYS); exprs = Lists.newArrayList(expr, scaleExpr); } return new CeilDecimalExpression(exprs); }
@Test public void testCeilDecimalExpressionNoop() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(5, PInteger.INSTANCE); Expression ceilDecimalExpression = CeilDecimalExpression.create(decimalLiteral, 3); assertEquals(ceilDecimalExpression, decimalLiteral); }
public static Expression create(List<Expression> exprs) throws SQLException { Expression expr = exprs.get(0); if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } if (exprs.size() == 1) { Expression scaleExpr = LiteralExpression.newConstant(0, PInteger.INSTANCE, Determinism.ALWAYS); exprs = Lists.newArrayList(expr, scaleExpr); } return new CeilDecimalExpression(exprs); }
@Test public void testCeilDecimalExpressionKeyRangeCoverage() throws Exception { KeyPart baseKeyPart = getDecimalKeyPart(); for(int scale : SCALES) { ScalarFunction ceilDecimalExpression = (ScalarFunction) CeilDecimalExpression.create(DUMMY_DECIMAL, scale); KeyPart keyPart = ceilDecimalExpression.newKeyPart(baseKeyPart); verifyKeyPart(RoundingType.CEIL, scale, keyPart); } }
/** * Creates a {@link CeilDecimalExpression} with rounding scale given by @param scale. * */ public static Expression create(Expression expr, int scale) throws SQLException { if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } Expression scaleExpr = LiteralExpression.newConstant(scale, PInteger.INSTANCE, Determinism.ALWAYS); List<Expression> expressions = Lists.newArrayList(expr, scaleExpr); return new CeilDecimalExpression(expressions); }
@Test public void testCeilDecimalExpressionKeyRangeSimple() throws Exception { KeyPart baseKeyPart = getDecimalKeyPart(); ScalarFunction ceilDecimalExpression = (ScalarFunction)CeilDecimalExpression.create(DUMMY_DECIMAL, 3); byte[] upperBound = PDecimal.INSTANCE.toBytes(new BigDecimal("1.238")); byte[] lowerBound = PDecimal.INSTANCE.toBytes(new BigDecimal("1.237")); KeyRange expectedKeyRange = KeyRange.getKeyRange(lowerBound, false, upperBound, true); KeyPart keyPart = ceilDecimalExpression.newKeyPart(baseKeyPart); assertEquals(expectedKeyRange, keyPart.getKeyRange(CompareOp.EQUAL, LiteralExpression.newConstant(new BigDecimal("1.238"), PDecimal.INSTANCE))); }
/** * Creates a {@link CeilDecimalExpression} with rounding scale given by @param scale. * */ public static Expression create(Expression expr, int scale) throws SQLException { if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) { return expr; } Expression scaleExpr = LiteralExpression.newConstant(scale, PInteger.INSTANCE, Determinism.ALWAYS); List<Expression> expressions = Lists.newArrayList(expr, scaleExpr); return new CeilDecimalExpression(expressions); }
/** * Creates a {@link CeilDecimalExpression} 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"); } } }
/** * Creates a {@link CeilDecimalExpression} with a default scale of 0 used for rounding. * */ public static Expression create(Expression expr) throws SQLException { return create(expr, 0); }
@Test public void testCeilDecimalExpression() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(1.23898, PDecimal.INSTANCE); Expression ceilDecimalExpression = CeilDecimalExpression.create(decimalLiteral, 3); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); ceilDecimalExpression.evaluate(null, ptr); Object result = ceilDecimalExpression.getDataType().toObject(ptr); assertTrue(result instanceof BigDecimal); BigDecimal resultDecimal = (BigDecimal)result; assertEquals(BigDecimal.valueOf(1.239), resultDecimal); }
public static Expression getCeilExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); if(firstChildDataType.isCoercibleTo(PDate.INSTANCE)) { return CeilDateExpression.create(children); } else if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE) { return CeilTimestampExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return CeilDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }