Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-8023 Test failure after NIFI-7996 commit if system default TZ is… #4677

Closed
wants to merge 1 commit into from

Conversation

adenes
Copy link
Contributor

@adenes adenes commented Nov 19, 2020

… CET

Thank you for submitting a contribution to Apache NiFi.

Please provide a short description of the PR here:

Description of PR

Enables X functionality; fixes bug NIFI-YYYY.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically main)?

  • Is your initial contribution a single, squashed commit? Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not squash or use --force when pushing to allow for clean monitoring of changes.

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • Have you verified that the full build is successful on JDK 8?
  • Have you verified that the full build is successful on JDK 11?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.

@exceptionfactory
Copy link
Contributor

Looking at the issue in more detail, did you evaluate AvroTypeUtil.convertToAvroObject() and how it handles the LOGICAL_TYPE_DATE? The current implementation converts from java.sql.Date.getTime() to Instant, using Instant.ofEpochMilli() and passes in the milliseconds from java.sql.Date.getTime().

Since java.sql.Date does not include the timezone, the ChronoUnit.DAYS.between() calculation returns a different number of days depending on the JVM timezone.

Changing AvroTypeUtil.convertToAvroObject() line 669 to use java.sql.Date.toLocalDate().toEpochDay() results in both tests working without any changes, and appears to operate according to the original intent of the method.

@adenes
Copy link
Contributor Author

adenes commented Nov 20, 2020

Thanks @exceptionfactory for the review. Actually your recommended change is the revert of the original commit that caused these test to fail (see: 83948db#diff-98d8c798133bf7b6d8a2b49a35139863b6ac85c6001787a62b3c6117abafb440L669)

I think we have a more generic issue, using java.sql.Date for representing dates is not the best solution. Being a subclass of java.util.Data means that under the hood it stores the time too, just doesn't expose it.
Storing the time leads to a "date skew" issue: when 2020-11-20 is represented as 2020-11-20 00:00 UTC but if later it's converted to a different timezone (e.g. EST, UTC-5) the same instant is presented as 2020-11-19 19:00 EST, thus the date part has changed.
This case is covered by a newly added test case: https://github.com/apache/nifi/blob/main/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertRecord.java#L378

My opinion is that in case of dates time zone doesn't make sense (i.e. 2020-11-19 is the same date in every time zone, even if in some it starts earlier), that's why I removed the toLocalDate() call in the previously mentioned commit to fix this issue.

Unfortunately this led to these two failing tests, but I think the tests are incorrect.

@exceptionfactory
Copy link
Contributor

Thanks for the background @adenes that is helpful to know. I agree with you that the underlying issue is related to using java.sql.Date for representing dates without times.

Looking at the unit test you referenced in TestConvertRecord and stepping through the code, it looks like the better place to address the problem is in DataTypeUtils.toDate() where DateFormat.parse() creates a java.util.Date. As you described, when walking through the unit test, the parse result returns a java.util.Date with a local time zone, which skews the resulting java.sql.Date. Replacing DateFormat with LocalDate.parse() and using java.sql.Date.valueOf() avoids the skewed return value. Making the change at that level would avoid the need to readjust values later on.

Making the change in DataTypeUtils.toDate() will probably have other consequences, but that seems like it is worth considering to get to the root of the problem.

@adenes
Copy link
Contributor Author

adenes commented Jan 26, 2021

Closing in favor of implementation in #4781

@adenes adenes closed this Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants