You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2017/04/12 19:14:59 UTC

[04/12] lucene-solr:master: SOLR-10303: Switching from the fieldName param to subEvaluators

SOLR-10303:  Switching from the fieldName param to subEvaluators


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c3d205cd
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c3d205cd
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c3d205cd

Branch: refs/heads/master
Commit: c3d205cdccf1175d9960723d0fe4e0d4391dff11
Parents: b13945b
Author: Gethin James <ge...@alfresco.com>
Authored: Fri Mar 17 18:11:00 2017 +0100
Committer: Joel Bernstein <jb...@apache.org>
Committed: Wed Apr 12 15:00:06 2017 -0400

----------------------------------------------------------------------
 .../client/solrj/io/eval/DatePartEvaluator.java | 84 ++++++++++----------
 .../io/stream/eval/DatePartEvaluatorTest.java   |  4 +-
 2 files changed, 45 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c3d205cd/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.java
----------------------------------------------------------------------
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.java
index 1423364..e18de3e 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()));
     }
   }
 
   @Override
   public 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 {
 
   @Override
   public 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;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c3d205cd/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.java
----------------------------------------------------------------------
diff --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.java
index 307575c..6240bf8 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();