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 a1a796d1ecf..9229dcf9370 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 @@ -279,6 +279,8 @@ public class Lang { .withFunctionName("pivot", PivotEvaluator.class) .withFunctionName("ltrim", LeftShiftEvaluator.class) .withFunctionName("rtrim", RightShiftEvaluator.class) + .withFunctionName("repeat", RepeatEvaluator.class) + .withFunctionName("natural", NaturalEvaluator.class) // Boolean Stream Evaluators diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.java new file mode 100644 index 00000000000..f8469eea2a6 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalEvaluator.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 NaturalEvaluator extends RecursiveNumericEvaluator implements OneValueWorker { + protected static final long serialVersionUID = 1L; + + public NaturalEvaluator(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 natural = ((Number)value).intValue(); + List naturals = new ArrayList(); + for(int i=0; i repeated = 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(), 0); + assertEquals(out.get(1).intValue(), 1); + assertEquals(out.get(2).intValue(), 2); + assertEquals(out.get(3).intValue(), 3); + assertEquals(out.get(4).intValue(), 4); + assertEquals(out.get(5).intValue(), 5); + } + + + @Test + public void testRepeat() throws Exception { + String cexpr = "repeat(6.5, 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).doubleValue(), 6.5, 0); + assertEquals(out.get(1).doubleValue(), 6.5, 0); + assertEquals(out.get(2).doubleValue(), 6.5, 0); + assertEquals(out.get(3).doubleValue(), 6.5, 0); + assertEquals(out.get(4).doubleValue(), 6.5, 0); + assertEquals(out.get(5).doubleValue(), 6.5, 0); + } + + @Test public void testLtrim() throws Exception { String cexpr = "ltrim(array(1,2,3,4,5,6), 2)";