/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link CeilDateExpression}. */ 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 CeilDateExpression.create(expressions); }
public static Expression create(List<Expression> children) throws SQLException { Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new CeilWeekExpression(children); case MONTH: return new CeilMonthExpression(children); case YEAR: return new CeilYearExpression(children); default: return new CeilDateExpression(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 CeilDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
@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(); if(TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) { return new CeilTimestampExpression(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 CeilDateExpression.create(newChildren); }
@Override protected PDataCodec getKeyRangeCodec(PDataType columnDataType) { return columnDataType == PTimestamp.INSTANCE ? PDate.INSTANCE.getCodec() : columnDataType == PUnsignedTimestamp.INSTANCE ? PUnsignedDate.INSTANCE.getCodec() : super.getKeyRangeCodec(columnDataType); }
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link CeilDateExpression}. */ 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 CeilDateExpression.create(expressions); }
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"); } }
@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 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 { Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new CeilWeekExpression(children); case MONTH: return new CeilMonthExpression(children); case YEAR: return new CeilYearExpression(children); default: return new CeilDateExpression(children); } }
/** * @param timeUnit - unit of time to round up to * @param multiplier - determines the roll up window size. * Create a {@link CeilDateExpression}. */ 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 CeilDateExpression.create(expressions); }
/** * @param timeUnit - unit of time to round up to. * Creates a {@link CeilDateExpression} 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; // 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; }
public static Expression create(List<Expression> children) throws SQLException { Object timeUnitValue = ((LiteralExpression)children.get(1)).getValue(); TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null); switch(timeUnit) { case WEEK: return new CeilWeekExpression(children); case MONTH: return new CeilMonthExpression(children); case YEAR: return new CeilYearExpression(children); default: return new CeilDateExpression(children); } }
/** * @param timeUnit - unit of time to round up to. * Creates a {@link CeilDateExpression} with default multiplier of 1. */ public static Expression create(Expression expr, TimeUnit timeUnit) throws SQLException { return create(expr, timeUnit, 1); }
@Test public void testCeilDateExpressionWithMultiplier() throws Exception { Expression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression ceilDateExpression = CeilDateExpression.create(dateLiteral, TimeUnit.SECOND, 10); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); ceilDateExpression.evaluate(null, ptr); Object result = ceilDateExpression.getDataType().toObject(ptr); assertTrue(result instanceof Date); Date resultDate = (Date)result; assertEquals(DateUtil.parseDate("2012-01-01 14:25:30"), resultDate); }
@Test public void testCeilDateExpression() throws Exception { LiteralExpression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression ceilDateExpression = CeilDateExpression.create(dateLiteral, TimeUnit.DAY); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); ceilDateExpression.evaluate(null, ptr); Object result = ceilDateExpression.getDataType().toObject(ptr); assertTrue(result instanceof Date); Date resultDate = (Date)result; assertEquals(DateUtil.parseDate("2012-01-02 00:00:00"), resultDate); }
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(); if(TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) { return new CeilTimestampExpression(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 CeilDateExpression.create(newChildren); }