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

Fix: Date field format parsing for legacy query engine #3160

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.sql.legacy.esdomain.LocalClusterState;
import org.opensearch.sql.legacy.esdomain.mapping.FieldMappings;
import org.opensearch.sql.legacy.utils.StringUtils;

/** Formatter to transform date fields into a consistent format for consumption by clients. */
public class DateFieldFormatter {
Expand Down Expand Up @@ -83,7 +84,6 @@ public void applyJDBCDateFormat(Map<String, Object> rowSource) {
Date date = parseDateString(formats, columnOriginalDate.toString());
if (date != null) {
rowSource.put(columnName, DateFormat.getFormattedDate(date, FORMAT_JDBC));
break;
} else {
LOG.warn("Could not parse date value; returning original value");
}
Expand Down Expand Up @@ -152,15 +152,27 @@ private Date parseDateString(List<String> formats, String columnOriginalDate) {
switch (columnFormat) {
case "date_optional_time":
case "strict_date_optional_time":
parsedDate =
DateUtils.parseDate(
columnOriginalDate,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_LOGS_EXCEPTION,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_FLIGHTS_EXCEPTION,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_FLIGHTS_EXCEPTION_NO_TIME,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_ECOMMERCE_EXCEPTION,
FORMAT_DOT_DATE_AND_TIME,
FORMAT_DOT_DATE);
// It's possible to have date stored in second / millisecond form without explicit
// format hint.
// Parse it on a best-effort basis.
dai-chen marked this conversation as resolved.
Show resolved Hide resolved
if (StringUtils.isNumeric(columnOriginalDate)) {
long timestamp = Long.parseLong(columnOriginalDate);
if (timestamp > Integer.MAX_VALUE) {
parsedDate = new Date(timestamp);
} else {
parsedDate = new Date(timestamp * 1000);
}
} else {
parsedDate =
DateUtils.parseDate(
columnOriginalDate,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_LOGS_EXCEPTION,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_FLIGHTS_EXCEPTION,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_FLIGHTS_EXCEPTION_NO_TIME,
FORMAT_DOT_OPENSEARCH_DASHBOARDS_SAMPLE_DATA_ECOMMERCE_EXCEPTION,
FORMAT_DOT_DATE_AND_TIME,
FORMAT_DOT_DATE);
}
break;
case "epoch_millis":
parsedDate = new Date(Long.parseLong(columnOriginalDate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,24 @@ public void testStrictDateOptionalTimeOrEpochMillsShouldPass() {
verifyFormatting(columnName, dateFormat, originalDateValue, expectedDateValue);
}

@Test
public void testDateInTimestampFormInSecondWithoutHint() {
String columnName = "date_field";
String dateFormat = "date_optional_time";
String originalDateValue = "1732057981";
String expectedDateValue = "2024-11-19 23:13:01.000";
verifyFormatting(columnName, dateFormat, originalDateValue, expectedDateValue);
}

@Test
public void testDateInTimestampFormInMilliSecondWithoutHint() {
String columnName = "date_field";
String dateFormat = "date_optional_time";
String originalDateValue = "1732057981000";
String expectedDateValue = "2024-11-19 23:13:01.000";
verifyFormatting(columnName, dateFormat, originalDateValue, expectedDateValue);
}

private void verifyFormatting(
String columnName,
String dateFormatProperty,
Expand Down
Loading