Skip to content

Commit

Permalink
MONDRIAN: Add Cell.getCoordinateList();
Browse files Browse the repository at this point in the history
    Implement an alternative format for converting an MDX query result to a string.
    (Code currently lives in TestContext, but could move to util or even to olap4j.)

[git-p4: depot-paths = "//open/mondrian/": change = 12590]
  • Loading branch information
julianhyde committed Apr 11, 2009
1 parent 114b988 commit c67c1df
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/main/mondrian/olap/Cell.java
Expand Up @@ -4,7 +4,7 @@
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2001-2002 Kana Software, Inc.
// Copyright (C) 2001-2006 Julian Hyde and others
// Copyright (C) 2001-2009 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
Expand All @@ -13,6 +13,8 @@

package mondrian.olap;

import java.util.List;

/**
* A <code>Cell</code> is an item in the grid of a {@link Result}. It is
* returned by {@link Result#getCell}.
Expand All @@ -22,6 +24,13 @@
* @version $Id$
*/
public interface Cell {
/**
* Returns the coordinates of this Cell in its {@link Result}.
*
* @return Coordinates of this Cell
*/
List<Integer> getCoordinateList();

/**
* Returns the cell's raw value. This is useful for sending to further data
* processing, such as plotting a chart.
Expand All @@ -41,7 +50,6 @@ public interface Cell {
*/
Object getValue();


/**
* Return the cached formatted string, that survives an aggregate cache clear
*/
Expand Down
15 changes: 13 additions & 2 deletions src/main/mondrian/rolap/RolapCell.java
Expand Up @@ -15,8 +15,7 @@
import mondrian.rolap.agg.CellRequest;

import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import java.util.*;

/**
* <code>RolapCell</code> implements {@link mondrian.olap.Cell} within a
Expand All @@ -42,6 +41,18 @@ class RolapCell implements Cell {
this.ci = ci;
}

public List<Integer> getCoordinateList() {
return new AbstractList<Integer>() {
public Integer get(int index) {
return pos[index];
}

public int size() {
return pos.length;
}
};
}

public Object getValue() {
if (ci.value == Util.nullValue) {
return null;
Expand Down
126 changes: 126 additions & 0 deletions testsrc/main/mondrian/olap/UtilTestCase.java
Expand Up @@ -15,6 +15,7 @@
import java.sql.Driver;

import mondrian.util.*;
import mondrian.test.TestContext;

/**
* Tests for methods in {@link mondrian.olap.Util}.
Expand Down Expand Up @@ -582,6 +583,131 @@ public void assertEqualsArray(int[] expected, int[] actual) {
assertEquals(expectedList, actualList);
}
}


public void testTextFormatter() {
/*
| 1997 |
| Q1 | Q2 |
| | 4 |
| Unit Sales | Store Sales | Unit Sales | Store Sales |
----+----+---------------+------------+-------------+------------+-------------+
USA | CA | Los Angeles | | | | |
| WA | Seattle | | | | |
| CA | San Francisco | | | | |
1997
Q1 Q2
4
Unit Sales Store Sales Unit Sales Store Sales
=== == ============= ========== =========== ========== ===========
USA CA Los Angeles 12 34.5 13 35.60
WA Seattle 12 34.5 13 35.60
CA San Francisco 12 34.5 13 35.60
*/
final String queryString =
"select\n"
+ " crossjoin(\n"
+ " {[Time].[1997].[Q1], [Time].[1997].[Q2].[4]},\n"
+ " {[Measures].[Unit Sales], [Measures].[Store Sales]}) on 0,\n"
+ " {[USA].[CA].[Los Angeles],\n"
+ " [USA].[WA].[Seattle],\n"
+ " [USA].[CA].[San Francisco]} on 1\n"
+ "FROM [Sales]";
assertFormat(
queryString,
TestContext.Format.TRADITIONAL,
TestContext.fold(
"Axis #0:\n"
+ "{}\n"
+ "Axis #1:\n"
+ "{[Time].[1997].[Q1], [Measures].[Unit Sales]}\n"
+ "{[Time].[1997].[Q1], [Measures].[Store Sales]}\n"
+ "{[Time].[1997].[Q2].[4], [Measures].[Unit Sales]}\n"
+ "{[Time].[1997].[Q2].[4], [Measures].[Store Sales]}\n"
+ "Axis #2:\n"
+ "{[Store].[All Stores].[USA].[CA].[Los Angeles]}\n"
+ "{[Store].[All Stores].[USA].[WA].[Seattle]}\n"
+ "{[Store].[All Stores].[USA].[CA].[San Francisco]}\n"
+ "Row #0: 6,373\n"
+ "Row #0: 13,736.97\n"
+ "Row #0: 1,865\n"
+ "Row #0: 3,917.49\n"
+ "Row #1: 6,098\n"
+ "Row #1: 12,760.64\n"
+ "Row #1: 2,121\n"
+ "Row #1: 4,444.06\n"
+ "Row #2: 439\n"
+ "Row #2: 936.51\n"
+ "Row #2: 149\n"
+ "Row #2: 327.33\n"));

// Same query, Rectangular format
assertFormat(
queryString,
TestContext.Format.COMPACT_RECTANGULAR,
TestContext.fold(
" 1997 1997 1997 1997\n"
+ " Q1 Q1 Q2 Q2\n"
+ " 4 4\n"
+ " Unit Sales Store Sales Unit Sales Store Sales\n"
+ "=== == ============= ========== =========== ========== ===========\n"
+ "USA CA Los Angeles 6,373 13,736.97 1,865 3,917.49\n"
+ "USA WA Seattle 6,098 12,760.64 2,121 4,444.06\n"
+ "USA CA San Francisco 439 936.51 149 327.33\n"));

// Similar query with an 'all' member on rows. Need an extra column.
assertFormat(
"select\n"
+ " crossjoin(\n"
+ " {[Time].[1997].[Q1], [Time].[1997].[Q2].[4]},\n"
+ " {[Measures].[Unit Sales], [Measures].[Store Sales]}) on 0,\n"
+ " {[Store],\n"
+ " [Store].[USA],\n"
+ " [Store].[USA].[CA],\n"
+ " [Store].[USA].[CA].[Los Angeles],\n"
+ " [Store].[USA].[WA]} on 1\n"
+ "FROM [Sales]",
TestContext.Format.COMPACT_RECTANGULAR,
TestContext.fold(
" 1997 1997 1997 1997\n"
+ " Q1 Q1 Q2 Q2\n"
+ " 4 4\n"
+ " Unit Sales Store Sales Unit Sales Store Sales\n"
+ "========== === == =========== ========== =========== ========== ===========\n"
+ "All Stores 66,291 139,628.35 20,179 42,878.25\n"
+ "All Stores USA 66,291 139,628.35 20,179 42,878.25\n"
+ "All Stores USA CA 16,890 36,175.20 6,382 13,605.89\n"
+ "All Stores USA CA Los Angeles 6,373 13,736.97 1,865 3,917.49\n"
+ "All Stores USA WA 30,114 63,282.86 9,896 20,926.37\n"));

// TODO: test with rows axis empty
// TODO: test with cols axis empty
// TODO: test with 0 axes
// TODO: test with 1 axes
// TODO: test with 3 axes
// TODO: formatter should right-justify cells
// TODO: implement & test non-compact rect formatter
// TODO: eliminate repeated captions (e.g. 'Q1 Q1' becomes 'Q1')
// but make sure that they are only eliminated if the parent
// is the same
}

private void assertFormat(
String queryString,
TestContext.Format format,
String expected)
{
Result result =
TestContext.instance().executeQuery(queryString);
String resultString =
TestContext.toString(result, format);
TestContext.assertEqualsVerbose(
expected,
resultString);
}
}

// End UtilTestCase.java

0 comments on commit c67c1df

Please sign in to comment.