private double sumSquaredDeviation() { double m = mean(); double result = 0.0; for (Entry<ImmutableBytesPtr, Integer> entry : valueVsCount.entrySet()) { double colValue = (Double) PDouble.INSTANCE.toObject(entry.getKey(), this.stdDevColExp.getDataType()); double delta = colValue - m; result += (delta * delta) * entry.getValue(); } return result; }
private void init() { PDataType returnType = getChildren().get(0).getDataType(); inputCodec = DateUtil.getCodecFor(returnType); } }
/** * 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 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; }
@Override public String getColumnTypeName(int column) throws SQLException { PDataType type = rowProjector.getColumnProjector(column-1).getExpression().getDataType(); return type == null ? null : type.getSqlTypeName(); }
@Override public Expression wrap(Expression lhs, Expression rhs, boolean rowKeyOrderOptimizable) throws SQLException { PDataType lhsType = lhs.getDataType(); Expression e = CoerceExpression.create(rhs, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
@Override public Aggregator newServerAggregator(Configuration conf) { Expression child = getAggregatorExpression(); return newAggregator(child.getDataType(), child.getSortOrder(), null); }
public TargetDataExpression(Expression expr) { this.type = expr.getDataType(); this.maxLength = expr.getMaxLength(); this.scale = expr.getScale(); this.sortOrder = expr.getSortOrder(); }
@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; }
@Override public Array getArray(int columnIndex) throws SQLException { checkCursorState(); // Get the value using the expected type instead of trying to coerce to VARCHAR. // We can't coerce using our formatter because we don't have enough context in PDataType. ColumnProjector projector = rowProjector.getColumnProjector(columnIndex-1); Array value = (Array)projector.getValue(currentRow, projector.getExpression().getDataType(), ptr); wasNull = (value == null); return value; }
@Override public DistinctValueWithCountClientAggregator newClientAggregator() { if (children.get(0).getDataType() == PDecimal.INSTANCE) { // Special Aggregators for DECIMAL datatype for more precision than double return new DecimalStddevPopAggregator(children, getAggregatorExpression().getSortOrder()); } return new StddevPopAggregator(children, getAggregatorExpression().getSortOrder()); }
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()); }
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); }
@Override public SortOrder getSortOrder() { if (getLHSExpr().getDataType().isArrayType()) { return isNullArray ? getRHSExpr().getSortOrder() : getLHSExpr().getSortOrder(); } else { return isNullArray ? getLHSExpr().getSortOrder() : getRHSExpr().getSortOrder(); } }
@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 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); }