/** * @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); }
/** * @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 { 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); }
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"); } }
@Test public void testHavingFuncToWhere() throws SQLException { // TODO: confirm that this is a valid optimization String query = "select count(1) from atable group by a_date having round(a_date, 'hour') > ?"; Date date = new Date(System.currentTimeMillis()); List<Object> binds = Arrays.<Object>asList(date); Expressions expressions = compileStatement(query,binds); Expression w = constantComparison(CompareOp.GREATER, RoundDateExpression.create(Arrays.asList(A_DATE,LiteralExpression.newConstant("hour"),LiteralExpression.newConstant(1))), date); assertEquals(w, expressions.whereClause); assertNull(expressions.havingClause); }
/** * 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) { } }
/** * Tests {@link RoundDateExpression} constructor for a valid value of time unit. */ @Test public void testRoundDateExpressionValidation_2() throws Exception { LiteralExpression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); LiteralExpression timeUnitLiteral = LiteralExpression.newConstant("millis", PVarchar.INSTANCE); List<Expression> childExpressions = new ArrayList<Expression>(1); childExpressions.add(dateLiteral); childExpressions.add(timeUnitLiteral); try { RoundDateExpression.create(childExpressions); fail("Only a valid time unit represented by TimeUnit enum is allowed and millis is invalid."); } catch(IllegalArgumentException e) { } }
/** * @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); }
/** * @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); }
@Test public void round() throws Exception { List<Expression> args = Lists.newArrayList(getInvertedLiteral(date(12, 11, 2001), PDate.INSTANCE), getLiteral("hour"), getLiteral(1)); evaluateAndAssertResult(RoundDateExpression.create(args), date(12, 11, 2001)); }
@Test public void testRoundDateExpression() throws Exception { LiteralExpression dateLiteral = LiteralExpression.newConstant(DateUtil.parseDate("2012-01-01 14:25:28"), PDate.INSTANCE); Expression roundDateExpression = RoundDateExpression.create(dateLiteral, TimeUnit.DAY); 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-02 00:00:00"), resultDate); }
@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); }
/** * @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); }
/** * @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 { 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); }
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); }
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"); } }
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"); } }