Skip to content

Commit

Permalink
MONDRIAN: Fix NativeTopCount caching bug. The cache key did not take …
Browse files Browse the repository at this point in the history
…into account the number of elements returned. So TopCount(... 2 ...) and TopCount(... 3 ...) returned the same result from the cache.

[git-p4: depot-paths = "//open/mondrian/": change = 12200]
  • Loading branch information
Andreas Voss committed Jan 6, 2009
1 parent 77bd7b0 commit c5fae12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/mondrian/rolap/RolapNativeTopCount.java
Expand Up @@ -35,13 +35,15 @@ public RolapNativeTopCount() {
static class TopCountConstraint extends SetConstraint {
Exp orderByExpr;
boolean ascending;
Integer count;

public TopCountConstraint(
public TopCountConstraint(int count,
CrossJoinArg[] args, RolapEvaluator evaluator,
Exp orderByExpr, boolean ascending) {
super(args, evaluator, true);
this.orderByExpr = orderByExpr;
this.ascending = ascending;
this.count = new Integer(count);
}

/**
Expand Down Expand Up @@ -80,6 +82,7 @@ public Object getCacheKey() {
key.add(orderByExpr.toString());
}
key.add(ascending);
key.add(count);
return key;
}
}
Expand Down Expand Up @@ -147,7 +150,7 @@ NativeEvaluator createEvaluator(RolapEvaluator evaluator, FunDef fun, Exp[] args
evaluator = overrideContext(evaluator, cargs, sql.getStoredMeasure());

TupleConstraint constraint =
new TopCountConstraint(cargs, evaluator, orderByExpr, ascending);
new TopCountConstraint(count, cargs, evaluator, orderByExpr, ascending);
SetEvaluator sev = new SetEvaluator(cargs, schemaReader, constraint);
sev.setMaxRows(count);
return sev;
Expand Down
38 changes: 38 additions & 0 deletions testsrc/main/mondrian/rolap/NonEmptyTest.java
Expand Up @@ -68,6 +68,44 @@ public NonEmptyTest(String name) {
super(name);
}

public void testTopCountCacheKeyMustIncludeCount() {
/**
* When caching topcount results, the number of elements must
* be part of the cache key
*/
TestContext ctx = TestContext.instance();
// fill cache
ctx.assertQueryReturns(
"select {[Measures].[Unit Sales]} ON COLUMNS, " +
"TopCount([Product].[Product Subcategory].Members, 2, [Measures].[Unit Sales]) ON ROWS " +
"from [Sales]",
"Axis #0:" + nl +
"{}" + nl +
"Axis #1:" + nl +
"{[Measures].[Unit Sales]}" + nl +
"Axis #2:" + nl +
"{[Product].[All Products].[Food].[Produce].[Vegetables].[Fresh Vegetables]}" + nl +
"{[Product].[All Products].[Food].[Produce].[Fruit].[Fresh Fruit]}" + nl +
"Row #0: 20,739" + nl +
"Row #1: 11,767" + nl);
// run again with different count
ctx.assertQueryReturns(
"select {[Measures].[Unit Sales]} ON COLUMNS, " +
"TopCount([Product].[Product Subcategory].Members, 3, [Measures].[Unit Sales]) ON ROWS " +
"from [Sales]",
"Axis #0:" + nl +
"{}" + nl +
"Axis #1:" + nl +
"{[Measures].[Unit Sales]}" + nl +
"Axis #2:" + nl +
"{[Product].[All Products].[Food].[Produce].[Vegetables].[Fresh Vegetables]}" + nl +
"{[Product].[All Products].[Food].[Produce].[Fruit].[Fresh Fruit]}" + nl +
"{[Product].[All Products].[Food].[Canned Foods].[Canned Soup].[Soup]}" + nl +
"Row #0: 20,739" + nl +
"Row #1: 11,767" + nl +
"Row #2: 8,006" + nl);
}

public void testStrMeasure() {
TestContext ctx = TestContext.create(
null,
Expand Down

0 comments on commit c5fae12

Please sign in to comment.