[ https://issues.apache.org/jira/browse/SOLR-10303?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15958567#comment-15958567 ]
ASF GitHub Bot commented on SOLR-10303: --------------------------------------- Github user covolution commented on a diff in the pull request: https://github.com/apache/lucene-solr/pull/171#discussion_r110107181 --- 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) { --- End diff -- Instant does not work with human units of time(eg. year, month, or day), a timezone is required. So I am converting it to a LocalDateTime using ZoneOffset.UTC. > Add date/time Stream Evaluators > ------------------------------- > > Key: SOLR-10303 > URL: https://issues.apache.org/jira/browse/SOLR-10303 > Project: Solr > Issue Type: New Feature > Security Level: Public(Default Security Level. Issues are Public) > Reporter: Joel Bernstein > Attachments: SOLR-10303.patch > > > This ticket will add Stream Evaluators that extract date/time values from a > Solr date field. The following Evaluators will be supported: > hour (date) > minute (date) > month (date) > monthname(date) > quarter(date) > second (date) > year(date) > Syntax: > {code} > select(id, > year(recdate) as year, > month(recdate) as month, > day(recdate) as day, > search(logs, q="blah", fl="id, recdate", sort="recdate asc", > qt="/export")) > {code} -- This message was sent by Atlassian JIRA (v6.3.15#6346) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org For additional commands, e-mail: dev-h...@lucene.apache.org