Skip to content

Commit 7e31ed0

Browse files
committed
add Filter() for QueryAxis
1 parent d07acc9 commit 7e31ed0

File tree

4 files changed

+154
-4
lines changed

4 files changed

+154
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ src/org/olap4j/mdx/parser/impl/SimpleCharStream.java
1616
src/org/olap4j/mdx/parser/impl/Token.java
1717
src/org/olap4j/mdx/parser/impl/TokenMgrError.java
1818
testlib/
19+
eclipse-bin/
20+

src/org/olap4j/query/Olap4jNodeConverter.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,22 @@ private static AxisNode toOlap4j(QueryAxis axis) {
299299
}
300300
}
301301

302+
ParseTreeNode filteredNode = null;
303+
if (axis.getFilterCondition() != null) {
304+
LiteralNode conditionNode =
305+
LiteralNode.createSymbol(
306+
null,
307+
axis.getFilterCondition());
308+
filteredNode =
309+
new CallNode(
310+
null,
311+
"Filter",
312+
Syntax.Function,
313+
callNode,
314+
conditionNode);
315+
} else {
316+
filteredNode = callNode;
317+
}
302318
// We might need to limit the axis set
303319
ParseTreeNode limitedNode = null;
304320
if (axis.getLimitFunction() != null) {
@@ -318,7 +334,7 @@ private static AxisNode toOlap4j(QueryAxis axis) {
318334
null,
319335
axis.getLimitFunction().toString(),
320336
Syntax.Function,
321-
callNode,
337+
filteredNode,
322338
n,
323339
evaluatorNode);
324340
} else {
@@ -327,11 +343,11 @@ private static AxisNode toOlap4j(QueryAxis axis) {
327343
null,
328344
axis.getLimitFunction().toString(),
329345
Syntax.Function,
330-
callNode,
346+
filteredNode,
331347
n);
332348
}
333349
} else {
334-
limitedNode = callNode;
350+
limitedNode = filteredNode;
335351
}
336352
// We might need to sort the whole axis.
337353
ParseTreeNode sortedNode = null;

src/org/olap4j/query/QueryAxis.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class QueryAxis extends QueryNodeImpl {
4747
private LimitFunction limitFunction = null;
4848
private BigDecimal limitFunctionN = null;
4949
private String limitFunctionSortLiteral = null;
50+
private String filterCondition = null;
5051
/**
5152
* Creates a QueryAxis.
5253
*
@@ -418,7 +419,30 @@ public BigDecimal getLimitFunctionN() {
418419
*/ public String getLimitFunctionSortLiteral() {
419420
return limitFunctionSortLiteral;
420421
}
421-
422+
423+
/**
424+
* Filter the axis using the given condition before TopCount / Order
425+
* are applied
426+
* @param filterCondition - the condition used for Filter()
427+
*/
428+
public void filter(String filterCondition) {
429+
this.filterCondition = filterCondition;
430+
}
431+
432+
/**
433+
* Returns the Filter Condition used to filter the axis set
434+
* @return filter condition
435+
*/
436+
public String getFilterCondition() {
437+
return this.filterCondition;
438+
}
439+
440+
/**
441+
* Clears the filter for this axis
442+
*/
443+
public void clearFilter() {
444+
this.filterCondition = null;
445+
}
422446

423447
}
424448

testsrc/org/olap4j/OlapTest.java

+108
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,114 @@ public void testLimitFunction() throws Exception {
15241524
+ "Row #4: 3,836\n"
15251525
+ "Row #5: 3,064\n",
15261526
resultsString);
1527+
}
1528+
public void testFilter() throws Exception {
1529+
Cube cube = getFoodmartCube("Sales");
1530+
if (cube == null) {
1531+
fail("Could not find Sales cube");
1532+
}
1533+
// Setup a base query.
1534+
Query query = new Query("my query", cube);
1535+
QueryDimension productDimension = query.getDimension("Product");
1536+
NamedList<Level> productLevels =
1537+
productDimension.getDimension()
1538+
.getDefaultHierarchy().getLevels();
1539+
1540+
Level productLevel = productLevels.get("Product Category");
1541+
productDimension.include(productLevel);
1542+
1543+
QueryDimension measuresDimension = query.getDimension("Measures");
1544+
measuresDimension.include(nameList("Measures", "Sales Count"));
1545+
1546+
query.getAxis(Axis.ROWS).addDimension(productDimension);
1547+
query.getAxis(Axis.COLUMNS).addDimension(measuresDimension);
1548+
1549+
query.getAxis(Axis.ROWS).filter("InStr(Product.CurrentMember.Name, 'Beverages') > 0");
1550+
1551+
query.validate();
1552+
1553+
// Validate the generated MDX
1554+
String mdxString = query.getSelect().toString();
1555+
TestContext.assertEqualsVerbose(
1556+
"SELECT\n"
1557+
+ "{[Measures].[Sales Count]} ON COLUMNS,\n"
1558+
+ "Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'Beverages') > 0) ON ROWS\n"
1559+
+ "FROM [Sales]",
1560+
mdxString);
1561+
1562+
// Validate the returned results
1563+
CellSet results = query.execute();
1564+
String resultsString = TestContext.toString(results);
1565+
TestContext.assertEqualsVerbose(
1566+
"Axis #0:\n"
1567+
+ "{}\n"
1568+
+ "Axis #1:\n"
1569+
+ "{[Measures].[Sales Count]}\n"
1570+
+ "Axis #2:\n"
1571+
+ "{[Product].[Product].[Drink].[Beverages].[Carbonated Beverages]}\n"
1572+
+ "{[Product].[Product].[Drink].[Beverages].[Hot Beverages]}\n"
1573+
+ "{[Product].[Product].[Drink].[Beverages].[Pure Juice Beverages]}\n"
1574+
+ "Row #0: 1,107\n"
1575+
+ "Row #1: 1,391\n"
1576+
+ "Row #2: 1,096\n",
1577+
resultsString);
1578+
1579+
query.getAxis(Axis.ROWS).limit(
1580+
LimitFunction.TopCount,
1581+
new BigDecimal(2),
1582+
"[Measures].[Sales Count]");
1583+
1584+
query.validate();
1585+
1586+
// Validate the generated MDX
1587+
mdxString = query.getSelect().toString();
1588+
TestContext.assertEqualsVerbose(
1589+
"SELECT\n"
1590+
+ "{[Measures].[Sales Count]} ON COLUMNS,\n"
1591+
+ "TopCount(Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'Beverages') > 0), 2, [Measures].[Sales Count]) ON ROWS\n"
1592+
+ "FROM [Sales]",
1593+
mdxString);
1594+
1595+
// Validate the returned results
1596+
results = query.execute();
1597+
resultsString = TestContext.toString(results);
1598+
TestContext.assertEqualsVerbose(
1599+
"Axis #0:\n"
1600+
+ "{}\n"
1601+
+ "Axis #1:\n"
1602+
+ "{[Measures].[Sales Count]}\n"
1603+
+ "Axis #2:\n"
1604+
+ "{[Product].[Product].[Drink].[Beverages].[Hot Beverages]}\n"
1605+
+ "{[Product].[Product].[Drink].[Beverages].[Carbonated Beverages]}\n"
1606+
+ "Row #0: 1,391\n"
1607+
+ "Row #1: 1,107\n",
1608+
resultsString);
1609+
1610+
query.getAxis(Axis.ROWS).filter("InStr(Product.CurrentMember.Name, 'NoMatchingString') > 0");
1611+
1612+
query.validate();
1613+
1614+
// Validate the generated MDX
1615+
mdxString = query.getSelect().toString();
1616+
TestContext.assertEqualsVerbose(
1617+
"SELECT\n"
1618+
+ "{[Measures].[Sales Count]} ON COLUMNS,\n"
1619+
+ "TopCount(Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'NoMatchingString') > 0), 2, [Measures].[Sales Count]) ON ROWS\n"
1620+
+ "FROM [Sales]",
1621+
mdxString);
1622+
1623+
// Validate the returned results
1624+
results = query.execute();
1625+
resultsString = TestContext.toString(results);
1626+
TestContext.assertEqualsVerbose(
1627+
"Axis #0:\n"
1628+
+ "{}\n"
1629+
+ "Axis #1:\n"
1630+
+ "{[Measures].[Sales Count]}\n"
1631+
+ "Axis #2:\n",
1632+
resultsString);
1633+
1634+
15271635
}
15281636
public void testHierarchyConsistency() throws Exception {
15291637
Cube cube = getFoodmartCube("Sales");

0 commit comments

Comments
 (0)