From 573221275923b299710cfd3c6077190e8e357a05 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Tue, 12 Oct 2010 00:39:07 +0000 Subject: [PATCH] Fixes for cellsets with no filter or compound filter. Fix formatting if filter is compound (by upgrading mondrian). Add tests now that mondrian correctly returns all positions of compound filter. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@357 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- ivy.xml | 2 +- .../layout/TraditionalCellSetFormatter.java | 35 +++---- testsrc/org/olap4j/CellSetFormatterTest.java | 87 +++++++++++++++++- testsrc/org/olap4j/ConnectionTest.java | 91 +++++++++++++++---- testsrc/org/olap4j/test/TestContext.java | 2 +- 5 files changed, 173 insertions(+), 44 deletions(-) diff --git a/ivy.xml b/ivy.xml index 3b3279c..3892cb7 100644 --- a/ivy.xml +++ b/ivy.xml @@ -76,7 +76,7 @@ - pos) { - CellSetAxis _axis = axis < 0 - ? cellSet.getFilterAxis() - : cellSet.getAxes().get(axis); - List positions = _axis.getPositions(); + final CellSetAxis _axis = cellSet.getAxes().get(axis); + final List positions = _axis.getPositions(); final int positionCount = positions.size(); for (int i = 0; i < positionCount; i++) { - if (axis < 0) { - if (i > 0) { - pw.print(", "); - } + pos.set(axis, i); + if (axis == 0) { + int row = + axis + 1 < pos.size() + ? pos.get(axis + 1) + : 0; + pw.print("Row #" + row + ": "); printCell(cellSet, pw, pos); + pw.println(); } else { - pos.set(axis, i); - if (axis == 0) { - int row = - axis + 1 < pos.size() - ? pos.get(axis + 1) - : 0; - pw.print("Row #" + row + ": "); - } printRows(cellSet, pw, axis - 1, pos); - if (axis == 0) { - pw.println(); - } } } } diff --git a/testsrc/org/olap4j/CellSetFormatterTest.java b/testsrc/org/olap4j/CellSetFormatterTest.java index 7c3ff6f..e1ba11b 100644 --- a/testsrc/org/olap4j/CellSetFormatterTest.java +++ b/testsrc/org/olap4j/CellSetFormatterTest.java @@ -127,8 +127,6 @@ public enum Format { // ~ Tests follow ========================================================== public void testQuery1Traditional() throws SQLException { - final TestContext.Tester.Flavor flavor = - TestContext.instance().getTester().getFlavor(); assertFormat( query1, Format.TRADITIONAL, @@ -316,6 +314,91 @@ public void testEmptyColumnsRectangular() throws SQLException { + "| | | WA | |\n"); } + public void testFilter() throws SQLException { + final String queryString = + "select\n" + + " crossjoin(\n" + + " {[Time].[1997].[Q1], [Time].[1997].[Q2].[4]},\n" + + " {[Measures].[Unit Sales]}) on 0,\n" + + " {[USA].[CA].[Los Angeles],\n" + + " [USA].[CA].[San Francisco]} on 1\n" + + "FROM [Sales]\n" + + "WHERE ([Gender].[M], [Marital Status].[S])"; + + assertFormat( + queryString, + Format.TRADITIONAL, + "Axis #0:\n" + + "{[Gender].[M], [Marital Status].[S]}\n" + + "Axis #1:\n" + + "{[Time].[1997].[Q1], [Measures].[Unit Sales]}\n" + + "{[Time].[1997].[Q2].[4], [Measures].[Unit Sales]}\n" + + "Axis #2:\n" + + "{[Store].[USA].[CA].[Los Angeles]}\n" + + "{[Store].[USA].[CA].[San Francisco]}\n" + + "Row #0: 1,615\n" + + "Row #0: 594\n" + + "Row #1: 101\n" + + "Row #1: 55\n"); + + // TODO: rectagular formatter should print filter + assertFormat( + queryString, + Format.COMPACT_RECTANGULAR, + " 1997 \n" + + " Q1 Q2\n" + + " 4\n" + + " Unit Sales Unit Sales\n" + + "=== == ============= ========== ==========\n" + + "USA CA Los Angeles 1,615 594\n" + + " San Francisco 101 55\n"); + + // TODO: rectagular formatter should print filter + assertFormat( + queryString, + Format.RECTANGULAR, + "| | 1997 |\n" + + "| | Q1 | Q2 |\n" + + "| | | 4 |\n" + + "| | Unit Sales | Unit Sales |\n" + + "+-----+----+---------------+------------+------------+\n" + + "| USA | CA | Los Angeles | 1,615 | 594 |\n" + + "| | | San Francisco | 101 | 55 |\n"); + } + + public void testFilterCompound() throws SQLException { + final String queryString = + "select\n" + + " crossjoin(\n" + + " {[Time].[1997].[Q1], [Time].[1997].[Q2].[4]},\n" + + " {[Measures].[Unit Sales]}) on 0,\n" + + " {[USA].[CA].[Los Angeles],\n" + + " [USA].[CA].[San Francisco]} on 1\n" + + "FROM [Sales]\n" + + "WHERE [Gender].Children * [Marital Status].Members"; + + assertFormat( + queryString, + Format.TRADITIONAL, + "Axis #0:\n" + + "{[Gender].[F], [Marital Status].[All Marital Status]}\n" + + "{[Gender].[F], [Marital Status].[M]}\n" + + "{[Gender].[F], [Marital Status].[S]}\n" + + "{[Gender].[M], [Marital Status].[All Marital Status]}\n" + + "{[Gender].[M], [Marital Status].[M]}\n" + + "{[Gender].[M], [Marital Status].[S]}\n" + + "Axis #1:\n" + + "{[Time].[1997].[Q1], [Measures].[Unit Sales]}\n" + + "{[Time].[1997].[Q2].[4], [Measures].[Unit Sales]}\n" + + "Axis #2:\n" + + "{[Store].[USA].[CA].[Los Angeles]}\n" + + "{[Store].[USA].[CA].[San Francisco]}\n" + + "Row #0: 6,373\n" + + "Row #0: 1,865\n" + + "Row #1: 439\n" + + "Row #1: 149\n"); + } + public void testZeroAxesRectangular() throws SQLException { final String mdx = "select\n" diff --git a/testsrc/org/olap4j/ConnectionTest.java b/testsrc/org/olap4j/ConnectionTest.java index d37fd14..9f44d3d 100644 --- a/testsrc/org/olap4j/ConnectionTest.java +++ b/testsrc/org/olap4j/ConnectionTest.java @@ -522,7 +522,12 @@ public void testAxes() throws SQLException { assertEquals(0, position.getMembers().size()); } - public void testCompoundSlicer() throws SQLException { + /** + * Tests a filter with more than one position. + * + * @throws SQLException on error + */ + public void testCompoundFilter() throws SQLException { connection = tester.createConnection(); OlapConnection olapConnection = tester.getWrapper().unwrap(connection, OlapConnection.class); @@ -531,30 +536,76 @@ public void testCompoundSlicer() throws SQLException { CellSet cellSet = statement.executeOlapQuery( "SELECT {[Measures].[Unit Sales]} on 0,\n" - + "{[Store].Children} on 1\n" + + "{[Product].Children} on 1\n" + "FROM [Sales]\n" + "WHERE [Time].[1997].[Q1] * [Gender].Members"); List axesList = cellSet.getAxes(); assertEquals(2, axesList.size()); final CellSetAxis filterAxis = cellSet.getFilterAxis(); - final Tester.Flavor flavor = - TestContext.instance().getTester().getFlavor(); - switch (flavor) { - case MONDRIAN: - assertEquals(1, filterAxis.getPositionCount()); - break; - case XMLA: - case REMOTE_XMLA: - assertEquals(3, filterAxis.getPositionCount()); - final List filterPositions = filterAxis.getPositions(); - assertEquals(3, filterPositions.size()); - final Position filterPosition = filterPositions.get(2); - assertEquals(2, filterPosition.getMembers().size()); - assertEquals("M", filterPosition.getMembers().get(1).getName()); - break; - default: - throw Olap4jUtil.unexpected(flavor); - } + assertEquals(3, filterAxis.getPositionCount()); + final List filterPositions = filterAxis.getPositions(); + assertEquals(3, filterPositions.size()); + final Position filterPosition = filterPositions.get(2); + assertEquals(2, filterPosition.getMembers().size()); + assertEquals("M", filterPosition.getMembers().get(1).getName()); + } + + /** + * Tests a filter with zero positions. + * + * @throws SQLException on error + */ + public void testEmptyFilter() throws SQLException { + connection = tester.createConnection(); + OlapConnection olapConnection = + tester.getWrapper().unwrap(connection, OlapConnection.class); + OlapStatement statement = olapConnection.createStatement(); + + CellSet cellSet = + statement.executeOlapQuery( + "SELECT {[Measures].[Unit Sales]} on 0,\n" + + "{[Product].Children} on 1\n" + + "FROM [Sales]\n" + + "WHERE [Time].[1997].[Q1] * [Gender].Parent"); + List axesList = cellSet.getAxes(); + assertEquals(2, axesList.size()); + final CellSetAxis filterAxis = cellSet.getFilterAxis(); + assertEquals(0, filterAxis.getPositionCount()); + final List filterPositions = filterAxis.getPositions(); + assertEquals(0, filterPositions.size()); + assertEquals(2, filterAxis.getAxisMetaData().getHierarchies().size()); + final Cell cell = cellSet.getCell(Arrays.asList(0, 0)); + assertTrue(cell.isNull()); + } + + /** + * Tests a query with no filter (no WHERE clause). + * + * @throws SQLException on error + */ + public void testMissingFilter() throws SQLException { + connection = tester.createConnection(); + OlapConnection olapConnection = + tester.getWrapper().unwrap(connection, OlapConnection.class); + OlapStatement statement = olapConnection.createStatement(); + CellSet cellSet = + statement.executeOlapQuery( + "SELECT {[Measures].[Unit Sales]} on 0,\n" + + "{[Product].Children} on 1\n" + + "FROM [Sales]\n"); + List axesList = cellSet.getAxes(); + assertEquals(2, axesList.size()); + final CellSetAxis filterAxis = cellSet.getFilterAxis(); + assertEquals(1, filterAxis.getPositionCount()); + final List filterPositions = filterAxis.getPositions(); + assertEquals(1, filterPositions.size()); + final Position position = filterPositions.get(0); + assertEquals(0, position.getMembers().size()); + assertEquals( + 0, filterAxis.getAxisMetaData().getHierarchies().size()); + assertTrue(filterAxis.getAxisMetaData().getHierarchies().isEmpty()); + final Cell cell = cellSet.getCell(Arrays.asList(0, 0)); + assertEquals("24,597", cell.getFormattedValue()); } public void testMeasureVersusMemberCasting() throws Exception { diff --git a/testsrc/org/olap4j/test/TestContext.java b/testsrc/org/olap4j/test/TestContext.java index 467091a..d378f00 100644 --- a/testsrc/org/olap4j/test/TestContext.java +++ b/testsrc/org/olap4j/test/TestContext.java @@ -114,7 +114,7 @@ public static String toString(ParseTreeNode node) { public static String toString(CellSet cellSet) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); - new TraditionalCellSetFormatter().format(cellSet, pw); + new TraditionalCellSetFormatter().format(cellSet, pw); pw.flush(); return sw.toString(); }