pvillard31 commented on code in PR #10956:
URL: https://github.com/apache/nifi/pull/10956#discussion_r2958656178
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/compile/ExpressionCompiler.java:
##########
@@ -875,6 +883,26 @@ private Evaluator<?> buildFunctionEvaluator(final Tree
tree, final Evaluator<?>
return addToken(new
NumberToDateEvaluator(toWholeNumberEvaluator(subjectEvaluator)), "toDate");
}
}
+ case PLUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "PlusDuration");
Review Comment:
```suggestion
verifyArgCount(argEvaluators, 1, "plusDuration");
```
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MinusInstantDurationEvaluator.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.nifi.attribute.expression.language.evaluation.functions;
+import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
+import
org.apache.nifi.attribute.expression.language.evaluation.util.DateAmountParser;
+
+import java.time.Instant;
+import java.time.ZonedDateTime;
+/**
+ * Evaluator for {@code minusInstantDuration('3 months')} — subtracts a
calendar-aware amount from an Instant.
+ *
+ * <p>Examples:</p>
+ * <pre>
+ * ${date:toInstant('dd-MM-yyyy', 'UTC'):minusInstantDuration('1
month'):formatInstant('dd-MM-yyyy', 'UTC')}
+ * ${date:toInstant('dd-MM-yyyy', 'UTC'):minusInstantDuration('2 weeks')}
+ * </pre>
+ */
+public class MinusInstantDurationEvaluator extends
AbstractInstantArithmeticEvaluator {
Review Comment:
While not a huge problem, this class does not have the same formatting as
MinusDurationEvaluator with blank lines. Missing blank line between package
declaration and first import. Missing blank lines between class members
(fields, constructors, methods). Would be nice to be consistent and it's easier
to read.
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/compile/ExpressionCompiler.java:
##########
@@ -875,6 +883,26 @@ private Evaluator<?> buildFunctionEvaluator(final Tree
tree, final Evaluator<?>
return addToken(new
NumberToDateEvaluator(toWholeNumberEvaluator(subjectEvaluator)), "toDate");
}
}
+ case PLUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "PlusDuration");
+ return addToken(new
PlusDurationEvaluator(toDateEvaluator(subjectEvaluator),
+ toStringEvaluator(argEvaluators.get(0))),
"PlusDuration");
Review Comment:
```suggestion
toStringEvaluator(argEvaluators.get(0))),
"plusDuration");
```
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/compile/ExpressionCompiler.java:
##########
@@ -875,6 +883,26 @@ private Evaluator<?> buildFunctionEvaluator(final Tree
tree, final Evaluator<?>
return addToken(new
NumberToDateEvaluator(toWholeNumberEvaluator(subjectEvaluator)), "toDate");
}
}
+ case PLUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "PlusDuration");
+ return addToken(new
PlusDurationEvaluator(toDateEvaluator(subjectEvaluator),
+ toStringEvaluator(argEvaluators.get(0))),
"PlusDuration");
+ }
+ case MINUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "MinusDuration");
+ return addToken(new
MinusDurationEvaluator(toDateEvaluator(subjectEvaluator),
+ toStringEvaluator(argEvaluators.get(0))),
"MinusDuration");
Review Comment:
```suggestion
toStringEvaluator(argEvaluators.get(0))),
"minusDuration");
```
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/PlusInstantDurationEvaluator.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.nifi.attribute.expression.language.evaluation.functions;
+import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
+import
org.apache.nifi.attribute.expression.language.evaluation.util.DateAmountParser;
+
+import java.time.Instant;
+import java.time.ZonedDateTime;
+/**
+ * Evaluator for {@code plusInstantDuration('3 months')} — adds a
calendar-aware amount to an Instant.
+ *
+ * <p>Examples:</p>
+ * <pre>
+ * ${date:toInstant('dd-MM-yyyy', 'UTC'):plusInstantDuration('1
week'):formatInstant('dd-MM-yyyy', 'UTC')}
+ * ${date:toInstant('dd-MM-yyyy', 'UTC'):plusInstantDuration('2 years')}
+ * </pre>
+ */
+public class PlusInstantDurationEvaluator extends
AbstractInstantArithmeticEvaluator {
Review Comment:
Same comment here compared to PlusDurationEvaluator
##########
nifi-docs/src/main/asciidoc/expression-language-guide.adoc:
##########
@@ -2526,6 +2526,152 @@ the attribute to 1647613347678234 microseconds since
epoch.
`${dateTime:toInstant('yyyy/MM/dd HH:mm:ss.SSSSSSSSS',
'America/New_York'):toNanos()}` converts the Instant value of
the attribute to 1647613347678234567 nanoseconds since epoch.
+
+[.function]
+=== plusDuration
+
+*Description*: [.description]#Adds a specified amount of time to a Date value
using a human-readable expression such as `"1 week"` or `"3 months"`. Month and
year adjustments follow calendar rules.#
+
+If the subject is null, the function returns null.
+
+*Subject Type*: [.subject]#Date#
+
+*Arguments*:
+
+- [.argName]#_amount_# : [.argDesc]#A string in the form `"<number> <unit>"`.
A space between the number and unit is required.#
+
+*Supported Units*: nanosecond(s), second(s), minute(s), hour(s), day(s),
week(s), month(s), year(s)
Review Comment:
We should explicitly call out that nanoseconds won't have any effect since
we are using java.util.Date that has a precision of milliseconds. So while we
support it, it's not going to change the date.
##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/compile/ExpressionCompiler.java:
##########
@@ -875,6 +883,26 @@ private Evaluator<?> buildFunctionEvaluator(final Tree
tree, final Evaluator<?>
return addToken(new
NumberToDateEvaluator(toWholeNumberEvaluator(subjectEvaluator)), "toDate");
}
}
+ case PLUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "PlusDuration");
+ return addToken(new
PlusDurationEvaluator(toDateEvaluator(subjectEvaluator),
+ toStringEvaluator(argEvaluators.get(0))),
"PlusDuration");
+ }
+ case MINUS_DURATION: {
+ verifyArgCount(argEvaluators, 1, "MinusDuration");
Review Comment:
```suggestion
verifyArgCount(argEvaluators, 1, "minusDuration");
```
##########
nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g:
##########
@@ -77,7 +77,7 @@ tokens {
// functions that return Strings
zeroArgString : (TO_UPPER | TO_LOWER | TRIM | TO_STRING | URL_ENCODE |
URL_DECODE | BASE64_ENCODE | BASE64_DECODE | ESCAPE_JSON | ESCAPE_XML |
ESCAPE_CSV | ESCAPE_HTML3 | ESCAPE_HTML4 | UNESCAPE_JSON | UNESCAPE_XML |
UNESCAPE_CSV | UNESCAPE_HTML3 | UNESCAPE_HTML4 | EVALUATE_EL_STRING) LPAREN!
RPAREN!;
oneArgString : ((SUBSTRING_BEFORE | SUBSTRING_BEFORE_LAST | SUBSTRING_AFTER |
SUBSTRING_AFTER_LAST | REPLACE_NULL | REPLACE_EMPTY | REPLACE_BY_PATTERN |
- PREPEND | APPEND | STARTS_WITH | ENDS_WITH |
CONTAINS | UNIQUE | COMPACT_DELIMITED_LIST | TRIM_DELIMITED_LIST | JOIN |
JSON_PATH | JSON_PATH_DELETE | FROM_RADIX | UUID3 | UUID5 | HASH) LPAREN!
anyArg RPAREN!) |
+ PREPEND | APPEND | STARTS_WITH | ENDS_WITH |
CONTAINS | PLUS_DURATION | PLUS_INSTANT_DURATION | MINUS_DURATION |
MINUS_INSTANT_DURATION | UNIQUE | COMPACT_DELIMITED_LIST | TRIM_DELIMITED_LIST
| JOIN | JSON_PATH | JSON_PATH_DELETE | FROM_RADIX | UUID3 | UUID5 | HASH)
LPAREN! anyArg RPAREN!) |
Review Comment:
I don't think they should be added here. These functions return Date and
Instant, not String. I'd add those to oneArgNum to be around TO_DATE and
to_INSTANT
##########
nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/DateAmountParserTest.java:
##########
@@ -0,0 +1,352 @@
+/*
+ * 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.nifi.attribute.expression.language;
Review Comment:
Would be good to move this test class to the same package as the class being
tested:
org.apache.nifi.attribute.expression.language.evaluation.util
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]