Improve foldAnd's behavior when one or more conditions are null.
Add space after unary operator to prevent "-(-5)" becoming "--5".
diff --git a/pom.xml b/pom.xml
index 1caed25..12b054e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
<groupId>net.hydromatic</groupId>
<artifactId>linq4j</artifactId>
<packaging>jar</packaging>
- <version>TRUNK-SNAPSHOT</version>
+ <version>0.1.9-SNAPSHOT</version>
<!-- More project information. -->
<name>linq4j</name>
diff --git a/src/main/java/net/hydromatic/linq4j/expressions/ExpressionType.java b/src/main/java/net/hydromatic/linq4j/expressions/ExpressionType.java
index 81e25eb..439c67f 100644
--- a/src/main/java/net/hydromatic/linq4j/expressions/ExpressionType.java
+++ b/src/main/java/net/hydromatic/linq4j/expressions/ExpressionType.java
@@ -237,7 +237,7 @@
* An arithmetic negation operation, such as (-a). The object
* a should not be modified in place.
*/
- Negate("-", false, 2, true),
+ Negate("- ", false, 2, true),
/**
* A unary plus operation, such as (+a). The result of a
@@ -245,7 +245,7 @@
* operand, but user-defined implementations might have
* unusual results.
*/
- UnaryPlus("+", false, 2, true),
+ UnaryPlus("+ ", false, 2, true),
/**
* An arithmetic negation operation, such as (-a), that has
diff --git a/src/main/java/net/hydromatic/linq4j/expressions/Expressions.java b/src/main/java/net/hydromatic/linq4j/expressions/Expressions.java
index ca2bcdc..6c2db22 100644
--- a/src/main/java/net/hydromatic/linq4j/expressions/Expressions.java
+++ b/src/main/java/net/hydromatic/linq4j/expressions/Expressions.java
@@ -2910,13 +2910,20 @@
return new GotoStatement(GotoExpressionKind.Sequence, null, expression);
}
- /** Combines a list of expressions using AND. Returns TRUE if the list is
- * empty. */
+ /** Combines a list of expressions using AND.
+ * Returns TRUE if the list is empty.
+ * Returns FALSE if any of the conditions are constant FALSE;
+ * otherwise returns NULL if any of the conditions are constant NULL. */
public static Expression foldAnd(List<Expression> conditions) {
Expression e = null;
+ int nullCount = 0;
for (Expression condition : conditions) {
if (condition instanceof ConstantExpression) {
- if ((Boolean) ((ConstantExpression) condition).value) {
+ final Boolean value = (Boolean) ((ConstantExpression) condition).value;
+ if (value == null) {
+ ++nullCount;
+ continue;
+ } else if (value) {
continue;
} else {
return constant(false);
@@ -2928,19 +2935,29 @@
e = andAlso(e, condition);
}
}
+ if (nullCount > 0) {
+ return constant(null);
+ }
if (e == null) {
return constant(true);
}
return e;
}
- /** Combines a list of expressions using OR. Returns FALSE if the list is
- * empty. */
+ /** Combines a list of expressions using OR.
+ * Returns FALSE if the list is empty.
+ * Returns TRUE if any of the conditions are constant TRUE;
+ * otherwise returns NULL if all of the conditions are constant NULL. */
public static Expression foldOr(List<Expression> conditions) {
Expression e = null;
+ int nullCount = 0;
for (Expression condition : conditions) {
if (condition instanceof ConstantExpression) {
- if ((Boolean) ((ConstantExpression) condition).value) {
+ final Boolean value = (Boolean) ((ConstantExpression) condition).value;
+ if (value == null) {
+ ++nullCount;
+ continue;
+ } else if (value) {
return constant(true);
} else {
continue;
@@ -2953,6 +2970,9 @@
}
}
if (e == null) {
+ if (nullCount > 0) {
+ return constant(null);
+ }
return constant(false);
}
return e;
diff --git a/src/main/java/net/hydromatic/linq4j/expressions/Types.java b/src/main/java/net/hydromatic/linq4j/expressions/Types.java
index 925878b..8951e04 100644
--- a/src/main/java/net/hydromatic/linq4j/expressions/Types.java
+++ b/src/main/java/net/hydromatic/linq4j/expressions/Types.java
@@ -344,7 +344,9 @@
}
}
throw new RuntimeException(
- "while resolving method '" + methodName + "' in class " + clazz, e);
+ "while resolving method '" + methodName
+ + Arrays.toString(argumentTypes)
+ + "' in class " + clazz, e);
}
}
diff --git a/src/test/java/net/hydromatic/linq4j/test/ExpressionTest.java b/src/test/java/net/hydromatic/linq4j/test/ExpressionTest.java
index 30dbfe8..03f5a6c 100644
--- a/src/test/java/net/hydromatic/linq4j/test/ExpressionTest.java
+++ b/src/test/java/net/hydromatic/linq4j/test/ExpressionTest.java
@@ -285,6 +285,14 @@
Expressions.constant(2), Expressions.constant(3)),
Double.TYPE))));
+ // "--5" would be a syntax error
+ assertEquals(
+ "- - 5",
+ Expressions.toString(
+ Expressions.negate(
+ Expressions.negate(
+ Expressions.constant(5)))));
+
assertEquals(
"a.empno",
Expressions.toString(