From ac754fcd593fb334ea9e46677390a7afa100cd9d Mon Sep 17 00:00:00 2001 From: Dennis Gove Date: Fri, 8 Jan 2016 01:55:25 +0000 Subject: [PATCH] SOLR-8485: SelectStream now properly handles non-lowercase and/or quoted select field names git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1723648 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 1 + .../client/solrj/io/stream/SelectStream.java | 14 +++++++------ .../StreamExpressionToExpessionTest.java | 21 +++++++++++++++++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index af3ad80b2a6..335c288fe88 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -135,6 +135,7 @@ Bug Fixes * SOLR-8371: Try and prevent too many recovery requests from stacking up and clean up some faulty cancel recovery logic. (Mark Miller) +* SOLR-8485: SelectStream now properly handles non-lowercase and/or quoted select field names (Dennis Gove) Optimizations ---------------------- diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/SelectStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/SelectStream.java index ac8d68e751f..2e9d9ada1bc 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/SelectStream.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/SelectStream.java @@ -23,15 +23,12 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.function.BiConsumer; import org.apache.solr.client.solrj.io.Tuple; -import org.apache.solr.client.solrj.io.comp.FieldComparator; import org.apache.solr.client.solrj.io.comp.StreamComparator; import org.apache.solr.client.solrj.io.ops.StreamOperation; import org.apache.solr.client.solrj.io.stream.expr.Expressible; import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; -import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter; import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter; import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue; import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; @@ -89,9 +86,14 @@ public class SelectStream extends TupleStream implements Expressible { selectedFields = new HashMap(selectFieldsExpressions.size()); for(StreamExpressionParameter parameter : selectFieldsExpressions){ StreamExpressionValue selectField = (StreamExpressionValue)parameter; - String value = selectField.getValue().trim().toLowerCase(Locale.ROOT); - if(value.contains(" as ")){ - String[] parts = value.split(" as "); + String value = selectField.getValue().trim(); + + // remove possible wrapping quotes + if(value.length() > 2 && value.startsWith("\"") && value.endsWith("\"")){ + value = value.substring(1, value.length() - 1); + } + if(value.toLowerCase(Locale.ROOT).contains(" as ")){ + String[] parts = value.split("(?i) as "); // ensure we are splitting in a case-insensitive way if(2 != parts.length){ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting select field of form 'fieldA' or 'fieldA as alias' but found %s",expression, value)); } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionToExpessionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionToExpessionTest.java index 16bbb9d0749..64e600a427f 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionToExpessionTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionToExpessionTest.java @@ -44,6 +44,7 @@ public class StreamExpressionToExpessionTest extends LuceneTestCase { .withCollectionZkHost("collection1", "testhost:1234") .withCollectionZkHost("collection2", "testhost:1234") .withFunctionName("search", CloudSolrStream.class) + .withFunctionName("select", SelectStream.class) .withFunctionName("merge", MergeStream.class) .withFunctionName("unique", UniqueStream.class) .withFunctionName("top", RankStream.class) @@ -59,8 +60,7 @@ public class StreamExpressionToExpessionTest extends LuceneTestCase { .withFunctionName("avg", MeanMetric.class) ; } - - + @Test public void testCloudSolrStream() throws Exception { @@ -83,6 +83,23 @@ public class StreamExpressionToExpessionTest extends LuceneTestCase { } + @Test + public void testSelectStream() throws Exception { + + SelectStream stream; + String expressionString; + + // Basic test + stream = new SelectStream(StreamExpressionParser.parse("select(\"a_s as fieldA\", search(collection1, q=*:*, fl=\"id,a_s,a_i,a_f\", sort=\"a_f asc, a_i asc\"))"), factory); + expressionString = stream.toExpression(factory).toString(); + assertTrue(expressionString.contains("select(search(collection1,")); + assertTrue(expressionString.contains("q=\"*:*\"")); + assertTrue(expressionString.contains("fl=\"id,a_s,a_i,a_f\"")); + assertTrue(expressionString.contains("sort=\"a_f asc, a_i asc\"")); + assertTrue(expressionString.contains("a_s as fieldA")); + + } + @Test public void testStatsStream() throws Exception {