You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "arkadius (via GitHub)" <gi...@apache.org> on 2023/04/13 07:07:41 UTC

[GitHub] [nifi] arkadius opened a new pull request, #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

arkadius opened a new pull request, #7169:
URL: https://github.com/apache/nifi/pull/7169

   <!-- 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. -->
   
   # Summary
   
   Optimization improvement: Migrates NiFi Expression Language from SimpleDateFormat to DateTimeFormatter
   [NIFI-8161](https://issues.apache.org/jira/browse/NIFI-8161). This pull request is "reopen" of #4773 after rebase to 2.0, main branch
   
   # Tracking
   
   Please complete the following tracking steps prior to pull request creation.
   
   ### Issue Tracking
   
   - [x] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
   
   ### Pull Request Tracking
   
   - [x] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
   - [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
   
   ### Pull Request Formatting
   
   - [x] Pull Request based on current revision of the `main` branch
   - [  ] Pull Request refers to a feature branch with one commit containing changes
   
   # Verification
   
   Please indicate the verification steps performed prior to pull request creation.
   
   ### Build
   
   - [ ] Build completed using `mvn clean install -P contrib-check`
     - [x] JDK 11
     - [ ] JDK 17
   
   ### Licensing
   
   - [x] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
   - [x] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
   
   ### Documentation
   
   - [x] Documentation formatting appears as expected in rendered files
   


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1517836631

   Thanks for raising these important questions @arkadius! I [replied to the developer list thread](https://lists.apache.org/thread/7j3t58mc9v567ms97zv4dgm3rwshp78k) that @MikeThomsen started.
   
   We want to move in the direction of using the `java.time` classes across the framework, but it will require careful orchestration.
   
   From that perspective, I think keeping this particular PR focused on the Expression Language function is a good starting place, and we can consider the broader impact in separate issues, related to Jira epic [NIFI-9458](https://issues.apache.org/jira/browse/NIFI-9458).
   
   I will take a closer look at this PR soon, but I think it is generally in a good position to review.


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] MikeThomsen commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "MikeThomsen (via GitHub)" <gi...@apache.org>.
MikeThomsen commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1516967002

   @arkadius you raise some good questions. I would recommend not closing the PR. I'll raise the issue on the developer mailing list (which you are free to join, as it's public).


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] arkadius commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "arkadius (via GitHub)" <gi...@apache.org>.
arkadius commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1532851554

   It's ready. I forgot to change the status. I didn't introduce any changes since last review. I only rebased the branch


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory commented on a diff in pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #7169:
URL: https://github.com/apache/nifi/pull/7169#discussion_r1188740275


##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java:
##########
@@ -47,23 +71,27 @@ public QueryResult<String> evaluate(final EvaluationContext evaluationContext) {
             return new StringQueryResult(null);
         }
 
-        final QueryResult<String> formatResult = format.evaluate(evaluationContext);
-        final String format = formatResult.getValue();
-        if (format == null) {
-            return null;
+        DateTimeFormatter dtf;
+        if (preparedFormatter != null) {
+            dtf = preparedFormatter;
+        } else {
+            final QueryResult<String> formatResult = format.evaluate(evaluationContext);
+            final String format = formatResult.getValue();
+            if (format == null) {
+                return null;
+            }
+            dtf = DateTimeFormatter.ofPattern(format, Locale.US);
         }

Review Comment:
   Recommend reversing the conditional blocks to avoid the negative null check:
   ```suggestion
           if (preparedFormatter == null) {
               final QueryResult<String> formatResult = format.evaluate(evaluationContext);
               final String format = formatResult.getValue();
               if (format == null) {
                   return null;
               }
               dtf = DateTimeFormatter.ofPattern(format, Locale.US);
           } else {
               dtf = preparedFormatter;
           }
   ```



##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java:
##########
@@ -47,23 +71,27 @@ public QueryResult<String> evaluate(final EvaluationContext evaluationContext) {
             return new StringQueryResult(null);
         }
 
-        final QueryResult<String> formatResult = format.evaluate(evaluationContext);
-        final String format = formatResult.getValue();
-        if (format == null) {
-            return null;
+        DateTimeFormatter dtf;
+        if (preparedFormatter != null) {
+            dtf = preparedFormatter;
+        } else {
+            final QueryResult<String> formatResult = format.evaluate(evaluationContext);
+            final String format = formatResult.getValue();
+            if (format == null) {
+                return null;
+            }
+            dtf = DateTimeFormatter.ofPattern(format, Locale.US);
         }
 
-        final SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
-
-        if(timeZone != null) {
+        if ((preparedFormatter == null || !preparedFormatterHasRequestedTimeZone) && timeZone != null) {
             final QueryResult<String> tzResult = timeZone.evaluate(evaluationContext);
             final String tz = tzResult.getValue();
-            if(tz != null && TimeZone.getTimeZone(tz) != null) {
-                sdf.setTimeZone(TimeZone.getTimeZone(tz));
+            if(tz != null) {
+                dtf = dtf.withZone(ZoneId.of(tz));
             }
         }
 
-        return new StringQueryResult(sdf.format(subjectValue));
+        return new StringQueryResult(dtf.format(subjectValue.toInstant().atZone(ZoneId.systemDefault())));

Review Comment:
   It would be helpful to declare the result of `subject.toInstant().atZone(ZoneId.systemDefault())` and then pass it to avoid the nested calls.



##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java:
##########
@@ -47,23 +71,27 @@ public QueryResult<String> evaluate(final EvaluationContext evaluationContext) {
             return new StringQueryResult(null);
         }
 
-        final QueryResult<String> formatResult = format.evaluate(evaluationContext);
-        final String format = formatResult.getValue();
-        if (format == null) {
-            return null;
+        DateTimeFormatter dtf;
+        if (preparedFormatter != null) {
+            dtf = preparedFormatter;
+        } else {
+            final QueryResult<String> formatResult = format.evaluate(evaluationContext);
+            final String format = formatResult.getValue();
+            if (format == null) {
+                return null;
+            }
+            dtf = DateTimeFormatter.ofPattern(format, Locale.US);
         }
 
-        final SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
-
-        if(timeZone != null) {
+        if ((preparedFormatter == null || !preparedFormatterHasRequestedTimeZone) && timeZone != null) {
             final QueryResult<String> tzResult = timeZone.evaluate(evaluationContext);
             final String tz = tzResult.getValue();
-            if(tz != null && TimeZone.getTimeZone(tz) != null) {
-                sdf.setTimeZone(TimeZone.getTimeZone(tz));
+            if(tz != null) {

Review Comment:
   ```suggestion
               if (tz != null) {
   ```



##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/StringToDateEvaluator.java:
##########
@@ -17,27 +17,51 @@
 package org.apache.nifi.attribute.expression.language.evaluation.functions;
 
 import org.apache.nifi.attribute.expression.language.EvaluationContext;
+import org.apache.nifi.attribute.expression.language.StandardEvaluationContext;
 import org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator;
 import org.apache.nifi.attribute.expression.language.evaluation.DateQueryResult;
 import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
 import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
+import org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator;
 import org.apache.nifi.attribute.expression.language.exception.IllegalAttributeException;
+import org.apache.nifi.util.FormatUtils;
 
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.Collections;
 import java.util.Date;
-import java.util.Locale;
-import java.util.TimeZone;
 
 public class StringToDateEvaluator extends DateEvaluator {
 
     private final Evaluator<String> subject;
     private final Evaluator<String> format;
     private final Evaluator<String> timeZone;
 
+    private final DateTimeFormatter preparedFormatter;
+    private final boolean preparedFormatterHasRequestedTimeZone;
+
     public StringToDateEvaluator(final Evaluator<String> subject, final Evaluator<String> format, final Evaluator<String> timeZone) {
         this.subject = subject;
         this.format = format;
+        // if the search string is a literal, we don't need to prepare formatter each time; we can just
+        // prepare it once. Otherwise, it must be prepared for each time.
+        if (format instanceof StringLiteralEvaluator) {
+            DateTimeFormatter dtf = FormatUtils.prepareLenientCaseInsensitiveDateTimeFormatter(format.evaluate(new StandardEvaluationContext(Collections.emptyMap())).getValue());

Review Comment:
   See above note on nested calls and separate variable declaration.



##########
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java:
##########
@@ -17,26 +17,50 @@
 package org.apache.nifi.attribute.expression.language.evaluation.functions;
 
 import org.apache.nifi.attribute.expression.language.EvaluationContext;
+import org.apache.nifi.attribute.expression.language.StandardEvaluationContext;
 import org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator;
 import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
 import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
 import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator;
 import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult;
+import org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator;
 
-import java.text.SimpleDateFormat;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Locale;
-import java.util.TimeZone;
 
 public class FormatEvaluator extends StringEvaluator {
 
     private final DateEvaluator subject;
     private final Evaluator<String> format;
     private final Evaluator<String> timeZone;
 
+    private final DateTimeFormatter preparedFormatter;
+    private final boolean preparedFormatterHasRequestedTimeZone;
+
     public FormatEvaluator(final DateEvaluator subject, final Evaluator<String> format, final Evaluator<String> timeZone) {
         this.subject = subject;
         this.format = format;
+        // if the search string is a literal, we don't need to prepare formatter each time; we can just
+        // prepare it once. Otherwise, it must be prepared for each time.
+        if (format instanceof StringLiteralEvaluator) {
+            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format.evaluate(new StandardEvaluationContext(Collections.emptyMap())).getValue(), Locale.US);

Review Comment:
   The nested of format evaluation is difficult to follow, it would be helpful to declare the pattern string as a separate variable before passing it to `DateTimeFormatter.ofPattern()`.



-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] arkadius commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "arkadius (via GitHub)" <gi...@apache.org>.
arkadius commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1542677749

   Thank you for the review @exceptionfactory . I applied all suggestions and changed the code as you proposed.


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] MikeThomsen commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "MikeThomsen (via GitHub)" <gi...@apache.org>.
MikeThomsen commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1532845499

   @arkadius are you at a point where you want a review or do you want to keep going?


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] arkadius commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "arkadius (via GitHub)" <gi...@apache.org>.
arkadius commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1512122120

   I rebased this branch as we discussed in the original PR, but I see that meantime NiFi code become much more dependent on `DateFormat`. I see many public methods in `DataTypeUtils` taking or returning `DateFormat`. I see some transition methods like `DateTypeUtils.getDateTimeFormatter`. It makes me feel that:
   - I don't see the wider picture of where you already use `DateFormat` now and how you planning to use it in the future
   - I don't know what is the plan to get rid of `DateFormat` because it looks like there is some
   - I don't know if you want to use a lenient, case insensitive format in all these places or some other one
   
   If you want to draw me a wider picture I can try to continue this change but otherwise, I don't want to introduce some ambiguous behaviour in date format handling so I'll close this PR.


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] arkadius commented on pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "arkadius (via GitHub)" <gi...@apache.org>.
arkadius commented on PR #7169:
URL: https://github.com/apache/nifi/pull/7169#issuecomment-1517840330

   Ok, thank you for the explanation


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nifi] exceptionfactory closed pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0

Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory closed pull request #7169: NIFI-8161 NiFi EL: migration from SimpleDateFormat to DateTimeFormatter: rebased to 2.0
URL: https://github.com/apache/nifi/pull/7169


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org