You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2019/06/19 02:33:50 UTC

[lucene-solr] 01/02: SOLR-13560: Add isNull and notNull Stream Evaluators

This is an automated email from the ASF dual-hosted git repository.

jbernste pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 1dd98ca65dd90db6998fe54b56860e90d8f398d1
Author: Joel Bernstein <jb...@apache.org>
AuthorDate: Tue Jun 18 22:09:13 2019 -0400

    SOLR-13560: Add isNull and notNull Stream Evaluators
---
 .../java/org/apache/solr/client/solrj/io/Lang.java |  2 +
 .../client/solrj/io/eval/FieldValueEvaluator.java  |  9 +++
 .../solr/client/solrj/io/eval/IsNullEvaluator.java | 54 ++++++++++++++++
 .../client/solrj/io/eval/NotNullEvaluator.java     | 54 ++++++++++++++++
 .../solr/client/solrj/io/stream/HavingStream.java  |  1 +
 .../org/apache/solr/client/solrj/io/TestLang.java  |  2 +-
 .../client/solrj/io/stream/MathExpressionTest.java | 75 ++++++++++++++++++++++
 7 files changed, 196 insertions(+), 1 deletion(-)

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.java
index 4a1051e..52b8875 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 Evaluators
 
diff --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.java
index 3086fb4..809a284 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.java
new file mode 100644
index 0000000..dc912fd
--- /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.java
new file mode 100644
index 0000000..8b9a9b1
--- /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.java
index eeb2935..bf56484 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.java
index b7db83d..a2ad7de 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"};
 
   @Test
   public 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.java
index c14e126..8c8718c 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);
+
+  }
+
   @Test
   public void testLetWithNumericVariables() throws Exception {
     String cexpr = "let(echo=true, a=1.88888, b=8888888888.98)";