Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Filter() for QueryAxis

  • Loading branch information...
commit 7e31ed001f565caba65c3ee73e5754ed8d6622eb 1 parent d07acc9
@pstoellberger pstoellberger authored
View
2  .gitignore
@@ -16,3 +16,5 @@ src/org/olap4j/mdx/parser/impl/SimpleCharStream.java
src/org/olap4j/mdx/parser/impl/Token.java
src/org/olap4j/mdx/parser/impl/TokenMgrError.java
testlib/
+eclipse-bin/
+
View
22 src/org/olap4j/query/Olap4jNodeConverter.java
@@ -299,6 +299,22 @@ private static AxisNode toOlap4j(QueryAxis axis) {
}
}
+ ParseTreeNode filteredNode = null;
+ if (axis.getFilterCondition() != null) {
+ LiteralNode conditionNode =
+ LiteralNode.createSymbol(
+ null,
+ axis.getFilterCondition());
+ filteredNode =
+ new CallNode(
+ null,
+ "Filter",
+ Syntax.Function,
+ callNode,
+ conditionNode);
+ } else {
+ filteredNode = callNode;
+ }
// We might need to limit the axis set
ParseTreeNode limitedNode = null;
if (axis.getLimitFunction() != null) {
@@ -318,7 +334,7 @@ private static AxisNode toOlap4j(QueryAxis axis) {
null,
axis.getLimitFunction().toString(),
Syntax.Function,
- callNode,
+ filteredNode,
n,
evaluatorNode);
} else {
@@ -327,11 +343,11 @@ private static AxisNode toOlap4j(QueryAxis axis) {
null,
axis.getLimitFunction().toString(),
Syntax.Function,
- callNode,
+ filteredNode,
n);
}
} else {
- limitedNode = callNode;
+ limitedNode = filteredNode;
}
// We might need to sort the whole axis.
ParseTreeNode sortedNode = null;
View
26 src/org/olap4j/query/QueryAxis.java
@@ -47,6 +47,7 @@
private LimitFunction limitFunction = null;
private BigDecimal limitFunctionN = null;
private String limitFunctionSortLiteral = null;
+ private String filterCondition = null;
/**
* Creates a QueryAxis.
*
@@ -418,7 +419,30 @@ public BigDecimal getLimitFunctionN() {
*/ public String getLimitFunctionSortLiteral() {
return limitFunctionSortLiteral;
}
-
+
+ /**
+ * Filter the axis using the given condition before TopCount / Order
+ * are applied
+ * @param filterCondition - the condition used for Filter()
+ */
+ public void filter(String filterCondition) {
+ this.filterCondition = filterCondition;
+ }
+
+ /**
+ * Returns the Filter Condition used to filter the axis set
+ * @return filter condition
+ */
+ public String getFilterCondition() {
+ return this.filterCondition;
+ }
+
+ /**
+ * Clears the filter for this axis
+ */
+ public void clearFilter() {
+ this.filterCondition = null;
+ }
}
View
108 testsrc/org/olap4j/OlapTest.java
@@ -1525,6 +1525,114 @@ public void testLimitFunction() throws Exception {
+ "Row #5: 3,064\n",
resultsString);
}
+ public void testFilter() throws Exception {
+ Cube cube = getFoodmartCube("Sales");
+ if (cube == null) {
+ fail("Could not find Sales cube");
+ }
+ // Setup a base query.
+ Query query = new Query("my query", cube);
+ QueryDimension productDimension = query.getDimension("Product");
+ NamedList<Level> productLevels =
+ productDimension.getDimension()
+ .getDefaultHierarchy().getLevels();
+
+ Level productLevel = productLevels.get("Product Category");
+ productDimension.include(productLevel);
+
+ QueryDimension measuresDimension = query.getDimension("Measures");
+ measuresDimension.include(nameList("Measures", "Sales Count"));
+
+ query.getAxis(Axis.ROWS).addDimension(productDimension);
+ query.getAxis(Axis.COLUMNS).addDimension(measuresDimension);
+
+ query.getAxis(Axis.ROWS).filter("InStr(Product.CurrentMember.Name, 'Beverages') > 0");
+
+ query.validate();
+
+ // Validate the generated MDX
+ String mdxString = query.getSelect().toString();
+ TestContext.assertEqualsVerbose(
+ "SELECT\n"
+ + "{[Measures].[Sales Count]} ON COLUMNS,\n"
+ + "Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'Beverages') > 0) ON ROWS\n"
+ + "FROM [Sales]",
+ mdxString);
+
+ // Validate the returned results
+ CellSet results = query.execute();
+ String resultsString = TestContext.toString(results);
+ TestContext.assertEqualsVerbose(
+ "Axis #0:\n"
+ + "{}\n"
+ + "Axis #1:\n"
+ + "{[Measures].[Sales Count]}\n"
+ + "Axis #2:\n"
+ + "{[Product].[Product].[Drink].[Beverages].[Carbonated Beverages]}\n"
+ + "{[Product].[Product].[Drink].[Beverages].[Hot Beverages]}\n"
+ + "{[Product].[Product].[Drink].[Beverages].[Pure Juice Beverages]}\n"
+ + "Row #0: 1,107\n"
+ + "Row #1: 1,391\n"
+ + "Row #2: 1,096\n",
+ resultsString);
+
+ query.getAxis(Axis.ROWS).limit(
+ LimitFunction.TopCount,
+ new BigDecimal(2),
+ "[Measures].[Sales Count]");
+
+ query.validate();
+
+ // Validate the generated MDX
+ mdxString = query.getSelect().toString();
+ TestContext.assertEqualsVerbose(
+ "SELECT\n"
+ + "{[Measures].[Sales Count]} ON COLUMNS,\n"
+ + "TopCount(Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'Beverages') > 0), 2, [Measures].[Sales Count]) ON ROWS\n"
+ + "FROM [Sales]",
+ mdxString);
+
+ // Validate the returned results
+ results = query.execute();
+ resultsString = TestContext.toString(results);
+ TestContext.assertEqualsVerbose(
+ "Axis #0:\n"
+ + "{}\n"
+ + "Axis #1:\n"
+ + "{[Measures].[Sales Count]}\n"
+ + "Axis #2:\n"
+ + "{[Product].[Product].[Drink].[Beverages].[Hot Beverages]}\n"
+ + "{[Product].[Product].[Drink].[Beverages].[Carbonated Beverages]}\n"
+ + "Row #0: 1,391\n"
+ + "Row #1: 1,107\n",
+ resultsString);
+
+ query.getAxis(Axis.ROWS).filter("InStr(Product.CurrentMember.Name, 'NoMatchingString') > 0");
+
+ query.validate();
+
+ // Validate the generated MDX
+ mdxString = query.getSelect().toString();
+ TestContext.assertEqualsVerbose(
+ "SELECT\n"
+ + "{[Measures].[Sales Count]} ON COLUMNS,\n"
+ + "TopCount(Filter({[Product].[Product].[Product Category].Members}, InStr(Product.CurrentMember.Name, 'NoMatchingString') > 0), 2, [Measures].[Sales Count]) ON ROWS\n"
+ + "FROM [Sales]",
+ mdxString);
+
+ // Validate the returned results
+ results = query.execute();
+ resultsString = TestContext.toString(results);
+ TestContext.assertEqualsVerbose(
+ "Axis #0:\n"
+ + "{}\n"
+ + "Axis #1:\n"
+ + "{[Measures].[Sales Count]}\n"
+ + "Axis #2:\n",
+ resultsString);
+
+
+ }
public void testHierarchyConsistency() throws Exception {
Cube cube = getFoodmartCube("Sales");
if (cube == null) {
Please sign in to comment.
Something went wrong with that request. Please try again.