Skip to content

Commit

Permalink
Merge pull request pentaho#320 from kurtwalker/MONDRIAN-1869
Browse files Browse the repository at this point in the history
[MONDRIAN-1869] - avg measures need to be aggregated in SQL similar to d...
  • Loading branch information
lucboudreau committed May 21, 2014
2 parents 02c64b1 + f1d5991 commit 908340b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/mondrian/olap/fun/AggregateFunDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* http://www.eclipse.org/legal/epl-v10.html.
* You must accept the terms of that agreement to use this software.
*
* Copyright (c) 2002-2013 Pentaho Corporation.. All rights reserved.
* Copyright (c) 2002-2014 Pentaho Corporation.. All rights reserved.
*/

package mondrian.olap.fun;
Expand Down Expand Up @@ -106,7 +106,9 @@ public static Object aggregate(
null,
"Don't know how to rollup aggregator '" + aggregator + "'");
}
if (aggregator != RolapAggregator.DistinctCount) {
if (aggregator != RolapAggregator.DistinctCount
&& aggregator != RolapAggregator.Avg)
{
final int savepoint = evaluator.savepoint();
try {
evaluator.setNonEmpty(false);
Expand Down
42 changes: 42 additions & 0 deletions testsrc/main/mondrian/rolap/sql/SqlQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,48 @@ public void testLimitedRollupMemberRetrievableFromCache() throws Exception {
context.executeQuery(mdx);
assertQuerySqlOrNot(context, mdx, patterns, true, false, false);
}

/**
* This is a test for
* <a href="http://jira.pentaho.com/browse/MONDRIAN-1869">MONDRIAN-1869</a>
*
* <p>Avg Aggregates need to be computed in SQL to get correct values.
*/
public void testAvgAggregator() {
propSaver.set(propSaver.properties.GenerateFormattedSql, true);
TestContext context = getTestContext().createSubstitutingCube(
"Sales",
null,
" <Measure name=\"Avg Sales\" column=\"unit_sales\" aggregator=\"avg\"\n"
+ " formatString=\"#.###\"/>",
null,
null);
String mdx = "select measures.[avg sales] on 0 from sales"
+ " where { time.[1997].q1, time.[1997].q2.[4] }";
context.assertQueryReturns(
mdx,
"Axis #0:\n"
+ "{[Time].[1997].[Q1]}\n"
+ "{[Time].[1997].[Q2].[4]}\n"
+ "Axis #1:\n"
+ "{[Measures].[Avg Sales]}\n"
+ "Row #0: 3.069\n");
String sql =
"select\n"
+ " avg(`sales_fact_1997`.`unit_sales`) as `m0`\n"
+ "from\n"
+ " `time_by_day` as `time_by_day`,\n"
+ " `sales_fact_1997` as `sales_fact_1997`\n"
+ "where\n"
+ " `sales_fact_1997`.`time_id` = `time_by_day`.`time_id`\n"
+ "and\n"
+ " ((`time_by_day`.`quarter` = 'Q1' and `time_by_day`.`the_year` = 1997) "
+ "or (`time_by_day`.`month_of_year` = 4 and `time_by_day`.`quarter` = 'Q2' "
+ "and `time_by_day`.`the_year` = 1997))";
SqlPattern mySqlPattern =
new SqlPattern(Dialect.DatabaseProduct.MYSQL, sql, sql.length());
assertQuerySql(context, mdx, new SqlPattern[]{mySqlPattern});
}
}

// End SqlQueryTest.java
2 changes: 1 addition & 1 deletion testsrc/main/mondrian/test/BasicQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// You must accept the terms of that agreement to use this software.
//
// Copyright (C) 2003-2005 Julian Hyde
// Copyright (C) 2005-2013 Pentaho
// Copyright (C) 2005-2014 Pentaho
// All Rights Reserved.
//
// jhyde, Feb 14, 2003
Expand Down
2 changes: 1 addition & 1 deletion testsrc/main/mondrian/test/CompoundSlicerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ public void testRollupAvg() {
+ "where ([Measures].[Avg Unit Sales], [Customers].[OR and CA])",
"Axis #0:\n"
+ "{[Measures].[Avg Unit Sales], [Customers].[OR and CA]}\n"
+ "3.094");
+ "3.092");
}

/**
Expand Down

0 comments on commit 908340b

Please sign in to comment.