Apache Lucene open-source search software
SOLR-10303: Switching from the fieldName param to subEvaluators
Gethin Jamesc3d205cdccf1
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.javaindex 1423364b14a..e18de3e4f03 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.java@@ -38,10 +38,9 @@ import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;*/public class DatePartEvaluator extends NumberEvaluator {- public enum FUNCTION {year, month, day, dayofyear, dayofquarter, hour, minute, quarter, week, second, epoch};+ public enum FUNCTION {year, month, day, dayofyear, dayofquarter, hour, minute, quarter, week, second, epoch}private FUNCTION function;- private String fieldName;public DatePartEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {super(expression, factory);@@ -51,66 +50,63 @@ public class DatePartEvaluator extends NumberEvaluator {try {this.function = FUNCTION.valueOf(functionName);} catch (IllegalArgumentException e) {- throw new IOException(String.format(Locale.ROOT,"Invalid date expression %s - expecting one of %s",functionName, Arrays.toString(FUNCTION.values())));+ throw new IOException(String.format(Locale.ROOT, "Invalid date expression %s - expecting one of %s", functionName, Arrays.toString(FUNCTION.values())));}- fieldName = factory.getValueOperand(expression, 0);-- //Taken from Field evaluator- if(fieldName != null && fieldName.startsWith("'") && fieldName.endsWith("'") && fieldName.length() > 1){- fieldName = fieldName.substring(1, fieldName.length() - 1);- }-- if(1 != subEvaluators.size()){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting one value but found %d",expression,subEvaluators.size()));+ if (1 != subEvaluators.size()) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - expecting one value but found %d", expression, subEvaluators.size()));}}@Overridepublic Number evaluate(Tuple tuple) throws IOException {- try {- Object fieldValue = tuple.get(fieldName);- Instant instant = null;- LocalDateTime date = null;-- if (fieldValue == null) return null;-- if (fieldValue instanceof String) {- instant = getInstant((String)fieldValue);- } else if (fieldValue instanceof Instant) {- instant = (Instant) fieldValue;- } else if (fieldValue instanceof Date) {- instant = ((Date) fieldValue).toInstant();- } else if (fieldValue instanceof LocalDateTime) {- date = ((LocalDateTime) fieldValue);- }+ Instant instant = null;+ LocalDateTime date = null;- if (instant != null) {- if (function.equals(FUNCTION.epoch)) return instant.toEpochMilli();- date = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);- }+ //First evaluate the parameter+ StreamEvaluator streamEvaluator = subEvaluators.get(0);+ Object tupleValue = streamEvaluator.evaluate(tuple);- if (date != null) {- return evaluate(date);- }+ if (tupleValue == null) return null;++ if (tupleValue instanceof String) {+ instant = getInstant((String) tupleValue);+ } else if (tupleValue instanceof Instant) {+ instant = (Instant) tupleValue;+ } else if (tupleValue instanceof Date) {+ instant = ((Date) tupleValue).toInstant();+ } else if (tupleValue instanceof LocalDateTime) {+ date = ((LocalDateTime) tupleValue);+ }++ if (instant != null) {+ if (function.equals(FUNCTION.epoch)) return instant.toEpochMilli();+ date = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);+ }- } catch (DateTimeParseException e) {- throw new IOException(String.format(Locale.ROOT,"Invalid field %s - The field must be a string formatted in the ISO_INSTANT date format.",fieldName));+ if (date != null) {+ return evaluate(date);}- throw new IOException(String.format(Locale.ROOT,"Invalid field %s - The field must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.",fieldName));+ throw new IOException(String.format(Locale.ROOT, "Invalid parameter %s - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", String.valueOf(tupleValue)));}- private Instant getInstant(String dateStr) {+ private Instant getInstant(String dateStr) throws IOException {+if (dateStr != null && !dateStr.isEmpty()) {- return Instant.parse(dateStr);+ try {+ return Instant.parse(dateStr);+ } catch (DateTimeParseException e) {+ throw new IOException(String.format(Locale.ROOT, "Invalid parameter %s - The String must be formatted in the ISO_INSTANT date format.", dateStr));+ }}return null;}/*** Evaluate the date based on the specified function+ ** @param date* @return the evaluated value*/@@ -142,7 +138,13 @@ public class DatePartEvaluator extends NumberEvaluator {@Overridepublic StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {- return new StreamExpression(function.toString()).withParameter(fieldName);+ StreamExpression expression = new StreamExpression(function.toString());++ for (StreamEvaluator evaluator : subEvaluators) {+ expression.addParameter(evaluator.toExpression(factory));+ }++ return expression;}@Overridediff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.javaindex 307575c8fce..6240bf828ae 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.java@@ -109,7 +109,7 @@ public class DatePartEvaluatorTest {Object result = evaluator.evaluate(new Tuple(values));assertTrue(false);} catch (Exception e) {- assertEquals("Invalid field a - The field must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());+ assertEquals("Invalid parameter 12 - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());}try {@@ -118,7 +118,7 @@ public class DatePartEvaluatorTest {Object result = evaluator.evaluate(new Tuple(values));assertTrue(false);} catch (Exception e) {- assertEquals("Invalid field a - The field must be a string formatted in the ISO_INSTANT date format.", e.getMessage());+ assertEquals("Invalid parameter 1995-12-31 - The String must be formatted in the ISO_INSTANT date format.", e.getMessage());}values.clear();