public void evaluate(Tuple tuple) { for (int i = 0; i < keys.size(); i++) { if (!expressions.get(i).evaluate(tuple, keys.get(i))) { keys.get(i).set(EMPTY_PTR); } } }
@Override public Tuple next() throws SQLException { Tuple next; do { next = delegate.next(); } while (next != null && expression.evaluate(next, ptr) && Boolean.FALSE.equals(expression.getDataType().toObject(ptr))); return next; }
public static boolean evaluatesToTrue(Expression expression) { if (isConstant(expression)) { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); expression.evaluate(null, ptr); return Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(ptr)); } return false; }
@Override public void aggregate(Aggregator[] aggregators, Tuple result) { for (int i = 0; i < expressions.length; i++) { if (expressions[i].evaluate(result, ptr) && ptr.getLength() != 0) { aggregators[i].aggregate(result, ptr); } expressions[i].reset(); } }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { boolean wasEvaluated = super.evaluate(tuple, ptr); if (!wasEvaluated) { return false; } if (isConstantExpression()) { getAggregatorExpression().evaluate(tuple, ptr); } return true; }
@Override protected Tuple advance() throws SQLException { Tuple next; do { next = delegate.next(); expression.reset(); } while (next != null && (!expression.evaluate(next, ptr) || ptr.getLength() == 0 || !Boolean.TRUE.equals(expression.getDataType().toObject(ptr)))); return next; }
private void init() { Expression offsetExpr = children.get(1); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); if (offsetExpr.isStateless() && offsetExpr.getDeterminism() == Determinism.ALWAYS && offsetExpr.evaluate(null, ptr)) { offsetPreCompute = (Integer) PInteger.INSTANCE.toObject(ptr, offsetExpr.getSortOrder()); } else offsetPreCompute = null; }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (!getChildExpression().evaluate(tuple, ptr)) { return false; } if (ptr.getLength() == 0) { return true; } PDataType type = getDataType(); // FIXME: losing rowKeyOrderOptimizable here type.coerceBytes(ptr, type, getChildExpression().getSortOrder(), getSortOrder()); return true; }
public static Expression create(Expression child, boolean negate, ImmutableBytesWritable ptr) throws SQLException { if (!child.isNullable()) { return LiteralExpression.newConstant(negate, PBoolean.INSTANCE, child.getDeterminism()); } if (ExpressionUtil.isConstant(child)) { boolean evaluated = child.evaluate(null, ptr); return LiteralExpression.newConstant(negate ^ (!evaluated || ptr.getLength() == 0), PBoolean.INSTANCE, child.getDeterminism()); } return new IsNullExpression(child, negate); }
private void testGetByteExpression(Expression data, Expression offset, int expected) throws SQLException { List<Expression> expressions = Lists.newArrayList(data, offset); Expression getByteFunction = new GetByteFunction(expressions); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); getByteFunction.evaluate(null, ptr); Integer result = (Integer) getByteFunction.getDataType().toObject(ptr, getByteFunction.getSortOrder()); assertEquals(expected, result.intValue()); }
private String evalExp(Expression exp) { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); boolean eval = exp.evaluate(null, ptr); assertTrue(eval); String res = (String) exp.getDataType().toObject(ptr); return res; }
private void testSetByteExpression(Expression data, Expression offset, Expression newValue, byte[] expected) throws SQLException { List<Expression> expressions = Lists.newArrayList(data, offset, newValue); Expression setByteFunction = new SetByteFunction(expressions); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); setByteFunction.evaluate(null, ptr); byte[] result = (byte[]) setByteFunction.getDataType() .toObject(ptr, setByteFunction.getSortOrder()); assertArrayEquals(expected, result); }
private void testSetBitExpression(Expression data, Expression offset, Expression newValue, byte[] expected) throws SQLException { List<Expression> expressions = Lists.newArrayList(data, offset, newValue); Expression setBitFunction = new SetBitFunction(expressions); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); setBitFunction.evaluate(null, ptr); byte[] result = (byte[]) setBitFunction.getDataType().toObject(ptr, setBitFunction.getSortOrder()); assertArrayEquals(expected, result); }
private long evalExpression(CreateSequenceStatement sequence, StatementContext context, Expression expression, SQLExceptionCode code) throws SQLException { ImmutableBytesWritable ptr = context.getTempPtr(); expression.evaluate(null, ptr); if (ptr.getLength() == 0 || !expression.getDataType().isCoercibleTo(PLong.INSTANCE)) { TableName sequenceName = sequence.getSequenceName(); throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), code); } return (Long) PLong.INSTANCE.toObject(ptr, expression.getDataType()); }
public static LiteralExpression getConstantExpression(Expression expression, ImmutableBytesWritable ptr) throws SQLException { Object value = null; PDataType type = expression.getDataType(); if (expression.evaluate(null, ptr) && ptr.getLength() != 0) { value = type.toObject(ptr.get(), ptr.getOffset(), ptr.getLength(), type, expression.getSortOrder(), expression.getMaxLength(), expression.getScale()); } return LiteralExpression.newConstant(value, type, expression.getDeterminism()); }
@Test public void testFloorDecimalExpression() throws Exception { LiteralExpression decimalLiteral = LiteralExpression.newConstant(1.23898, PDecimal.INSTANCE); Expression floorDecimalExpression = FloorDecimalExpression.create(decimalLiteral, 3); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); floorDecimalExpression.evaluate(null, ptr); Object result = floorDecimalExpression.getDataType().toObject(ptr); assertTrue(result instanceof BigDecimal); BigDecimal resultDecimal = (BigDecimal)result; assertEquals(BigDecimal.valueOf(1.238), resultDecimal); }
@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); }
@Test public void testRoundDateExpressionWithMultiplier() throws Exception { Expression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression roundDateExpression = RoundDateExpression.create(dateLiteral, TimeUnit.MINUTE, 10); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); roundDateExpression.evaluate(null, ptr); Object result = roundDateExpression.getDataType().toObject(ptr); assertTrue(result instanceof Date); Date resultDate = (Date)result; assertEquals(DateUtil.parseDate("2012-01-01 14:30:00"), resultDate); }
@Test public void testFloorDateExpression() throws Exception { LiteralExpression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression floorDateExpression = FloorDateExpression.create(dateLiteral, TimeUnit.DAY); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); floorDateExpression.evaluate(null, ptr); Object result = floorDateExpression.getDataType().toObject(ptr); assertTrue(result instanceof Date); Date resultDate = (Date)result; assertEquals(DateUtil.parseDate("2012-01-01 00:00:00"), resultDate); }
@Test public void testFloorDateExpressionWithMultiplier() throws Exception { Expression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression floorDateExpression = FloorDateExpression.create(dateLiteral, TimeUnit.SECOND, 10); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); floorDateExpression.evaluate(null, ptr); Object result = floorDateExpression.getDataType().toObject(ptr); assertTrue(result instanceof Date); Date resultDate = (Date)result; assertEquals(DateUtil.parseDate("2012-01-01 14:25:20"), resultDate); }