/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link FloorDateExpression}. */ 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 { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE){ // 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())); children = newChildren; } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new FloorWeekExpression(children); case MONTH: return new FloorMonthExpression(children); case YEAR: return new FloorYearExpression(children); default: return new FloorDateExpression(children); } }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength()==0) { return true; } 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 FloorDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
public static Expression getFloorExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); //FLOOR on timestamp doesn't really care about the nanos part i.e. it just sets it to zero. //Which is exactly what FloorDateExpression does too. if(firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return FloorDateExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return FloorDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link FloorDateExpression}. */ 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); }
@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 = FloorDecimalExpression.create(rhs); } else if ((rhsType == PTimestamp.INSTANCE || rhsType == PUnsignedTimestamp.INSTANCE) && (lhsType != PTimestamp.INSTANCE && lhsType != PUnsignedTimestamp.INSTANCE)) { e = FloorDateExpression.create(rhs, TimeUnit.MILLISECOND); } e = CoerceExpression.create(e, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength()==0) { return true; } 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; } }
public static Expression create(List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE){ // 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())); children = newChildren; } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new FloorWeekExpression(children); case MONTH: return new FloorMonthExpression(children); case YEAR: return new FloorYearExpression(children); default: return new FloorDateExpression(children); } }
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link FloorDateExpression}. */ 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); }
/** * @param timeUnit - unit of time to round up to. * Creates a {@link FloorDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (children.get(0).evaluate(tuple, ptr)) { if (ptr.getLength()==0) { return true; } 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; } }
public static Expression create(List<Expression> children) throws SQLException { Expression firstChild = children.get(0); PDataType firstChildDataType = firstChild.getDataType(); if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE){ // 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())); children = newChildren; } Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new FloorWeekExpression(children); case MONTH: return new FloorMonthExpression(children); case YEAR: return new FloorYearExpression(children); default: return new FloorDateExpression(children); } }
/** * @param timeUnit - unit of time to round up to. * Creates a {@link FloorDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
@Test public void testFloorDateExpressionForWeek() throws Exception { Expression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2016-01-07 08:17:28"), PDate.INSTANCE); Expression floorDateExpression = FloorDateExpression.create(dateLiteral, TimeUnit.WEEK); 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("2016-01-04 00:00: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); }
public static Expression getFloorExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); //FLOOR on timestamp doesn't really care about the nanos part i.e. it just sets it to zero. //Which is exactly what FloorDateExpression does too. if(firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return FloorDateExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return FloorDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
public static Expression getFloorExpression(List<Expression> children) throws SQLException { final Expression firstChild = children.get(0); final PDataType firstChildDataType = firstChild.getDataType(); //FLOOR on timestamp doesn't really care about the nanos part i.e. it just sets it to zero. //Which is exactly what FloorDateExpression does too. if(firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) { return FloorDateExpression.create(children); } else if(firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) { return FloorDecimalExpression.create(children); } else { throw TypeMismatchException.newException(firstChildDataType, "1"); } }
@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 = FloorDecimalExpression.create(rhs); } else if ((rhsType == PTimestamp.INSTANCE || rhsType == PUnsignedTimestamp.INSTANCE) && (lhsType != PTimestamp.INSTANCE && lhsType != PUnsignedTimestamp.INSTANCE)) { e = FloorDateExpression.create(rhs, TimeUnit.MILLISECOND); } e = CoerceExpression.create(e, lhsType, lhs.getSortOrder(), lhs.getMaxLength(), rowKeyOrderOptimizable); return e; }