Skip to content

Commit

Permalink
MONDRIAN: CastFunDef can throw exceptions when the cell is not loaded…
Browse files Browse the repository at this point in the history
… yet. This causes RolapResult.executeBody() to fail. Now CastFunDef will return NULL instead.

[git-p4: depot-paths = "//open/mondrian/": change = 8482]
  • Loading branch information
Bart Pappyn committed Jan 8, 2007
1 parent bdc00f2 commit 39bc261
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/main/mondrian/olap/fun/CastFunDef.java
Expand Up @@ -16,6 +16,7 @@
import mondrian.calc.ExpCompiler;
import mondrian.calc.impl.GenericCalc;
import mondrian.mdx.ResolvedFunCall;
import mondrian.olap.fun.FunUtil;

/**
* Definition of the <code>CAST</code> MDX operator.
Expand Down Expand Up @@ -67,6 +68,9 @@ public String evaluateString(Evaluator evaluator) {

public int evaluateInteger(Evaluator evaluator) {
final Object o = evaluate(evaluator);
if (o == null) {
return FunUtil.IntegerNull;
}
if (o instanceof String) {
return Integer.parseInt((String) o);
}
Expand All @@ -78,6 +82,9 @@ public int evaluateInteger(Evaluator evaluator) {

public double evaluateDouble(Evaluator evaluator) {
final Object o = evaluate(evaluator);
if (o == null) {
return FunUtil.DoubleNull;
}
if (o instanceof String) {
return Double.valueOf((String) o);
}
Expand All @@ -89,6 +96,9 @@ public double evaluateDouble(Evaluator evaluator) {

public boolean evaluateBoolean(Evaluator evaluator) {
final Object o = evaluate(evaluator);
if (o == null) {
return FunUtil.BooleanNull;
}
if (o instanceof Boolean) {
return (Boolean) o;
}
Expand Down
21 changes: 10 additions & 11 deletions testsrc/main/mondrian/olap/fun/FunctionTest.java
Expand Up @@ -6771,18 +6771,17 @@ public void testCast() {
// To boolean (trivial)
assertExprReturns("1=1 AND Cast(1 = 1 AND 1 = 2 AS Boolean)", "false");

// From null
// To Integer
assertExprThrows("0 + Cast(NULL AS Integer)",
"cannot convert value 'null' to targetType 'DECIMAL(0)'");
// To Numeric
assertExprThrows("0 + Cast(NULL AS Numeric)",
"cannot convert value 'null' to targetType 'NUMERIC'");
// To String
// From null : should not throw exceptions since RolapResult.executeBody can receive NULL
// values when the cell value is not loaded yet, so should return null instead.
// To Integer : Expect to return NULL
assertExprReturns("0 * Cast(NULL AS Integer)", ""); // Expect to return NULL
// To Numeric : Expect to return NULL
assertExprReturns("0 * Cast(NULL AS Numeric)", ""); // Expect to return NULL
// To String : Expect to return "null"
assertExprReturns("'' || Cast(NULL AS String)", "null");
// To Boolean
assertExprThrows("1=1 AND Cast(NULL AS Boolean)",
"cannot convert value 'null' to targetType 'BOOLEAN'");
// To Boolean : Expect to return NULL, but since FunUtil.BooleanNull does not
// implement three-valued boolean logic yet, this will return false
assertExprReturns("1=1 AND Cast(NULL AS Boolean)", "false");

// Double is not allowed as a type
assertExprThrows("Cast(1 AS Double)",
Expand Down

0 comments on commit 39bc261

Please sign in to comment.