this.modify(function);
if (function.getParameters().size() != 3) {
return null;
}
//case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
Expression length = function.getParameters().get(2);
List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
Boolean isNegative = null;
if (length instanceof Literal) {
Literal l = (Literal)length;
if (!l.isMultiValued()) {
int value = (Integer)l.getValue();
isNegative = value < 0;
}
}
Expression maxLength = new Function(
SourceSystemFunctions.SUBTRACT_OP,
Arrays.asList(new Function(
SourceSystemFunctions.LENGTH,
Arrays.asList(function.getParameters().get(0)),
TypeFacility.RUNTIME_TYPES.INTEGER),
new Function(
SourceSystemFunctions.SUBTRACT_OP,
Arrays.asList(
function.getParameters().get(1),
new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),
TypeFacility.RUNTIME_TYPES.INTEGER)),
TypeFacility.RUNTIME_TYPES.INTEGER);
clauses.add(new SearchedWhenClause(new Comparison(length, maxLength, Operator.GT), maxLength));
Expression defaultExpr = null;
if (isNegative == null) {
clauses.add(new SearchedWhenClause(new Comparison(length, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), length));
} else if (isNegative) {
//TODO: could be done in the rewriter
function.getParameters().set(2, null);