Expression leftExpr = criteria.getLeftExpression();
Expression rightExpr = criteria.getRightExpression();
// Allow for concat and string literal to be on either side
Function concatFunction = null;
Constant timestampConstant = null;
if(leftExpr instanceof Function && rightExpr instanceof Constant) {
concatFunction = (Function) leftExpr;
timestampConstant = (Constant) rightExpr;
} else {
return criteria;
}
// Verify data type of string constant and that constant has a value
if(! timestampConstant.getType().equals(DataTypeManager.DefaultDataClasses.STRING)) {
return criteria;
}
// Verify function is concat function
if(! (concatFunction.getName().equalsIgnoreCase("concat") || concatFunction.getName().equals("||"))) { //$NON-NLS-1$ //$NON-NLS-2$
return criteria;
}
// Verify concat has formatdate and formattime functions
Expression[] args = concatFunction.getArgs();
if(! (args[0] instanceof Function && args[1] instanceof Function)) {
return criteria;
}
Function formatDateFunction = (Function) args[0];
Function formatTimeFunction = (Function) args[1];
if(! (formatDateFunction.getName().equalsIgnoreCase("formatdate") && formatTimeFunction.getName().equalsIgnoreCase("formattime"))) { //$NON-NLS-1$ //$NON-NLS-2$
return criteria;
}
// Verify format functions have constants
if(! (formatDateFunction.getArgs()[1] instanceof Constant && formatTimeFunction.getArgs()[1] instanceof Constant)) {
return criteria;
}
// Verify length of combined date/time constants == timestamp constant
String dateFormat = (String) ((Constant)formatDateFunction.getArgs()[1]).getValue();
String timeFormat = (String) ((Constant)formatTimeFunction.getArgs()[1]).getValue();
String timestampValue = (String) timestampConstant.getValue();
// Passed all the checks, so build the optimized version
try {
Timestamp ts = FunctionMethods.parseTimestamp(timestampValue, dateFormat + timeFormat);
Constant dateConstant = new Constant(TimestampWithTimezone.createDate(ts));
CompareCriteria dateCompare = new CompareCriteria(formatDateFunction.getArgs()[0], CompareCriteria.EQ, dateConstant);
Constant timeConstant = new Constant(TimestampWithTimezone.createTime(ts));
CompareCriteria timeCompare = new CompareCriteria(formatTimeFunction.getArgs()[0], CompareCriteria.EQ, timeConstant);
CompoundCriteria compCrit = new CompoundCriteria(CompoundCriteria.AND, dateCompare, timeCompare);
return compCrit;
} catch(FunctionExecutionException e) {