Github user dennisgove commented on a diff in the pull request:
https://github.com/apache/lucene-solr/pull/171#discussion_r107805373
--- Diff:
solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DatePartEvaluator.java
---
@@ -0,0 +1,169 @@
+/*
+ * 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.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoField;
+import java.time.temporal.IsoFields;
+import java.time.temporal.TemporalAccessor;
+import java.time.temporal.UnsupportedTemporalTypeException;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import
org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides numeric Date/Time stream evaluators
+ */
+public class DatePartEvaluator extends NumberEvaluator {
+
+ public enum FUNCTION {year, month, day, dayOfYear, dayOfQuarter, hour,
minute, quarter, week, second, epoch}
+
+ private final FUNCTION function;
+
+ public DatePartEvaluator(StreamExpression expression, StreamFactory
factory) throws IOException {
+ super(expression, factory);
+
+ String functionName = expression.getFunctionName();
+
+ 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())));
+ }
+
+ 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 {
+
+ Instant instant = null;
+ TemporalAccessor date = null;
+
+ //First evaluate the parameter
+ StreamEvaluator streamEvaluator = subEvaluators.get(0);
+ Object tupleValue = streamEvaluator.evaluate(tuple);
+
+ 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 TemporalAccessor) {
+ date = ((TemporalAccessor) tupleValue);
+ }
+
+ if (instant != null) {
+ if (function.equals(FUNCTION.epoch)) return instant.toEpochMilli();
+ date = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
+ }
+
+ if (date != null) {
+ return evaluate(date);
+ }
+
+ 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) throws IOException {
+
+ if (dateStr != null && !dateStr.isEmpty()) {
+ 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
+ */
+ private Number evaluate(TemporalAccessor date) throws IOException {
--- End diff --
If you were to break this class into multiple (one for each evaluator) then
this function should def exist in a parent class so keep the logic in a
singular place.
The way I could see the hierarchy working is something like this
```
ComplexEvaluator
TemporalEvaluator
YearEvaluator
MonthOfYearEvaluator
DayOfMonthEvalator
DayNameEvaluator
....
```
Basically, if some evaluator extends NumberEvaluator then that means this
evaluator works over numbers, not that it returns a number. In a sense, when
the sub-evaluators are executed they need to return something that is a Number.
Following that logic, these evaluators work over temporal objects, so they
extend TemporalEvaluator.
They can return whatever type they need to (String, Number, etc...) but
they must have a temporal value to work from.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]