Skip to content

Commit

Permalink
MONDRIAN: Fix bug 1412834 [NullPointerException in SqlConstraintUtils].
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 5479]
  • Loading branch information
julianhyde committed Feb 16, 2006
1 parent fbdc980 commit d72f93c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/main/mondrian/rolap/RolapAggregationManager.java
Expand Up @@ -47,8 +47,9 @@ public abstract class RolapAggregationManager implements CellReader {
* query for levels below each current member. This additional context
* makes the drill-through queries easier for humans to understand.
**/
static CellRequest makeRequest(Member[] members,
boolean extendedContext) {
static CellRequest makeRequest(
Member[] members,
boolean extendedContext) {
boolean showNames = extendedContext;
if (!(members[0] instanceof RolapStoredMeasure)) {
return null;
Expand Down
61 changes: 39 additions & 22 deletions src/main/mondrian/rolap/SqlConstraintUtils.java
Expand Up @@ -27,7 +27,7 @@
/**
* Utility class used by implementations of {@link mondrian.rolap.sql.SqlConstraint},
* used to generate constraints into {@link mondrian.rolap.sql.SqlQuery}.
*
*
* @author av
* @since Nov 21, 2005
* @version $Id$
Expand All @@ -41,24 +41,33 @@ private SqlConstraintUtils() {
/**
* For every restricting member in the current context, generates
* a WHERE condition and a join to the fact table.
*
*
* @param sqlQuery the query to modify
* @param strict defines the behavior if the current context contains
* @param strict defines the behavior if the current context contains
* calculated members.
* If true, an exception is thrown,
* otherwise calculated members are silently ignored.
*/
public static void addContextConstraint(SqlQuery sqlQuery, Evaluator e, boolean strict) {

public static void addContextConstraint(
SqlQuery sqlQuery,
Evaluator e,
boolean strict) {

Member[] members = e.getMembers();
if (strict) {
assert !containsCalculatedMember(members) : "addContextConstraint: can not restrict SQL to calculated Members";
}
else {
assert !containsCalculatedMember(members) :
"can not restrict SQL to calculated Members";
} else {
members = removeCalculatedMembers(members);
}

CellRequest request = RolapAggregationManager.makeRequest(members, false);

CellRequest request =
RolapAggregationManager.makeRequest(members, false);
if (request == null) {
// One or more of the members was null or calculated, so the
// request is impossible to satisfy.
return;
}
RolapStar.Column[] columns = request.getColumns();
Object[] values = request.getSingleValues();
int arity = columns.length;
Expand All @@ -70,27 +79,33 @@ public static void addContextConstraint(SqlQuery sqlQuery, Evaluator e, boolean

String expr = column.getExpression(sqlQuery);
String value = sqlQuery.quote(column.isNumeric(), values[i]);
sqlQuery.addWhere(expr, RolapUtil.sqlNullLiteral.equals(value) ? " is " : " = ", value);
sqlQuery.addWhere(expr,
RolapUtil.sqlNullLiteral.equals(value) ? " is " : " = ",
value);
}
}

private static Member[] removeCalculatedMembers(Member[] members) {
List result = new ArrayList();
for (int i = 0; i < members.length; i++)
if (!members[i].isCalculated())
for (int i = 0; i < members.length; i++) {
if (!members[i].isCalculated()) {
result.add(members[i]);
}
}
return (Member[]) result.toArray(new Member[result.size()]);
}

private static boolean containsCalculatedMember(Member[] members) {
for (int i = 0; i < members.length; i++)
if (members[i].isCalculated())
for (int i = 0; i < members.length; i++) {
if (members[i].isCalculated()) {
return true;
}
}
return false;
}

/**
* ensures that the table of <code>level</code> is joined to the fact table
* Ensures that the table of <code>level</code> is joined to the fact table
*/
public static void joinLevelTableToFactTable(SqlQuery sqlQuery, RolapCube cube,
RolapLevel level) {
Expand All @@ -105,8 +120,8 @@ public static void joinLevelTableToFactTable(SqlQuery sqlQuery, RolapCube cube,
* creates a WHERE parent = value
* @param sqlQuery the query to modify
* @param parent the list of parent members
* @param strict defines the behavior if <code>parent</code>
* is a calculated member.
* @param strict defines the behavior if <code>parent</code>
* is a calculated member.
* If true, an exception is thrown,
* otherwise calculated members are silently ignored.
*/
Expand All @@ -119,11 +134,11 @@ public static void addMemberConstraint(SqlQuery sqlQuery, RolapMember parent, bo
/**
* Creates a "WHERE exp IN (...)" condition containing the values
* of all parents. All parents must belong to the same level.
*
*
* @param sqlQuery the query to modify
* @param parents the list of parent members
* @param strict defines the behavior if <code>parents</code>
* contains calculated members.
* @param strict defines the behavior if <code>parents</code>
* contains calculated members.
* If true, an exception is thrown,
* otherwise calculated members are silently ignored.
*/
Expand Down Expand Up @@ -175,3 +190,5 @@ private static Collection getUniqueParentMembers(Collection members) {
return set;
}
}

// End SqlConstraintUtils.java
20 changes: 20 additions & 0 deletions testsrc/main/mondrian/rolap/NonEmptyTest.java
Expand Up @@ -696,6 +696,26 @@ public void excutingSql(TupleEvent e) {
}
}

public void testBug1412384() {
// Bug 1412384 causes a NPE in SqlConstraintUtils.
assertQueryReturns("select NON EMPTY {[Time].[1997]} ON COLUMNS,\n" +
"NON EMPTY Hierarchize(Union({[Customers].[All Customers]},\n" +
"[Customers].[All Customers].Children)) ON ROWS\n" +
"from [Sales]\n" +
"where [Measures].[Profit]",
fold(new String[] {
"Axis #0:",
"{[Measures].[Profit]}",
"Axis #1:",
"{[Time].[1997]}",
"Axis #2:",
"{[Customers].[All Customers]}",
"{[Customers].[All Customers].[USA]}",
"Row #0: $339,610.90",
"Row #1: $339,610.90",
""}));
}

/**
* make sure the following is not run natively
*/
Expand Down

0 comments on commit d72f93c

Please sign in to comment.