Apache Solr open-source search software
SOLR-13560: Add isNull and notNull Stream Evaluators
Joel Bernstein1dd98ca65dd9
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.javaindex 4a1051e61c7..52b88757591 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java@@ -287,6 +287,8 @@ public class Lang {.withFunctionName("natural", NaturalEvaluator.class).withFunctionName("movingMAD", MovingMADEvaluator.class).withFunctionName("recNum", RecNumEvaluator.class)+ .withFunctionName("notNull", NotNullEvaluator.class)+ .withFunctionName("isNull", IsNullEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.javaindex 3086fb44213..809a2840342 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java@@ -19,8 +19,12 @@ package org.apache.solr.client.solrj.io.eval;import java.io.IOException;import java.util.ArrayList;import java.util.List;+import java.util.Map;+import java.util.Set;+import java.util.HashSet;import org.apache.solr.client.solrj.io.Tuple;+import org.apache.solr.client.solrj.io.stream.StreamContext;import org.apache.solr.client.solrj.io.stream.expr.Explanation;import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;@@ -84,7 +88,12 @@ public class FieldValueEvaluator extends SourceEvaluator {}}+ StreamContext sc = getStreamContext();++ if(sc != null) {sc.getTupleContext().remove("null");}+if(value == null) {+ if(sc != null) {sc.getTupleContext().put("null", fieldName);}return fieldName;}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IsNullEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IsNullEvaluator.javanew file mode 100644index 00000000000..dc912fd3a7b--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/IsNullEvaluator.java@@ -0,0 +1,54 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.solrj.io.eval;++import java.io.IOException;+import java.util.Map;+import java.util.Locale;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class IsNullEvaluator extends RecursiveBooleanEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public IsNullEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);++ if(containedEvaluators.size() != 1){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting one parameter but found %d",expression,containedEvaluators.size()));+ }+ }++ public Object doWork(Object ... values) throws IOException {++ if(values[0] instanceof String) {+ //Check to see if the this tuple had a null value for that string.+ Map tupleContext = getStreamContext().getTupleContext();+ String nullField = (String)tupleContext.get("null");+ if(nullField != null && nullField.equals(values[0])) {+ return true;+ }+ }++ return false;+ }++ protected Checker constructChecker(Object value) throws IOException {+ return null;+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NotNullEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NotNullEvaluator.javanew file mode 100644index 00000000000..8b9a9b10b87--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NotNullEvaluator.java@@ -0,0 +1,54 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.solrj.io.eval;++import java.io.IOException;+import java.util.Map;+import java.util.Locale;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class NotNullEvaluator extends RecursiveBooleanEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public NotNullEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);++ if(containedEvaluators.size() != 1){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting one parameter but found %d",expression,containedEvaluators.size()));+ }+ }++ public Object doWork(Object ... values) throws IOException {++ if(values[0] instanceof String) {+ //Check to see if the this tuple had a null value for that string.+ Map tupleContext = getStreamContext().getTupleContext();+ String nullField = (String)tupleContext.get("null");+ if(nullField != null && nullField.equals(values[0])) {+ return false;+ }+ }++ return true;+ }++ protected Checker constructChecker(Object value) throws IOException {+ return null;+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/HavingStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/HavingStream.javaindex eeb29359982..bf564844ab3 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/HavingStream.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/HavingStream.java@@ -132,6 +132,7 @@ public class HavingStream extends TupleStream implements Expressible {public void setStreamContext(StreamContext context) {this.streamContext = context;this.stream.setStreamContext(context);+ this.evaluator.setStreamContext(context);}public List<TupleStream> children() {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.javaindex b7db83d762f..a2ad7debb4e 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java@@ -75,7 +75,7 @@ public class TestLang extends SolrTestCase {"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate","getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius","getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export",- "zplot", "natural", "repeat", "movingMAD", "hashRollup", "noop", "var", "stddev", "recNum"};+ "zplot", "natural", "repeat", "movingMAD", "hashRollup", "noop", "var", "stddev", "recNum", "isNull", "notNull"};@Testpublic void testLang() {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.javaindex c14e126a761..8c8718c2b95 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java@@ -1997,6 +1997,81 @@ public class MathExpressionTest extends SolrCloudTestCase {}++ @Test+ public void testNotNullHaving() throws Exception {+ String cexpr = "having(list(tuple(a=add(1, 1)), tuple(b=add(1, 2))), notNull(b))";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertEquals(tuples.size(), 1);+ Tuple tuple0 = tuples.get(0);+ assertEquals(tuple0.getLong("b").longValue(), 3L);+ }++ @Test+ public void testIsNullHaving() throws Exception {+ String cexpr = "having(list(tuple(a=add(1, 1)), tuple(b=add(1, 2))), isNull(b))";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertEquals(tuples.size(), 1);+ Tuple tuple0 = tuples.get(0);+ assertEquals(tuple0.getLong("a").longValue(), 2L);+ }+++ @Test+ public void testNotNullSelect() throws Exception {+ String cexpr = "select(list(tuple(a=add(1, 1)), tuple(b=add(1, 2))), if(notNull(a),a, 0) as out)";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertEquals(tuples.size(), 2);+ Tuple tuple0 = tuples.get(0);+ assertEquals(tuple0.getLong("out").longValue(), 2L);++ Tuple tuple1 = tuples.get(1);+ assertEquals(tuple1.getLong("out").longValue(), 0L);++ }+++ @Test+ public void testIsNullSelect() throws Exception {+ String cexpr = "select(list(tuple(a=add(1, 1)), tuple(b=add(1, 2))), if(isNull(a), 0, a) as out)";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertEquals(tuples.size(), 2);+ Tuple tuple0 = tuples.get(0);+ assertEquals(tuple0.getLong("out").longValue(), 2L);++ Tuple tuple1 = tuples.get(1);+ assertEquals(tuple1.getLong("out").longValue(), 0L);++ }+@Testpublic void testLetWithNumericVariables() throws Exception {String cexpr = "let(echo=true, a=1.88888, b=8888888888.98)";