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.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to