/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link RoundDateExpression}. */ public static Expression create(Expression expr, TimeUnit timeUnit, int multiplier) throws SQLException { Expression timeUnitExpr = getTimeUnitExpr(timeUnit); Expression defaultMultiplierExpr = getMultiplierExpr(multiplier); List<Expression> expressions = Lists.newArrayList(expr, timeUnitExpr, defaultMultiplierExpr); return create(expressions); }
public static Expression create(List<Expression> children) throws SQLException { int numChildren = children.size(); if(numChildren < 2 || numChildren > 3) { throw new IllegalArgumentException("Wrong number of arguments : " + numChildren); } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new RoundWeekExpression(children); case MONTH: return new RoundMonthExpression(children); case YEAR: return new RoundYearExpression(children); default: return new RoundDateExpression(children); } }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength() == 0) { return true; // child evaluated to null } PDataType dataType = getDataType(); long time = dataType.getCodec().decodeLong(ptr, children.get(0).getSortOrder()); long value = roundTime(time); Date d = new Date(value); byte[] byteValue = dataType.toBytes(d); ptr.set(byteValue); return true; } return false; }
/** * @param timeUnit - unit of time to round up to. * Creates a {@link RoundDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
protected long roundTime(long time) { long value; long roundUpAmount = getRoundUpAmount(); if (time <= Long.MAX_VALUE - roundUpAmount) { // If no overflow, add value = (time + roundUpAmount) / divBy; } else { // Else subtract and add one value = (time - roundUpAmount) / divBy + 1; } return value * divBy; }
@Override protected PDataCodec getKeyRangeCodec(PDataType columnDataType) { return columnDataType == PTimestamp.INSTANCE ? PDate.INSTANCE.getCodec() : columnDataType == PUnsignedTimestamp.INSTANCE ? PUnsignedDate.INSTANCE.getCodec() : super.getKeyRangeCodec(columnDataType); }
public static Expression create (List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); String timeUnit = (String)((LiteralExpression)children.get(1)).getValue(); LiteralExpression multiplierExpr = (LiteralExpression)children.get(2); /* * When rounding off timestamp to milliseconds, nanos play a part only when the multiplier value * is equal to 1. This is because for cases when multiplier value is greater than 1, number of nanos/multiplier * will always be less than half the nanos in a millisecond. */ if((timeUnit == null || TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) && ((Number)multiplierExpr.getValue()).intValue() == 1) { return new RoundTimestampExpression(children); } // Coerce TIMESTAMP to DATE, as the nanos has no affect List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size()); newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE)); newChildren.addAll(children.subList(1, children.size())); return RoundDateExpression.create(newChildren); }
@Override public int hashCode() { final int prime = 31; int result = 1; long roundUpAmount = this.getRoundUpAmount(); result = prime * result + (int)(divBy ^ (divBy >>> 32)); result = prime * result + (int)(roundUpAmount ^ (roundUpAmount >>> 32)); result = prime * result + children.get(0).hashCode(); return result; }
PDataCodec codec = getKeyRangeCodec(type); int offset = ByteUtil.isInclusive(op) ? 1 : 0; long value = codec.decodeLong(key, 0, SortOrder.getDefault());
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link RoundDateExpression}. */ public static Expression create(Expression expr, TimeUnit timeUnit, int multiplier) throws SQLException { Expression timeUnitExpr = getTimeUnitExpr(timeUnit); Expression defaultMultiplierExpr = getMultiplierExpr(multiplier); List<Expression> expressions = Lists.newArrayList(expr, timeUnitExpr, defaultMultiplierExpr); return create(expressions); }
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"); } }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength() == 0) { return true; // child evaluated to null } PDataType dataType = getDataType(); long time = dataType.getCodec().decodeLong(ptr, children.get(0).getSortOrder()); long value = roundTime(time); Date d = new Date(value); byte[] byteValue = dataType.toBytes(d); ptr.set(byteValue); return true; } return false; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; RoundDateExpression other = (RoundDateExpression)obj; if (divBy != other.divBy) return false; if (getRoundUpAmount() != other.getRoundUpAmount()) return false; return children.get(0).equals(other.children.get(0)); }
@Override protected PDataCodec getKeyRangeCodec(PDataType columnDataType) { return columnDataType == PTimestamp.INSTANCE ? PDate.INSTANCE.getCodec() : columnDataType == PUnsignedTimestamp.INSTANCE ? PUnsignedDate.INSTANCE.getCodec() : super.getKeyRangeCodec(columnDataType); }
public static Expression create(List<Expression> children) throws SQLException { int numChildren = children.size(); if(numChildren < 2 || numChildren > 3) { throw new IllegalArgumentException("Wrong number of arguments : " + numChildren); } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new RoundWeekExpression(children); case MONTH: return new RoundMonthExpression(children); case YEAR: return new RoundYearExpression(children); default: return new RoundDateExpression(children); } }
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link RoundDateExpression}. */ public static Expression create(Expression expr, TimeUnit timeUnit, int multiplier) throws SQLException { Expression timeUnitExpr = getTimeUnitExpr(timeUnit); Expression defaultMultiplierExpr = getMultiplierExpr(multiplier); List<Expression> expressions = Lists.newArrayList(expr, timeUnitExpr, defaultMultiplierExpr); return create(expressions); }
/** * Tests {@link RoundDateExpression} constructor check which only allows number of arguments between 2 and 3. */ @Test public void testRoundDateExpressionValidation_1() throws Exception { LiteralExpression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); List<Expression> childExpressions = new ArrayList<Expression>(1); childExpressions.add(dateLiteral); try { RoundDateExpression.create(childExpressions); fail("Instantiating a RoundDateExpression with only one argument should have failed."); } catch(IllegalArgumentException e) { } }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength() == 0) { return true; // child evaluated to null } PDataType dataType = getDataType(); long time = dataType.getCodec().decodeLong(ptr, children.get(0).getSortOrder()); long value = roundTime(time); Date d = new Date(value); byte[] byteValue = dataType.toBytes(d); ptr.set(byteValue); return true; } return false; }
protected long roundTime(long time) { long value; long roundUpAmount = getRoundUpAmount(); if (time <= Long.MAX_VALUE - roundUpAmount) { // If no overflow, add value = (time + roundUpAmount) / divBy; } else { // Else subtract and add one value = (time - roundUpAmount) / divBy + 1; } return value * divBy; }
@Override protected PDataCodec getKeyRangeCodec(PDataType columnDataType) { return columnDataType == PTimestamp.INSTANCE ? PDate.INSTANCE.getCodec() : columnDataType == PUnsignedTimestamp.INSTANCE ? PUnsignedDate.INSTANCE.getCodec() : super.getKeyRangeCodec(columnDataType); }