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: Convert java.sql.Date between UTC/local time zone normaliz… #4781
Conversation
…ed forms before/after database operations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @turcsanyip. This looks like a more straightforward solution than previous pull requests. See individual comments for a couple questions and recommendations on streamlining the UTC references.
@@ -26,12 +27,12 @@ | |||
import java.math.BigDecimal; | |||
import java.math.BigInteger; | |||
import java.sql.Array; | |||
import java.sql.Date; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing java.util.Date
to java.sql.Date
changes the value of DATE_CLASS_NAME
, does that have any other implications that should be considered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was intentional because it seemed to be a bug. ResultSet.getMetaData().getColumnClassName()
would never return java.util.Date
but the SQL date/time types.
However, now I see that this code section is related to QueryRecord and it may use java.util.Date
. I'll revert it back.
Thanks for the heads up that made me double check it in more detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for responding regarding the column class name handling. I will take another look once you push the next set of updates.
* @return java.sql.Date with UTC 00:00:00 | ||
*/ | ||
public static Date convertDateToUTC(Date date) { | ||
return new Date(date.toLocalDate().atStartOfDay(ZoneId.of("UTC")).toInstant().toEpochMilli()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the ZoneId.of("UTC")
reference can be replaced with ZoneOffset.UTC
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
atStartOfDay()
has ZoneId
parameter only.
In my understanding ZoneOffset
is DST-agnostic, so the ZoneId
is the right choice in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the documentation, ZoneOffset
extends ZoneId
so would ZoneOffset.UTC
function the same as ZoneId.of("UTC")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I did not notice that ZoneOffset
extends ZoneId
. Just focused on that ZoneId
references an offset and the DST info.
As there is no DST in UTC, it is a simple offset so ZoneOffset.UTC
will be fine. Thanks!
assertEquals(date, actual); | ||
java.sql.Date actual = new java.sql.Date(millisSinceEpoch); | ||
LOGGER.debug("comparing dates, expecting '{}', actual '{}'", expected, actual); | ||
assertEquals(expected, actual); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of performing multiple conversions for the test, could this comparison be simplified by comparing daysSinceEpoch
to the actual number of expected days? That would make the assert much clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests (historically) do not assert the raw value (int / long) from the Avro record but reconstruct the Date/Time/Timestamp objects and compare them with the original objects (which were the inputs of the test cases).
I believe the reason was to avoid to calculate the days/millis/etc. from the test input data which would be a similarly complicated method.
As the test inputs are literals, it is not needed to calculate the expected value from code, but it can be determined once and hard coded as literals too. What do you think?
I mean:
int expectedDaysSinceEpoch = 17444;
assertDate.accept(record, expectedDaysSinceEpoch);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code snippet you suggested looks much clearer and avoids the conversion, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not go on that way because testConvertToAvroStreamForDateTime()
is used in two places and the other really expects a Date object.
However, I found a simpler way to convert the Date to the expected number of days, and then to assert the raw value from Avro against that.
* @return java.sql.Date with local time zone 00:00:00 | ||
*/ | ||
public static Date convertDateToLocalTZ(Date date) { | ||
return Date.valueOf(Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.of("UTC")).toLocalDate()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method naming and implementation are a little confusing on initial read. The atZone()
method returns a ZonedDateTime
and toLocalDate()
return the java.time.LocalDate
portion, but does that actually convert the date to the system default time zone? It may be helpful to break this into multiple lines as it is a bit hard to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed... what do you think about this?
public static Date convertDateToUTC(Date dateLocalTZ) {
ZonedDateTime zdtLocalTZ = ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateLocalTZ.getTime()), ZoneId.systemDefault());
ZonedDateTime zdtUTC = zdtLocalTZ.withZoneSameLocal(ZoneId.of("UTC"));
Date dateUTC = new Date(zdtUTC.toInstant().toEpochMilli());
return dateUTC;
}
No LocaDate
and I believe withZoneSameLocal()
in the middle of the method really grasps what is going on here.
The same can be applied to convertDateToLocalTZ()
similarly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updated suggestion, breaking things out read much more easily, and the withZoneSameLocal()
method makes more sense now. Based on that snippet, I would suggest just returning the final Date
object instead of declaring and returning.
@exceptionfactory Thanks for the review, implemented your suggestions. |
@turcsanyip Thanks for making the changes. I confirmed that several of the tests run in different time zones as designed. I noticed that the changes for |
@turcsanyip Thanks for adding the tests, I confirmed the changes function as designed and cover the lines changed in |
…ed forms before/after database operations This closes apache#4781 Signed-off-by: David Handermann <exceptionfactory@gmail.com>
…ed forms before/after database operations This closes apache#4781 Signed-off-by: David Handermann <exceptionfactory@gmail.com>
…ed forms before/after database operations
https://issues.apache.org/jira/browse/NIFI-8023?focusedCommentId=17271710&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17271710
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:
mvn -Pcontrib-check clean install
at the rootnifi
folder?LICENSE
file, including the mainLICENSE
file undernifi-assembly
?NOTICE
file, including the mainNOTICE
file found undernifi-assembly
?.displayName
in addition to .name (programmatic access) for each of the new properties?For documentation related changes:
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.