From 43c086a002b3488a6fdd6a71ce1879a2538cce4e Mon Sep 17 00:00:00 2001 From: Jason Gerlowski Date: Fri, 27 Apr 2018 09:46:11 -0400 Subject: [PATCH 1/2] SOLR-11749: Fix logic errors in some assert funcs --- solr/bin-test/utils/assert.sh | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/solr/bin-test/utils/assert.sh b/solr/bin-test/utils/assert.sh index 7c0f3236da0..a4bd56d9347 100644 --- a/solr/bin-test/utils/assert.sh +++ b/solr/bin-test/utils/assert.sh @@ -14,13 +14,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -ASSERT_SUCCEEDED=1 -ASSERT_FAILURE=0 +ASSERT_SUCCESS=0 +ASSERT_FAILURE=1 + +TEST_SUCCESS=0 +TEST_FAILURE=1 function assert_cmd_succeeded() { retval=$? - if [[ $? -ne 0 ]]; then + if [[ $retval -ne 0 ]]; then echo "Expected command $1 to succeed, but exited with $retval" return $ASSERT_FAILURE fi @@ -31,7 +34,7 @@ function assert_cmd_succeeded() { function assert_cmd_failed() { retval=$? - if [[ $? -eq 0 ]]; then + if [[ $retval -eq 0 ]]; then echo "Expected command $1 to fail, but exited with $retval" return $ASSERT_FAILURE fi @@ -67,9 +70,9 @@ function assert_collection_exists() { local coll_name=$1 local coll_list=$(bin/solr zk ls /collections -z localhost:9983) - for coll in "$coll_list"; + for coll in $coll_list; do - if [[ $(echo $coll | tr -d " ") -eq $coll_name ]]; then + if [[ $(echo $coll | tr -d " ") == $coll_name ]]; then return $ASSERT_SUCCESS fi done @@ -81,9 +84,10 @@ function assert_collection_exists() { function assert_collection_doesnt_exist() { local coll_name=$1 local coll_list=$(bin/solr zk ls /collections -z localhost:9983) - for coll in "$coll_list"; + for coll in $coll_list; do - if [[ $(echo $coll | tr -d " ") -eq $coll_name ]]; then + echo "Comparing $coll to $coll_name" + if [[ $(echo $coll | tr -d " ") == "$coll_name" ]]; then echo "Expected not to find collection [$coll_name], but it exists" return $ASSERT_FAILURE fi @@ -96,9 +100,9 @@ function assert_config_exists() { local config_name=$1 local config_list=$(bin/solr zk ls /configs -z localhost:9983) - for config in "$config_list"; + for config in $config_list; do - if [[ $(echo $config | tr -d " ") -eq $config_name ]]; then + if [[ $(echo $config | tr -d " ") == $config_name ]]; then return $ASSERT_SUCCESS fi done @@ -111,9 +115,9 @@ function assert_config_doesnt_exist() { local config_name=$1 local config_list=$(bin/solr zk ls /configs -z localhost:9983) - for config in "$config_list"; + for config in $config_list; do - if [[ $(echo $config | tr -d " ") -eq $config_name ]]; then + if [[ $(echo $config | tr -d " ") == $config_name ]]; then echo "Expected not to find config [$config_name], but it exists" return $ASSERT_FAILURE fi From 2c487947e8cd9e6583d746540451cba4e8157e45 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Fri, 27 Apr 2018 13:44:12 -0400 Subject: [PATCH 2/2] SOLR-11734: Add ones and zeros Stream Evaluators --- .../org/apache/solr/client/solrj/io/Lang.java | 4 +- .../client/solrj/io/eval/OnesEvaluator.java | 47 +++++++++++++++++++ .../client/solrj/io/eval/ZerosEvaluator.java | 47 +++++++++++++++++++ .../apache/solr/client/solrj/io/TestLang.java | 2 +- .../solrj/io/stream/MathExpressionTest.java | 45 ++++++++++++++++++ 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.java create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ZerosEvaluator.java 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 f0862db0e47..941c1a1a9f2 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 @@ -242,8 +242,10 @@ public class Lang { .withFunctionName("earthMovers", EarthMoversEvaluator.class) .withFunctionName("euclidean", EuclideanEvaluator.class) .withFunctionName("chebyshev", ChebyshevEvaluator.class) + .withFunctionName("ones", OnesEvaluator.class) + .withFunctionName("zeros", ZerosEvaluator.class) - // Boolean Stream Evaluators + // Boolean Stream Evaluators .withFunctionName("and", AndEvaluator.class) .withFunctionName("eor", ExclusiveOrEvaluator.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.java new file mode 100644 index 00000000000..bef53fd2151 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OnesEvaluator.java @@ -0,0 +1,47 @@ +/* + * 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.ArrayList; +import java.util.List; +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 OnesEvaluator extends RecursiveNumericEvaluator implements OneValueWorker { + protected static final long serialVersionUID = 1L; + + public OnesEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{ + super(expression, factory); + + if(1 != containedEvaluators.size()){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size())); + } + } + + @Override + public Object doWork(Object value){ + int size = ((Number)value).intValue(); + List ones = new ArrayList(); + for(int i=0; i ones = new ArrayList(); + for(int i=0; i tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List out = (List)tuples.get(0).get("return-value"); + assertEquals(out.size(), 6); + assertEquals(out.get(0).intValue(), 1); + assertEquals(out.get(1).intValue(), 1); + assertEquals(out.get(2).intValue(), 1); + assertEquals(out.get(3).intValue(), 1); + assertEquals(out.get(4).intValue(), 1); + assertEquals(out.get(5).intValue(), 1); + } + + @Test + public void testZeros() throws Exception { + String cexpr = "zeros(6)"; + 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 tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List out = (List)tuples.get(0).get("return-value"); + assertEquals(out.size(), 6); + assertEquals(out.get(0).intValue(), 0); + assertEquals(out.get(1).intValue(), 0); + assertEquals(out.get(2).intValue(), 0); + assertEquals(out.get(3).intValue(), 0); + assertEquals(out.get(4).intValue(), 0); + assertEquals(out.get(5).intValue(), 0); + } + @Test public void testMatrix() throws Exception { String cexpr = "let(echo=true," +