CompiledAggregateFunction extends AbstractCompiledValue { @Override public Object evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException { boolean isPRQueryNode = context.getIsPRQueryNode(); boolean isBucketNode = context.getBucketList() != null; switch (this.aggFuncType) { case OQLLexerTokenTypes.SUM: if (isPRQueryNode) { return this.distinctOnly ? new SumDistinctPRQueryNode() : new Sum(); } else { return this.distinctOnly ? (isBucketNode ? new DistinctAggregator() : new SumDistinct()) : new Sum(); } case OQLLexerTokenTypes.MAX: return new MaxMin(true); case OQLLexerTokenTypes.MIN: return new MaxMin(false); case OQLLexerTokenTypes.AVG: if (isPRQueryNode) { return this.distinctOnly ? new AvgDistinctPRQueryNode() : new AvgPRQueryNode(); } else { return this.distinctOnly ? (isBucketNode ? new DistinctAggregator() : new AvgDistinct()) : (isBucketNode ? new AvgBucketNode() : new Avg()); } case OQLLexerTokenTypes.COUNT: if (isPRQueryNode) { return this.distinctOnly ? new CountDistinctPRQueryNode() : new CountPRQueryNode(); } else { return this.distinctOnly ? (isBucketNode ? new DistinctAggregator() : new CountDistinct()) : new Count(); } default: throw new UnsupportedOperationException("Aggregate function not implemented"); } } CompiledAggregateFunction(CompiledValue expr, int aggFunc);  CompiledAggregateFunction(CompiledValue expr, int aggFunc, boolean distinctOnly); @Override int getType(); @Override Object evaluate(ExecutionContext context); int getFunctionType(); CompiledValue getParameter(); ObjectType getObjectType(); @Override void generateCanonicalizedExpression(StringBuilder clauseBuffer, ExecutionContext context);  }
@Test public void testCount() throws Exception { CompiledAggregateFunction caf1 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT); ExecutionContext context1 = new ExecutionContext(null, cache); assertThat(caf1.evaluate(context1)).isInstanceOf(Count.class); CompiledAggregateFunction caf2 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT, true); ExecutionContext context2 = new ExecutionContext(null, cache); assertThat(caf2.evaluate(context2)).isInstanceOf(CountDistinct.class); CompiledAggregateFunction caf3 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT); ExecutionContext context3 = new ExecutionContext(null, cache); context3.setIsPRQueryNode(true); assertThat(caf3.evaluate(context3)).isInstanceOf(CountPRQueryNode.class); CompiledAggregateFunction caf4 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT); QueryExecutionContext context4 = new QueryExecutionContext(null, cache); context4.setBucketList(bucketList); assertThat(caf4.evaluate(context4)).isInstanceOf(Count.class); CompiledAggregateFunction caf5 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT, true); ExecutionContext context5 = new ExecutionContext(null, cache); context5.setIsPRQueryNode(true); assertThat(caf5.evaluate(context5)).isInstanceOf(CountDistinctPRQueryNode.class); CompiledAggregateFunction caf6 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.COUNT, true); QueryExecutionContext context6 = new QueryExecutionContext(null, cache); context6.setBucketList(bucketList); assertThat(caf6.evaluate(context6)).isInstanceOf(DistinctAggregator.class); }
@Test public void testSum() throws Exception { CompiledAggregateFunction caf1 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM); ExecutionContext context1 = new ExecutionContext(null, cache); assertThat(caf1.evaluate(context1)).isInstanceOf(Sum.class); CompiledAggregateFunction caf2 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM, true); ExecutionContext context2 = new ExecutionContext(null, cache); assertThat(caf2.evaluate(context2)).isInstanceOf(SumDistinct.class); CompiledAggregateFunction caf3 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM); ExecutionContext context3 = new ExecutionContext(null, cache); context3.setIsPRQueryNode(true); assertThat(caf3.evaluate(context3)).isInstanceOf(Sum.class); CompiledAggregateFunction caf4 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM); QueryExecutionContext context4 = new QueryExecutionContext(null, cache); context4.setBucketList(bucketList); assertThat(caf4.evaluate(context4)).isInstanceOf(Sum.class); CompiledAggregateFunction caf5 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM, true); ExecutionContext context5 = new ExecutionContext(null, cache); context5.setIsPRQueryNode(true); assertThat(caf5.evaluate(context5)).isInstanceOf(SumDistinctPRQueryNode.class); CompiledAggregateFunction caf6 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.SUM, true); QueryExecutionContext context6 = new QueryExecutionContext(null, cache); context6.setBucketList(bucketList); assertThat(caf6.evaluate(context6)).isInstanceOf(DistinctAggregator.class); }
@Test public void testAvg() throws Exception { CompiledAggregateFunction caf1 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG); ExecutionContext context1 = new ExecutionContext(null, cache); assertThat(caf1.evaluate(context1)).isInstanceOf(Avg.class); CompiledAggregateFunction caf2 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG, true); ExecutionContext context2 = new ExecutionContext(null, cache); assertThat(caf2.evaluate(context2)).isInstanceOf(AvgDistinct.class); CompiledAggregateFunction caf3 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG); ExecutionContext context3 = new ExecutionContext(null, cache); context3.setIsPRQueryNode(true); assertThat(caf3.evaluate(context3)).isInstanceOf(AvgPRQueryNode.class); CompiledAggregateFunction caf4 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG); QueryExecutionContext context4 = new QueryExecutionContext(null, cache); context4.setBucketList(this.bucketList); assertThat(caf4.evaluate(context4)).isInstanceOf(AvgBucketNode.class); CompiledAggregateFunction caf5 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG, true); ExecutionContext context5 = new ExecutionContext(null, cache); context5.setIsPRQueryNode(true); assertThat(caf5.evaluate(context5)).isInstanceOf(AvgDistinctPRQueryNode.class); CompiledAggregateFunction caf6 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.AVG, true); QueryExecutionContext context6 = new QueryExecutionContext(null, cache); context6.setBucketList(this.bucketList); assertThat(caf6.evaluate(context6)).isInstanceOf(DistinctAggregator.class); }
@Test public void testMaxMin() throws Exception { CompiledAggregateFunction caf1 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.MAX); ExecutionContext context1 = new ExecutionContext(null, cache); Aggregator agg = (Aggregator) caf1.evaluate(context1); assertThat(agg).isInstanceOf(MaxMin.class); MaxMin maxMin = (MaxMin) agg; Class maxMinClass = MaxMin.class; Field findMax = maxMinClass.getDeclaredField("findMax"); findMax.setAccessible(true); assertThat(findMax.get(maxMin)).isEqualTo(Boolean.TRUE); CompiledAggregateFunction caf2 = new CompiledAggregateFunction(null, OQLLexerTokenTypes.MIN); Aggregator agg1 = (Aggregator) caf2.evaluate(context1); assertThat(agg1).isInstanceOf(MaxMin.class); MaxMin maxMin1 = (MaxMin) agg1; assertThat(findMax.get(maxMin1)).isEqualTo(Boolean.FALSE); }