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 Added toLocalDate() and updated toDate() in DataTypeUtils #4734

Closed
wants to merge 6 commits into from

Conversation

exceptionfactory
Copy link
Contributor

Description of PR

NIFI-8023 Added DataTypeUtils.toLocalDate() method for use in AvroTypeUtil to determine epoch days when logical date types enabled.

Updated DataTypeUtils.toDate() with new parseDate() method that evaluates the format pattern of java.text.SimpleDateFormat to determine whether a Time Zone field Z is present in the pattern. This conditional adjustment is necessary because absence of the Time Zone Field in a java.text.SimpleDateFormat pattern definition produces different results depending on the system time zone configuration.

This approach resolves a latent issue when using the DataTypeUtils.toDate() method to parse a string that does not contain any time zone information. Changes to DataTypeUtils.toDate() and updates to unit tests in referencing classes indicate the expected behavior.

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.

Enhanced toDate() string parsing with conditional time zone adjustment
@turcsanyip
Copy link
Contributor

@exceptionfactory I ran some test flows and there is a regression in ConvertRecord.

ConvertRecord setup:

  • Json reader/writer (XML and CSV also affected)
  • explicit Avro schema specified with Date logical type
  • input date: "1970-01-05"
  • expected output: the same date ("1970-01-05")

If I run it in a UTC+ time zone, the output is one day earlier ("1970-01-04").

The other PR on this jira (#4677 by @adenes) has the same issue so I think it needs to be fixed somewhere else in the codebase (I guess where the java.sql.Date value is written from the record to string).

There is one more use case which is not working. This is not a regression and might have never worked but it would be great to fix it also: ExecuteSQLRecord with Json/CSV/XML record writer (the date is one day earlier then in the database if NiFi is running in a UTC+ time zone)

@adenes
Copy link
Contributor

adenes commented Jan 5, 2021

@exceptionfactory , thanks for the PR.

I ran the unit tests in CET timezone, and one of the newly added tests fails:

[ERROR] testFormattedStringToDateWithSystemDefaultZoneId(org.apache.nifi.serialization.record.TestDataTypeUtils)  Time elapsed: 0.007 s  <<< FAILURE!
org.junit.ComparisonFailure: expected:<19[70-01-0]1> but was:<19[69-12-3]1>
	at org.apache.nifi.serialization.record.TestDataTypeUtils.testFormattedStringToDateWithSystemDefaultZoneId(TestDataTypeUtils.java:168)

I'll do some debugging to see what goes wrong.

@exceptionfactory
Copy link
Contributor Author

@exceptionfactory , thanks for the PR.

I ran the unit tests in CET timezone, and one of the newly added tests fails:

[ERROR] testFormattedStringToDateWithSystemDefaultZoneId(org.apache.nifi.serialization.record.TestDataTypeUtils)  Time elapsed: 0.007 s  <<< FAILURE!
org.junit.ComparisonFailure: expected:<19[70-01-0]1> but was:<19[69-12-3]1>
	at org.apache.nifi.serialization.record.TestDataTypeUtils.testFormattedStringToDateWithSystemDefaultZoneId(TestDataTypeUtils.java:168)

I'll do some debugging to see what goes wrong.

@adenes Thanks for the feedback. After looking more closely at DateUtils.getDateFormat(format, timezoneID) it makes sense that the test in question would behave differently based on the system default timezone. I pushed an update to TestDataTypeUtils that changes the test to use DateUtils.getDateFormat(format), which implicitly sets the DateFormat time zone to GMT. This should avoid failures due to system differences and also test the implicit behavior.

@exceptionfactory
Copy link
Contributor Author

Thanks for the feedback and testing @turcsanyip. I will run some additional tests using the flow configuration described.

@exceptionfactory
Copy link
Contributor Author

@turcsanyip The other use of AvroTypeUtil.LOGICAL_TYPE_DATE was in the normalizeValue(), which was causing the problem with ConvertRecord. The use of new java.sql.Date() with milliseconds resulted in the system default timezone throwing off the converted date. I updated normalizeValue() to use LocalDate.ofEpochDay() and added a unit test method to confirm the corrected behavior.

Copy link
Contributor

@markap14 markap14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @exceptionfactory for improving the timezone handling here! I think there are a few additional cases that need to be taken into account. Commented inline.

if (dateFormat instanceof SimpleDateFormat) {
final SimpleDateFormat simpleDateFormat = (SimpleDateFormat) dateFormat;
final String pattern = simpleDateFormat.toPattern();
adjustmentRequired = !pattern.contains(TIME_ZONE_PATTERN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is sufficient. According to https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html, the timezone can be represented using z, Z, or X, each having a different meaning. I think we have to account for all of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I adjusted the check to use a regular expression pattern for all three characters.

@exceptionfactory
Copy link
Contributor Author

@markap14 Thanks for the review and additional feedback, I pushed an update to address your comments. There still appear to be some edge cases for some record-oriented processors when running NiFi in different time zones, based on running some test flows from @turcsanyip. The PR as it stands addresses the test failures for certain time zones as described in NIFI-8023 through the use of the new DataTypeUtils.toLocalDate() method.

Additional work is probably necessary to address system time zone issues when using some of the JSON Record Readers and Writers, but perhaps it would be better to create a new issue to resolve those problems.

Any additional feedback @turcsanyip or @adenes?

@adenes
Copy link
Contributor

adenes commented Jan 20, 2021

@exceptionfactory , thanks for the follow-up commits. I ran the tests with CET system TZ and unfortunately some test cases fail:

[ERROR] Failures:
[ERROR]   TestAvroReaderWithEmbeddedSchema.testLogicalTypes:66->testLogicalTypes:125 expected:<2017-04-0[4]> but was:<2017-04-0[3]>
[ERROR]   TestAvroReaderWithEmbeddedSchema.testNullableLogicalTypes:72->testLogicalTypes:125 expected:<2017-04-0[4]> but was:<2017-04-0[3]>
[ERROR]   TestCSVRecordReader.testDate:119 expected:<30> but was:<29>
[ERROR]   TestCSVRecordReader.testDateNoCoersionExpectedFormat:163 expected:<30> but was:<29>
[ERROR]   TestJacksonCSVRecordReader.testDate:105 expected:<30> but was:<29>

Could you please have a look at them?

@exceptionfactory
Copy link
Contributor Author

@exceptionfactory , thanks for the follow-up commits. I ran the tests with CET system TZ and unfortunately some test cases fail:

[ERROR] Failures:
[ERROR]   TestAvroReaderWithEmbeddedSchema.testLogicalTypes:66->testLogicalTypes:125 expected:<2017-04-0[4]> but was:<2017-04-0[3]>
[ERROR]   TestAvroReaderWithEmbeddedSchema.testNullableLogicalTypes:72->testLogicalTypes:125 expected:<2017-04-0[4]> but was:<2017-04-0[3]>
[ERROR]   TestCSVRecordReader.testDate:119 expected:<30> but was:<29>
[ERROR]   TestCSVRecordReader.testDateNoCoersionExpectedFormat:163 expected:<30> but was:<29>
[ERROR]   TestJacksonCSVRecordReader.testDate:105 expected:<30> but was:<29>

Could you please have a look at them?

Thanks @adenes I will take a look at those tests.

@exceptionfactory
Copy link
Contributor Author

@adenes After evaluating the unit tests mentioned, I found that each one was performing date conversion in order to compare the expected results. The conversion changed the expected value, which was throwing off the tests. I updated the tests and confirmed successful test completion with the CET time zone.

@exceptionfactory
Copy link
Contributor Author

Closing in favor of implementation in #4781

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants