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:15:02 UTC
[07/12] lucene-solr:master: SOLR-10303: Refactored to multiple
TemporalEvaluator classes based on feedback
SOLR-10303: Refactored to multiple TemporalEvaluator classes based on feedback
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/d70fc967
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/d70fc967
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/d70fc967
Branch: refs/heads/master
Commit: d70fc967d44fd0a7e3706707479853dfd43ea908
Parents: b314bc6
Author: Gethin James <ge...@alfresco.com>
Authored: Thu Apr 6 11:58:26 2017 +0200
Committer: Joel Bernstein <jb...@apache.org>
Committed: Wed Apr 12 15:04:51 2017 -0400
----------------------------------------------------------------------
.../org/apache/solr/handler/StreamHandler.java | 33 +-
.../client/solrj/io/eval/TemporalEvaluator.java | 122 ++++++++
.../solrj/io/eval/TemporalEvaluatorDay.java | 48 +++
.../io/eval/TemporalEvaluatorDayOfQuarter.java | 48 +++
.../io/eval/TemporalEvaluatorDayOfYear.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorEpoch.java | 54 ++++
.../solrj/io/eval/TemporalEvaluatorHour.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorMinute.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorMonth.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorQuarter.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorSecond.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorWeek.java | 48 +++
.../solrj/io/eval/TemporalEvaluatorYear.java | 48 +++
.../io/stream/eval/DatePartEvaluatorTest.java | 274 ----------------
.../io/stream/eval/TemporalEvaluatorsTest.java | 309 +++++++++++++++++++
15 files changed, 991 insertions(+), 281 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index 1f087b6..8ec5536 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -43,7 +43,6 @@ import org.apache.solr.client.solrj.io.eval.CeilingEvaluator;
import org.apache.solr.client.solrj.io.eval.CoalesceEvaluator;
import org.apache.solr.client.solrj.io.eval.CosineEvaluator;
import org.apache.solr.client.solrj.io.eval.CubedRootEvaluator;
-import org.apache.solr.client.solrj.io.eval.DatePartEvaluator;
import org.apache.solr.client.solrj.io.eval.DivideEvaluator;
import org.apache.solr.client.solrj.io.eval.EqualsEvaluator;
import org.apache.solr.client.solrj.io.eval.ExclusiveOrEvaluator;
@@ -86,6 +85,17 @@ import org.apache.solr.client.solrj.io.stream.metrics.MaxMetric;
import org.apache.solr.client.solrj.io.stream.metrics.MeanMetric;
import org.apache.solr.client.solrj.io.stream.metrics.MinMetric;
import org.apache.solr.client.solrj.io.stream.metrics.SumMetric;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDay;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDayOfQuarter;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDayOfYear;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorEpoch;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorHour;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorMinute;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorMonth;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorQuarter;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorSecond;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorWeek;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorYear;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
@@ -223,7 +233,20 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("lteq", LessThanEqualToEvaluator.class)
.withFunctionName("not", NotEvaluator.class)
.withFunctionName("or", OrEvaluator.class)
-
+
+ // Date Time Evaluators
+ .withFunctionName(TemporalEvaluatorYear.FUNCTION_NAME, TemporalEvaluatorYear.class)
+ .withFunctionName(TemporalEvaluatorMonth.FUNCTION_NAME, TemporalEvaluatorMonth.class)
+ .withFunctionName(TemporalEvaluatorDay.FUNCTION_NAME, TemporalEvaluatorDay.class)
+ .withFunctionName(TemporalEvaluatorDayOfYear.FUNCTION_NAME, TemporalEvaluatorDayOfYear.class)
+ .withFunctionName(TemporalEvaluatorHour.FUNCTION_NAME, TemporalEvaluatorHour.class)
+ .withFunctionName(TemporalEvaluatorMinute.FUNCTION_NAME, TemporalEvaluatorMinute.class)
+ .withFunctionName(TemporalEvaluatorSecond.FUNCTION_NAME, TemporalEvaluatorSecond.class)
+ .withFunctionName(TemporalEvaluatorEpoch.FUNCTION_NAME, TemporalEvaluatorEpoch.class)
+ .withFunctionName(TemporalEvaluatorWeek.FUNCTION_NAME, TemporalEvaluatorWeek.class)
+ .withFunctionName(TemporalEvaluatorQuarter.FUNCTION_NAME, TemporalEvaluatorQuarter.class)
+ .withFunctionName(TemporalEvaluatorDayOfQuarter.FUNCTION_NAME, TemporalEvaluatorDayOfQuarter.class)
+
// Number Stream Evaluators
.withFunctionName("abs", AbsoluteValueEvaluator.class)
.withFunctionName("add", AddEvaluator.class)
@@ -250,16 +273,12 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("coalesce", CoalesceEvaluator.class)
.withFunctionName("uuid", UuidEvaluator.class)
+
// Conditional Stream Evaluators
.withFunctionName("if", IfThenElseEvaluator.class)
.withFunctionName("analyze", AnalyzeEvaluator.class)
;
- // Date evaluators
- for (DatePartEvaluator.FUNCTION function: DatePartEvaluator.FUNCTION.values()) {
- streamFactory.withFunctionName(function.toString(), DatePartEvaluator.class);
- }
-
// This pulls all the overrides and additions from the config
List<PluginInfo> pluginInfos = core.getSolrConfig().getPluginInfos(Expressible.class.getName());
for (PluginInfo pluginInfo : pluginInfos) {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluator.java
new file mode 100644
index 0000000..884653f
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluator.java
@@ -0,0 +1,122 @@
+/*
+ * 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.TemporalAccessor;
+import java.time.temporal.UnsupportedTemporalTypeException;
+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;
+
+/**
+ * A generic date evaluator for use with a TemporalAccessor
+ */
+public abstract class TemporalEvaluator extends ComplexEvaluator {
+
+ public TemporalEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+
+ 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 Object 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 Long) {
+ instant = Instant.ofEpochMilli((Long)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 (TemporalEvaluatorEpoch.FUNCTION_NAME.equals(getFunction())) return instant.toEpochMilli();
+ date = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
+ }
+
+ if (date != null) {
+ try {
+ return evaluateDate(date);
+ } catch (UnsupportedTemporalTypeException utte) {
+ throw new IOException(String.format(Locale.ROOT, "It is not possible to call '%s' function on %s", getFunction(), date.getClass().getName()));
+ }
+ }
+
+ 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)));
+ }
+
+ public abstract Object evaluateDate(TemporalAccessor aDate) throws IOException;
+ public abstract String getFunction();
+
+ protected 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;
+ }
+
+ @Override
+ public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {
+ StreamExpression expression = new StreamExpression(getFunction());
+
+ for (StreamEvaluator evaluator : subEvaluators) {
+ expression.addParameter(evaluator.toExpression(factory));
+ }
+
+ return expression;
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) throws IOException {
+ return new Explanation(nodeId.toString())
+ .withExpressionType(Explanation.ExpressionType.EVALUATOR)
+ .withImplementingClass(getClass().getName())
+ .withExpression(toExpression(factory).toString());
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDay.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDay.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDay.java
new file mode 100644
index 0000000..88b5043
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDay.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a day stream evaluator
+ */
+public class TemporalEvaluatorDay extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "day";
+
+ public TemporalEvaluatorDay(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.DAY_OF_MONTH);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfQuarter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfQuarter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfQuarter.java
new file mode 100644
index 0000000..f034377
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfQuarter.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.IsoFields;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a dayOfQuarter stream evaluator
+ */
+public class TemporalEvaluatorDayOfQuarter extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "dayOfQuarter";
+
+ public TemporalEvaluatorDayOfQuarter(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(IsoFields.DAY_OF_QUARTER);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfYear.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfYear.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfYear.java
new file mode 100644
index 0000000..339f938
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorDayOfYear.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a dayOfYear stream evaluator
+ */
+public class TemporalEvaluatorDayOfYear extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "dayOfYear";
+
+ public TemporalEvaluatorDayOfYear(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.DAY_OF_YEAR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorEpoch.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorEpoch.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorEpoch.java
new file mode 100644
index 0000000..bea4f5d
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorEpoch.java
@@ -0,0 +1,54 @@
+/*
+ * 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.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a epoch stream evaluator
+ */
+public class TemporalEvaluatorEpoch extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "epoch";
+
+ public TemporalEvaluatorEpoch(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) throws IOException {
+ if (aDate instanceof LocalDateTime) {
+ return ((LocalDateTime)aDate).atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
+ }
+ throw new IOException(String.format(Locale.ROOT, "Unsupported function '%s' called on %s", FUNCTION_NAME, aDate.toString()));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorHour.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorHour.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorHour.java
new file mode 100644
index 0000000..5480870
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorHour.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a hour stream evaluator
+ */
+public class TemporalEvaluatorHour extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "hour";
+
+ public TemporalEvaluatorHour(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.HOUR_OF_DAY);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMinute.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMinute.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMinute.java
new file mode 100644
index 0000000..9438e9d
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMinute.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a minute stream evaluator
+ */
+public class TemporalEvaluatorMinute extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "minute";
+
+ public TemporalEvaluatorMinute(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.MINUTE_OF_HOUR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMonth.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMonth.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMonth.java
new file mode 100644
index 0000000..b9a70fe
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorMonth.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a month stream evaluator
+ */
+public class TemporalEvaluatorMonth extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "month";
+
+ public TemporalEvaluatorMonth(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.MONTH_OF_YEAR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorQuarter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorQuarter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorQuarter.java
new file mode 100644
index 0000000..0144311
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorQuarter.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.IsoFields;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a quarter stream evaluator
+ */
+public class TemporalEvaluatorQuarter extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "quarter";
+
+ public TemporalEvaluatorQuarter(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(IsoFields.QUARTER_OF_YEAR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorSecond.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorSecond.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorSecond.java
new file mode 100644
index 0000000..f5b71fc
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorSecond.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a second stream evaluator
+ */
+public class TemporalEvaluatorSecond extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "second";
+
+ public TemporalEvaluatorSecond(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.SECOND_OF_MINUTE);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorWeek.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorWeek.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorWeek.java
new file mode 100644
index 0000000..1a2974a
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorWeek.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.IsoFields;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a week stream evaluator
+ */
+public class TemporalEvaluatorWeek extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "week";
+
+ public TemporalEvaluatorWeek(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorYear.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorYear.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorYear.java
new file mode 100644
index 0000000..0b8d69c
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TemporalEvaluatorYear.java
@@ -0,0 +1,48 @@
+/*
+ * 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.temporal.ChronoField;
+import java.time.temporal.TemporalAccessor;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+/**
+ * Provides a year stream evaluator
+ */
+public class TemporalEvaluatorYear extends TemporalEvaluator {
+
+ public static final String FUNCTION_NAME = "year";
+
+ public TemporalEvaluatorYear(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public String getFunction() {
+ return FUNCTION_NAME;
+ }
+
+ @Override
+ public Object evaluateDate(TemporalAccessor aDate) {
+ return aDate.get(ChronoField.YEAR);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/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
deleted file mode 100644
index 8b3317e..0000000
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/DatePartEvaluatorTest.java
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- * 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.stream.eval;
-
-import java.io.IOException;
-import java.time.Instant;
-import java.time.LocalDateTime;
-import java.time.MonthDay;
-import java.time.YearMonth;
-import java.time.ZoneOffset;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.Map;
-import java.util.TimeZone;
-
-import org.apache.commons.collections.map.HashedMap;
-import org.apache.solr.client.solrj.io.Tuple;
-import org.apache.solr.client.solrj.io.eval.DatePartEvaluator;
-import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
-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.StreamExpressionParser;
-import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
-import org.junit.Test;
-
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertTrue;
-
-/**
- * Tests numeric Date/Time stream evaluators
- */
-public class DatePartEvaluatorTest {
-
-
- StreamFactory factory;
- Map<String, Object> values;
-
- public DatePartEvaluatorTest() {
- super();
-
- factory = new StreamFactory();
-
- factory.withFunctionName("nope", DatePartEvaluator.class);
- for (DatePartEvaluator.FUNCTION function : DatePartEvaluator.FUNCTION.values()) {
- factory.withFunctionName(function.toString(), DatePartEvaluator.class);
- }
- values = new HashedMap();
- }
-
- @Test
- public void testInvalidExpression() throws Exception {
-
- StreamEvaluator evaluator;
-
- try {
- evaluator = factory.constructEvaluator("nope(a)");
- evaluator.evaluate(new Tuple(null));
- assertTrue(false);
- } catch (IOException e) {
- assertTrue(e.getCause().getCause().getMessage().contains("Invalid date expression nope"));
- assertTrue(e.getCause().getCause().getMessage().contains("expecting one of [year, month, day"));
- }
-
- try {
- evaluator = factory.constructEvaluator("week()");
- assertTrue(false);
- } catch (IOException e) {
- assertTrue(e.getCause().getCause().getMessage().contains("Invalid expression week()"));
- }
-
- try {
- evaluator = factory.constructEvaluator("week(a, b)");
- assertTrue(false);
- } catch (IOException e) {
- assertTrue(e.getCause().getCause().getMessage().contains("expecting one value but found 2"));
- }
-
- try {
- evaluator = factory.constructEvaluator("Week()");
- assertTrue(false);
- } catch (IOException e) {
- assertTrue(e.getMessage().contains("Invalid evaluator expression Week() - function 'Week' is unknown"));
- }
- }
-
-
- @Test
- public void testInvalidValues() throws Exception {
- StreamEvaluator evaluator = factory.constructEvaluator("year(a)");
-
- try {
- values.clear();
- values.put("a", 12);
- Object result = evaluator.evaluate(new Tuple(values));
- assertTrue(false);
- } catch (IOException e) {
- assertEquals("Invalid parameter 12 - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());
- }
-
- try {
- values.clear();
- values.put("a", "1995-12-31");
- Object result = evaluator.evaluate(new Tuple(values));
- assertTrue(false);
- } catch (IOException e) {
- assertEquals("Invalid parameter 1995-12-31 - The String must be formatted in the ISO_INSTANT date format.", e.getMessage());
- }
-
- try {
- values.clear();
- values.put("a", "");
- Object result = evaluator.evaluate(new Tuple(values));
- assertTrue(false);
- } catch (IOException e) {
- assertEquals("Invalid parameter - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());
- }
-
- values.clear();
- values.put("a", null);
- assertNull(evaluator.evaluate(new Tuple(values)));
- }
-
- @Test
- public void testAllFunctions() throws Exception {
-
- //year, month, day, dayofyear, hour, minute, quarter, week, second, epoch
- testFunction("year(a)", "1995-12-31T23:59:59Z", 1995);
- testFunction("month(a)","1995-12-31T23:59:59Z", 12);
- testFunction("day(a)", "1995-12-31T23:59:59Z", 31);
- testFunction("dayOfYear(a)", "1995-12-31T23:59:59Z", 365);
- testFunction("dayOfQuarter(a)", "1995-12-31T23:59:59Z", 92);
- testFunction("hour(a)", "1995-12-31T23:59:59Z", 23);
- testFunction("minute(a)", "1995-12-31T23:59:59Z", 59);
- testFunction("quarter(a)","1995-12-31T23:59:59Z", 4);
- testFunction("week(a)", "1995-12-31T23:59:59Z", 52);
- testFunction("second(a)", "1995-12-31T23:59:58Z", 58);
- testFunction("epoch(a)", "1995-12-31T23:59:59Z", 820454399000l);
-
- testFunction("year(a)", "2017-03-17T10:30:45Z", 2017);
- testFunction("year('a')", "2017-03-17T10:30:45Z", 2017);
- testFunction("month(a)","2017-03-17T10:30:45Z", 3);
- testFunction("day(a)", "2017-03-17T10:30:45Z", 17);
- testFunction("day('a')", "2017-03-17T10:30:45Z", 17);
- testFunction("dayOfYear(a)", "2017-03-17T10:30:45Z", 76);
- testFunction("dayOfQuarter(a)", "2017-03-17T10:30:45Z", 76);
- testFunction("hour(a)", "2017-03-17T10:30:45Z", 10);
- testFunction("minute(a)", "2017-03-17T10:30:45Z", 30);
- testFunction("quarter(a)","2017-03-17T10:30:45Z", 1);
- testFunction("week(a)", "2017-03-17T10:30:45Z", 11);
- testFunction("second(a)", "2017-03-17T10:30:45Z", 45);
- testFunction("epoch(a)", "2017-03-17T10:30:45Z", 1489746645000l);
-
- testFunction("epoch(a)", new Date(1489746645500l).toInstant().toString(), 1489746645500l);
- testFunction("epoch(a)", new Date(820454399990l).toInstant().toString(), 820454399990l);
-
- //Additionally test all functions to make sure they return a non-null number
- for (DatePartEvaluator.FUNCTION function : DatePartEvaluator.FUNCTION.values()) {
- StreamEvaluator evaluator = factory.constructEvaluator(function+"(a)");
- values.clear();
- values.put("a", "2017-03-17T10:30:45Z");
- Object result = evaluator.evaluate(new Tuple(values));
- assertNotNull(function+" should return a result",result);
- assertTrue(function+" should return a number", result instanceof Number);
- }
- }
-
- @Test
- public void testFunctionsOnDate() throws Exception {
- Calendar calendar = new GregorianCalendar(2017,12,5, 23, 59);
- calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date aDate = calendar.getTime();
- testFunction("year(a)", aDate, calendar.get(Calendar.YEAR));
- testFunction("month(a)", aDate, calendar.get(Calendar.MONTH)+1);
- testFunction("day(a)", aDate, calendar.get(Calendar.DAY_OF_MONTH));
- testFunction("hour(a)", aDate, calendar.get(Calendar.HOUR_OF_DAY));
- testFunction("minute(a)", aDate, calendar.get(Calendar.MINUTE));
- testFunction("epoch(a)", aDate, aDate.getTime());
- }
-
- @Test
- public void testFunctionsOnInstant() throws Exception {
- Calendar calendar = new GregorianCalendar(2017,12,5, 23, 59);
- calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date aDate = calendar.getTime();
- Instant instant = aDate.toInstant();
- testFunction("year(a)", instant, calendar.get(Calendar.YEAR));
- testFunction("month(a)", instant, calendar.get(Calendar.MONTH)+1);
- testFunction("day(a)", instant, calendar.get(Calendar.DAY_OF_MONTH));
- testFunction("hour(a)", instant, calendar.get(Calendar.HOUR_OF_DAY));
- testFunction("minute(a)", instant, calendar.get(Calendar.MINUTE));
- testFunction("epoch(a)", instant, aDate.getTime());
- }
-
- @Test
- public void testFunctionsLocalDateTime() throws Exception {
-
- LocalDateTime localDateTime = LocalDateTime.of(2017,12,5, 23, 59);
- Date aDate = Date.from(localDateTime.atZone(ZoneOffset.UTC).toInstant());
- testFunction("year(a)", localDateTime, 2017);
- testFunction("month(a)", localDateTime, 12);
- testFunction("day(a)", localDateTime, 5);
- testFunction("hour(a)", localDateTime, 23);
- testFunction("minute(a)", localDateTime, 59);
- testFunction("epoch(a)", localDateTime, aDate.getTime());
- }
-
- @Test
- public void testLimitedFunctions() throws Exception {
-
- MonthDay monthDay = MonthDay.of(12,5);
- testFunction("month(a)", monthDay, 12);
- testFunction("day(a)", monthDay, 5);
-
- try {
- testFunction("year(a)", monthDay, 2017);
- assertTrue(false);
- } catch (IOException e) {
- assertEquals("It is not possible to call 'year' function on java.time.MonthDay", e.getMessage());
- }
-
- YearMonth yearMonth = YearMonth.of(2018, 4);
- testFunction("month(a)", yearMonth, 4);
- testFunction("year(a)", yearMonth, 2018);
-
- try {
- testFunction("day(a)", yearMonth, 5);
- assertTrue(false);
- } catch (IOException e) {
- assertEquals("It is not possible to call 'day' function on java.time.YearMonth", e.getMessage());
- }
-
- }
-
-
- public void testFunction(String expression, Object value, Number expected) throws Exception {
- StreamEvaluator evaluator = factory.constructEvaluator(expression);
- values.clear();
- values.put("a", value);
- Object result = evaluator.evaluate(new Tuple(values));
- assertTrue(result instanceof Number);
- assertEquals(expected, result);
- }
-
- @Test
- public void testExplain() throws IOException {
- StreamExpression express = StreamExpressionParser.parse("month('myfield')");
- DatePartEvaluator datePartEvaluator = new DatePartEvaluator(express,factory);
- Explanation explain = datePartEvaluator.toExplanation(factory);
- assertEquals("month(myfield)", explain.getExpression());
-
- express = StreamExpressionParser.parse("day(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb)");
- datePartEvaluator = new DatePartEvaluator(express,factory);
- explain = datePartEvaluator.toExplanation(factory);
- assertEquals("day(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb)", explain.getExpression());
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d70fc967/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/TemporalEvaluatorsTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/TemporalEvaluatorsTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/TemporalEvaluatorsTest.java
new file mode 100644
index 0000000..a6c590b
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/eval/TemporalEvaluatorsTest.java
@@ -0,0 +1,309 @@
+/*
+ * 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.stream.eval;
+
+import java.io.IOException;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.MonthDay;
+import java.time.YearMonth;
+import java.time.ZoneOffset;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Map;
+import java.util.TimeZone;
+
+import org.apache.commons.collections.map.HashedMap;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDay;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDayOfQuarter;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorDayOfYear;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorEpoch;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorHour;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorMinute;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorMonth;
+import org.apache.solr.client.solrj.io.eval.DatePartEvaluator;
+import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorQuarter;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorSecond;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorWeek;
+import org.apache.solr.client.solrj.io.eval.TemporalEvaluatorYear;
+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.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * Tests numeric Date/Time stream evaluators
+ */
+public class TemporalEvaluatorsTest {
+
+
+ StreamFactory factory;
+ Map<String, Object> values;
+
+ public TemporalEvaluatorsTest() {
+ super();
+
+ factory = new StreamFactory();
+
+ factory.withFunctionName("nope", DatePartEvaluator.class);
+ factory.withFunctionName(TemporalEvaluatorYear.FUNCTION_NAME, TemporalEvaluatorYear.class);
+ factory.withFunctionName(TemporalEvaluatorMonth.FUNCTION_NAME, TemporalEvaluatorMonth.class);
+ factory.withFunctionName(TemporalEvaluatorDay.FUNCTION_NAME, TemporalEvaluatorDay.class);
+ factory.withFunctionName(TemporalEvaluatorDayOfYear.FUNCTION_NAME, TemporalEvaluatorDayOfYear.class);
+ factory.withFunctionName(TemporalEvaluatorHour.FUNCTION_NAME, TemporalEvaluatorHour.class);
+ factory.withFunctionName(TemporalEvaluatorMinute.FUNCTION_NAME, TemporalEvaluatorMinute.class);
+ factory.withFunctionName(TemporalEvaluatorSecond.FUNCTION_NAME, TemporalEvaluatorSecond.class);
+ factory.withFunctionName(TemporalEvaluatorEpoch.FUNCTION_NAME, TemporalEvaluatorEpoch.class);
+ factory.withFunctionName(TemporalEvaluatorWeek.FUNCTION_NAME, TemporalEvaluatorWeek.class);
+ factory.withFunctionName(TemporalEvaluatorQuarter.FUNCTION_NAME, TemporalEvaluatorQuarter.class);
+ factory.withFunctionName(TemporalEvaluatorDayOfQuarter.FUNCTION_NAME, TemporalEvaluatorDayOfQuarter.class);
+
+ values = new HashedMap();
+ }
+
+ @Test
+ public void testInvalidExpression() throws Exception {
+
+ StreamEvaluator evaluator;
+
+ try {
+ evaluator = factory.constructEvaluator("nope(a)");
+ evaluator.evaluate(new Tuple(null));
+ assertTrue(false);
+ } catch (IOException e) {
+ assertTrue(e.getCause().getCause().getMessage().contains("Invalid date expression nope"));
+ assertTrue(e.getCause().getCause().getMessage().contains("expecting one of [year, month, day"));
+ }
+
+ try {
+ evaluator = factory.constructEvaluator("week()");
+ assertTrue(false);
+ } catch (IOException e) {
+ assertTrue(e.getCause().getCause().getMessage().contains("Invalid expression week()"));
+ }
+
+ try {
+ evaluator = factory.constructEvaluator("week(a, b)");
+ assertTrue(false);
+ } catch (IOException e) {
+ assertTrue(e.getCause().getCause().getMessage().contains("expecting one value but found 2"));
+ }
+
+ try {
+ evaluator = factory.constructEvaluator("Week()");
+ assertTrue(false);
+ } catch (IOException e) {
+ assertTrue(e.getMessage().contains("Invalid evaluator expression Week() - function 'Week' is unknown"));
+ }
+ }
+
+
+ @Test
+ public void testInvalidValues() throws Exception {
+ StreamEvaluator evaluator = factory.constructEvaluator("year(a)");
+
+ try {
+ values.clear();
+ values.put("a", 12);
+ Object result = evaluator.evaluate(new Tuple(values));
+ assertTrue(false);
+ } catch (IOException e) {
+ assertEquals("Invalid parameter 12 - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());
+ }
+
+ try {
+ values.clear();
+ values.put("a", "1995-12-31");
+ Object result = evaluator.evaluate(new Tuple(values));
+ assertTrue(false);
+ } catch (IOException e) {
+ assertEquals("Invalid parameter 1995-12-31 - The String must be formatted in the ISO_INSTANT date format.", e.getMessage());
+ }
+
+ try {
+ values.clear();
+ values.put("a", "");
+ Object result = evaluator.evaluate(new Tuple(values));
+ assertTrue(false);
+ } catch (IOException e) {
+ assertEquals("Invalid parameter - The parameter must be a string formatted ISO_INSTANT or of type Instant,Date or LocalDateTime.", e.getMessage());
+ }
+
+ values.clear();
+ values.put("a", null);
+ assertNull(evaluator.evaluate(new Tuple(values)));
+ }
+
+ @Test
+ public void testAllFunctions() throws Exception {
+
+ //year, month, day, dayofyear, hour, minute, quarter, week, second, epoch
+ testFunction("year(a)", "1995-12-31T23:59:59Z", 1995);
+ testFunction("month(a)","1995-12-31T23:59:59Z", 12);
+ testFunction("day(a)", "1995-12-31T23:59:59Z", 31);
+ testFunction("dayOfYear(a)", "1995-12-31T23:59:59Z", 365);
+ testFunction("dayOfQuarter(a)", "1995-12-31T23:59:59Z", 92);
+ testFunction("hour(a)", "1995-12-31T23:59:59Z", 23);
+ testFunction("minute(a)", "1995-12-31T23:59:59Z", 59);
+ testFunction("quarter(a)","1995-12-31T23:59:59Z", 4);
+ testFunction("week(a)", "1995-12-31T23:59:59Z", 52);
+ testFunction("second(a)", "1995-12-31T23:59:58Z", 58);
+ testFunction("epoch(a)", "1995-12-31T23:59:59Z", 820454399000l);
+
+ testFunction("year(a)", "2017-03-17T10:30:45Z", 2017);
+ testFunction("year('a')", "2017-03-17T10:30:45Z", 2017);
+ testFunction("month(a)","2017-03-17T10:30:45Z", 3);
+ testFunction("day(a)", "2017-03-17T10:30:45Z", 17);
+ testFunction("day('a')", "2017-03-17T10:30:45Z", 17);
+ testFunction("dayOfYear(a)", "2017-03-17T10:30:45Z", 76);
+ testFunction("dayOfQuarter(a)", "2017-03-17T10:30:45Z", 76);
+ testFunction("hour(a)", "2017-03-17T10:30:45Z", 10);
+ testFunction("minute(a)", "2017-03-17T10:30:45Z", 30);
+ testFunction("quarter(a)","2017-03-17T10:30:45Z", 1);
+ testFunction("week(a)", "2017-03-17T10:30:45Z", 11);
+ testFunction("second(a)", "2017-03-17T10:30:45Z", 45);
+ testFunction("epoch(a)", "2017-03-17T10:30:45Z", 1489746645000l);
+
+ testFunction("epoch(a)", new Date(1489746645500l).toInstant().toString(), 1489746645500l);
+ testFunction("epoch(a)", new Date(820454399990l).toInstant().toString(), 820454399990l);
+
+ //Additionally test all functions to make sure they return a non-null number
+ for (DatePartEvaluator.FUNCTION function : DatePartEvaluator.FUNCTION.values()) {
+ StreamEvaluator evaluator = factory.constructEvaluator(function+"(a)");
+ values.clear();
+ values.put("a", "2017-03-17T10:30:45Z");
+ Object result = evaluator.evaluate(new Tuple(values));
+ assertNotNull(function+" should return a result",result);
+ assertTrue(function+" should return a number", result instanceof Number);
+ }
+ }
+
+ @Test
+ public void testFunctionsOnDate() throws Exception {
+ Calendar calendar = new GregorianCalendar(2017,12,5, 23, 59);
+ calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
+ Date aDate = calendar.getTime();
+ testFunction("year(a)", aDate, calendar.get(Calendar.YEAR));
+ testFunction("month(a)", aDate, calendar.get(Calendar.MONTH)+1);
+ testFunction("day(a)", aDate, calendar.get(Calendar.DAY_OF_MONTH));
+ testFunction("hour(a)", aDate, calendar.get(Calendar.HOUR_OF_DAY));
+ testFunction("minute(a)", aDate, calendar.get(Calendar.MINUTE));
+ testFunction("epoch(a)", aDate, aDate.getTime());
+ }
+
+ @Test
+ public void testFunctionsOnInstant() throws Exception {
+ Calendar calendar = new GregorianCalendar(2017,12,5, 23, 59);
+ calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
+ Date aDate = calendar.getTime();
+ Instant instant = aDate.toInstant();
+ testFunction("year(a)", instant, calendar.get(Calendar.YEAR));
+ testFunction("month(a)", instant, calendar.get(Calendar.MONTH)+1);
+ testFunction("day(a)", instant, calendar.get(Calendar.DAY_OF_MONTH));
+ testFunction("hour(a)", instant, calendar.get(Calendar.HOUR_OF_DAY));
+ testFunction("minute(a)", instant, calendar.get(Calendar.MINUTE));
+ testFunction("epoch(a)", instant, aDate.getTime());
+ }
+
+ @Test
+ public void testFunctionsLocalDateTime() throws Exception {
+
+ LocalDateTime localDateTime = LocalDateTime.of(2017,12,5, 23, 59);
+ Date aDate = Date.from(localDateTime.atZone(ZoneOffset.UTC).toInstant());
+ testFunction("year(a)", localDateTime, 2017);
+ testFunction("month(a)", localDateTime, 12);
+ testFunction("day(a)", localDateTime, 5);
+ testFunction("hour(a)", localDateTime, 23);
+ testFunction("minute(a)", localDateTime, 59);
+ testFunction("epoch(a)", localDateTime, aDate.getTime());
+ }
+
+ @Test
+ public void testFunctionsOnLong() throws Exception {
+
+ Long longDate = 1512518340000l;
+
+ testFunction("year(a)", longDate, 2017);
+ testFunction("month(a)", longDate, 12);
+ testFunction("day(a)", longDate, 5);
+ testFunction("hour(a)", longDate, 23);
+ testFunction("minute(a)", longDate, 59);
+ testFunction("second(a)", longDate, 0);
+ testFunction("epoch(a)", longDate, longDate);
+
+ }
+
+ @Test
+ public void testLimitedFunctions() throws Exception {
+
+ MonthDay monthDay = MonthDay.of(12,5);
+ testFunction("month(a)", monthDay, 12);
+ testFunction("day(a)", monthDay, 5);
+
+ try {
+ testFunction("year(a)", monthDay, 2017);
+ assertTrue(false);
+ } catch (IOException e) {
+ assertEquals("It is not possible to call 'year' function on java.time.MonthDay", e.getMessage());
+ }
+
+ YearMonth yearMonth = YearMonth.of(2018, 4);
+ testFunction("month(a)", yearMonth, 4);
+ testFunction("year(a)", yearMonth, 2018);
+
+ try {
+ testFunction("day(a)", yearMonth, 5);
+ assertTrue(false);
+ } catch (IOException e) {
+ assertEquals("It is not possible to call 'day' function on java.time.YearMonth", e.getMessage());
+ }
+
+ }
+
+
+ public void testFunction(String expression, Object value, Number expected) throws Exception {
+ StreamEvaluator evaluator = factory.constructEvaluator(expression);
+ values.clear();
+ values.put("a", value);
+ Object result = evaluator.evaluate(new Tuple(values));
+ assertTrue(result instanceof Number);
+ assertEquals(expected, result);
+ }
+
+ @Test
+ public void testExplain() throws IOException {
+ StreamExpression express = StreamExpressionParser.parse("month('myfield')");
+ DatePartEvaluator datePartEvaluator = new DatePartEvaluator(express,factory);
+ Explanation explain = datePartEvaluator.toExplanation(factory);
+ assertEquals("month(myfield)", explain.getExpression());
+
+ express = StreamExpressionParser.parse("day(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb)");
+ datePartEvaluator = new DatePartEvaluator(express,factory);
+ explain = datePartEvaluator.toExplanation(factory);
+ assertEquals("day(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb)", explain.getExpression());
+ }
+}