mirror of https://github.com/apache/lucene.git
SOLR-13560: Add isNull and notNull Stream Evaluators
This commit is contained in:
parent
aad7b96128
commit
1dd98ca65d
|
@ -287,6 +287,8 @@ public class Lang {
|
||||||
.withFunctionName("natural", NaturalEvaluator.class)
|
.withFunctionName("natural", NaturalEvaluator.class)
|
||||||
.withFunctionName("movingMAD", MovingMADEvaluator.class)
|
.withFunctionName("movingMAD", MovingMADEvaluator.class)
|
||||||
.withFunctionName("recNum", RecNumEvaluator.class)
|
.withFunctionName("recNum", RecNumEvaluator.class)
|
||||||
|
.withFunctionName("notNull", NotNullEvaluator.class)
|
||||||
|
.withFunctionName("isNull", IsNullEvaluator.class)
|
||||||
|
|
||||||
// Boolean Stream Evaluators
|
// Boolean Stream Evaluators
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,12 @@ package org.apache.solr.client.solrj.io.eval;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.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;
|
||||||
import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
|
import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
|
||||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
|
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(value == null) {
|
||||||
|
if(sc != null) {sc.getTupleContext().put("null", fieldName);}
|
||||||
return fieldName;
|
return fieldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -132,6 +132,7 @@ public class HavingStream extends TupleStream implements Expressible {
|
||||||
public void setStreamContext(StreamContext context) {
|
public void setStreamContext(StreamContext context) {
|
||||||
this.streamContext = context;
|
this.streamContext = context;
|
||||||
this.stream.setStreamContext(context);
|
this.stream.setStreamContext(context);
|
||||||
|
this.evaluator.setStreamContext(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TupleStream> children() {
|
public List<TupleStream> children() {
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class TestLang extends SolrTestCase {
|
||||||
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
|
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
|
||||||
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
|
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
|
||||||
"getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export",
|
"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
|
@Test
|
||||||
public void testLang() {
|
public void testLang() {
|
||||||
|
|
|
@ -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
|
@Test
|
||||||
public void testLetWithNumericVariables() throws Exception {
|
public void testLetWithNumericVariables() throws Exception {
|
||||||
String cexpr = "let(echo=true, a=1.88888, b=8888888888.98)";
|
String cexpr = "let(echo=true, a=1.88888, b=8888888888.98)";
|
||||||
|
|
Loading…
Reference in New Issue