From c1d495c970ca55cf8b3ba4c41998100eb619c1fb Mon Sep 17 00:00:00 2001 From: Dinu John <86094133+dinujoh@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:03:51 -0600 Subject: [PATCH] Handle MySQL Year data type with value <=1900 and >2155 Signed-off-by: Dinu John <86094133+dinujoh@users.noreply.github.com> --- .../source/rds/datatype/impl/TemporalTypeHandler.java | 10 +++++++++- .../rds/datatype/impl/TemporalTypeHandlerTest.java | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java index fe992c8bb5..cac3ea7072 100644 --- a/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java +++ b/data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandler.java @@ -142,7 +142,15 @@ private Long parseDateTimeStrAsEpochMillis(final String dateTimeStr) { private Long handleYear(final String yearStr) { try { // MySQL YEAR values are typically four-digit numbers (e.g., 2024). - return Long.parseLong(yearStr); + final long year = Long.parseLong(yearStr); + + // MySQL converts values in 1- or 2-digit strings in the range '0' to '99' to YYYY format + // MySQL YEAR values in YYYY format are with a range of 1901 to 2155. Outside this range the value is 0. + if (year <= 1900 || year > 2155) { + return 0L; + } + + return year; } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid year format: " + yearStr, e); } diff --git a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java index 82fbd26f36..fdee424ed3 100644 --- a/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java +++ b/data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/impl/TemporalTypeHandlerTest.java @@ -132,8 +132,13 @@ void handle_withYearType_returnsCorrectEpochMillis(String input, long expected) private static Stream provideYearTestCases() { return Stream.of( Arguments.of("2023", 2023), - Arguments.of("1900", 1900), - Arguments.of("1997", 1997) + Arguments.of("1900", 0), + Arguments.of("1997", 1997), + Arguments.of("1889", 0), + Arguments.of("1901", 1901), + Arguments.of("2155", 2155), + Arguments.of("2156", 0), + Arguments.of("3015", 0) ); }