-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ | |
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
@@ -1085,6 +1087,28 @@ public static java.sql.Date toDate(final Object value, final Supplier<DateFormat | |
throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " to Date for field " + fieldName); | ||
} | ||
|
||
/** | ||
* Converts a java.sql.Date object with 00:00:00 in local time zone (typically coming from a java.sql.ResultSet) | ||
* to UTC normalized form (storing epoch corresponding to UTC 00:00:00 on the given day). | ||
* | ||
* @param date java.sql.Date with local time zone 00:00:00 | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. Can the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading the documentation, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. I did not notice that |
||
} | ||
|
||
/** | ||
* Converts a java.sql.Date object with 00:00:00 in UTC | ||
* to local time zone normalized form (storing epoch corresponding to 00:00:00 in local time zone on the given day). | ||
* | ||
* @param date java.sql.Date with UTC 00:00:00 | ||
* @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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed... what do you think about this?
No There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
public static boolean isDateTypeCompatible(final Object value, final String format) { | ||
if (value == null) { | ||
return false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import org.apache.avro.io.DatumReader; | ||
import org.apache.avro.util.Utf8; | ||
import org.apache.commons.io.input.ReaderInputStream; | ||
import org.apache.nifi.serialization.record.util.DataTypeUtils; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
|
@@ -679,11 +680,12 @@ public void testConvertToAvroStreamForDateTimeAsLogicalType() throws SQLExceptio | |
|
||
testConvertToAvroStreamForDateTime(options, | ||
(record, date) -> { | ||
java.sql.Date expected = DataTypeUtils.convertDateToUTC(date); | ||
final int daysSinceEpoch = (int) record.get("date"); | ||
final long millisSinceEpoch = TimeUnit.MILLISECONDS.convert(daysSinceEpoch, TimeUnit.DAYS); | ||
java.sql.Date actual = java.sql.Date.valueOf(Instant.ofEpochMilli(millisSinceEpoch).atZone(ZoneOffset.UTC).toLocalDate()); | ||
LOGGER.debug("comparing dates, expecting '{}', actual '{}'", date, actual); | ||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). 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?
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I could not go on that way because |
||
}, | ||
(record, time) -> { | ||
int millisSinceMidnight = (int) record.get("time"); | ||
|
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
tojava.sql.Date
changes the value ofDATE_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 returnjava.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.