protected CountAggregateFunction getDelegateFunction(List<Expression> children, StatementContext context) { CountAggregateFunction countFunc = null; if (getChildren().get(0).isStateless()) { countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); } return countFunc; }
@Test public void testAggFuncInHaving() throws SQLException { String query = "select count(1) from atable group by a_string having count(a_string) >= 1"; List<Object> binds = Collections.emptyList(); Expressions expressions = compileStatement(query,binds); Expression h = constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(Arrays.asList(A_STRING)),1L); assertNull(expressions.whereClause); assertEquals(h, expressions.havingClause); }
@Override public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException { SumAggregateFunction sumFunc; CountAggregateFunction countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); if (!countFunc.isConstantExpression()) { sumFunc = (SumAggregateFunction)context.getExpressionManager().addIfAbsent(new SumAggregateFunction(countFunc.getChildren(),null)); } else { sumFunc = null; } return new AvgAggregateFunction(children, countFunc, sumFunc); } }
@Test public void testAndHavingToWhere() throws SQLException { String query = "select count(1) from atable group by a_string having count(1) >= 1 and a_string = 'foo'"; List<Object> binds = Collections.emptyList(); Expressions expressions = compileStatement(query,binds); Expression h = constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(),1L); Expression w = constantComparison(CompareOp.EQUAL, A_STRING,"foo"); assertEquals(w, expressions.whereClause); assertEquals(h, expressions.havingClause); }
@Test public void testAndHavingToAndWhere() throws SQLException { String query = "select count(1) from atable where b_string > 'bar' group by a_string having count(1) >= 1 and a_string = 'foo'"; List<Object> binds = Collections.emptyList(); Expressions expressions = compileStatement(query,binds); Expression h = constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(),1L); Expression w = and(constantComparison(CompareOp.GREATER, B_STRING,"bar"),constantComparison(CompareOp.EQUAL, A_STRING,"foo")); assertEquals(w, expressions.whereClause); assertEquals(h, expressions.havingClause); }
@Test public void testOrAggFuncInHaving() throws SQLException { String query = "select count(1) from atable group by a_string having count(1) >= 1 or a_string = 'foo'"; List<Object> binds = Collections.emptyList(); Expressions expressions = compileStatement(query,binds); PColumn aCol = ATABLE.getColumnForColumnName("A_STRING"); Expression h = or( constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(),1L), constantComparison(CompareOp.EQUAL, new RowKeyColumnExpression(aCol, // a_string comes from group by key in this case new RowKeyValueAccessor(Arrays.<PColumn>asList(aCol), 0)),"foo")); assertNull(expressions.whereClause); assertEquals(h, expressions.havingClause); }
protected CountAggregateFunction getDelegateFunction(List<Expression> children, StatementContext context) { CountAggregateFunction countFunc = null; if (getChildren().get(0).isStateless()) { countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); } return countFunc; }
protected CountAggregateFunction getDelegateFunction(List<Expression> children, StatementContext context) { CountAggregateFunction countFunc = null; if (getChildren().get(0).isStateless()) { countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); } return countFunc; }
@Override public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException { SumAggregateFunction sumFunc; CountAggregateFunction countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); if (!countFunc.isConstantExpression()) { sumFunc = (SumAggregateFunction)context.getExpressionManager().addIfAbsent(new SumAggregateFunction(countFunc.getChildren(),null)); } else { sumFunc = null; } return new AvgAggregateFunction(children, countFunc, sumFunc); } }
@Override public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException { SumAggregateFunction sumFunc; CountAggregateFunction countFunc = (CountAggregateFunction)context.getExpressionManager().addIfAbsent(new CountAggregateFunction(children)); if (!countFunc.isConstantExpression()) { sumFunc = (SumAggregateFunction)context.getExpressionManager().addIfAbsent(new SumAggregateFunction(countFunc.getChildren(),null)); } else { sumFunc = null; } return new AvgAggregateFunction(children, countFunc, sumFunc); } }