SOLR-10303: Switching from the fieldName param to subEvaluators

This commit is contained in:
Gethin James 2017-03-17 18:11:00 +01:00 committed by Joel Bernstein
parent b13945b1ef
commit c3d205cdcc
2 changed files with 45 additions and 43 deletions

View File

@ -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,39 +50,34 @@ 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;
//First evaluate the parameter
StreamEvaluator streamEvaluator = subEvaluators.get(0);
Object tupleValue = streamEvaluator.evaluate(tuple);
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);
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) {
@ -95,22 +89,24 @@ public class DatePartEvaluator extends NumberEvaluator {
return evaluate(date);
}
} 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));
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)));
}
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));
}
private Instant getInstant(String dateStr) throws IOException {
private Instant getInstant(String dateStr) {
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
*/
@ -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

View File

@ -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();