Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jul 10, 2023
1 parent 56e5621 commit be8c34f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,111 +215,51 @@ private Optional<ExprType> type(String field) {
}

/**
* Parses value with the first matching formatter as an Instant to UTF.
*
* @param value - timestamp as string
* @param dateType - field type
* @return Instant without timezone
*/
private ExprValue parseTimestampString(String value, OpenSearchDateType dateType) {
Instant parsed = null;
List<DateFormatter> formatters = dateType.getAllNamedFormatters();
formatters.addAll(dateType.getAllCustomFormatters());

for (DateFormatter formatter : formatters) {
try {
TemporalAccessor accessor = formatter.parse(value);
ZonedDateTime zonedDateTime = DateFormatters.from(accessor);
// remove the Zone
parsed = zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toInstant();
return new ExprTimestampValue(parsed);
} catch (IllegalArgumentException ignored) {
// nothing to do, try another format
}
}

// if no formatters are available, try the default formatter
try {
parsed = DateFormatters.from(DATE_TIME_FORMATTER.parse(value)).toInstant();
return new ExprTimestampValue(parsed);
} catch (DateTimeParseException ignored) {
// ignored
}

// otherwise, throw an error that no formatters worked
throw new IllegalArgumentException(
String.format(
"Construct ExprTimestampValue from \"%s\" failed, unsupported date format.", value)
);
}

/**
* return the first matching formatter as a time without timezone.
* Parse value with the first matching formatter into {@link ExprValue}
* with corresponding {@link ExprCoreType}.
*
* @param value - time as string
* @param dateType - field data type
* @return time without timezone
* @param dataType - field data type
* @return Parsed value
*/
private ExprValue parseTimeString(String value, OpenSearchDateType dateType) {
List<DateFormatter> formatters = dateType.getAllNamedFormatters();
formatters.addAll(dateType.getAllCustomFormatters());
private ExprValue parseDateTimeString(String value, OpenSearchDateType dataType) {
List<DateFormatter> formatters = dataType.getAllNamedFormatters();
formatters.addAll(dataType.getAllCustomFormatters());
ExprCoreType returnFormat = (ExprCoreType) dataType.getExprType();

for (DateFormatter formatter : formatters) {
try {
TemporalAccessor accessor = formatter.parse(value);
ZonedDateTime zonedDateTime = DateFormatters.from(accessor);
return new ExprTimeValue(
zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toLocalTime());
switch (returnFormat) {
case TIME: return new ExprTimeValue(
zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toLocalTime());
case DATE: return new ExprDateValue(
zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toLocalDate());
default: return new ExprTimestampValue(
zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toInstant());
}
} catch (IllegalArgumentException ignored) {
// nothing to do, try another format
}
}

// if no formatters are available, try the default formatter
try {
return new ExprTimeValue(
DateFormatters.from(STRICT_HOUR_MINUTE_SECOND_FORMATTER.parse(value)).toLocalTime());
} catch (DateTimeParseException ignored) {
// ignored
}

throw new IllegalArgumentException("Construct ExprTimeValue from \"" + value
+ "\" failed, unsupported time format.");
}

/**
* return the first matching formatter as a date without timezone.
*
* @param value - date as string
* @param dateType - field data type
* @return date without timezone
*/
private ExprValue parseDateString(String value, OpenSearchDateType dateType) {
List<DateFormatter> formatters = dateType.getAllNamedFormatters();
formatters.addAll(dateType.getAllCustomFormatters());

for (DateFormatter formatter : formatters) {
try {
TemporalAccessor accessor = formatter.parse(value);
ZonedDateTime zonedDateTime = DateFormatters.from(accessor);
// return the first matching formatter as a date without timezone
return new ExprDateValue(
zonedDateTime.withZoneSameLocal(UTC_ZONE_ID).toLocalDate());
} catch (IllegalArgumentException ignored) {
// nothing to do, try another format
switch (returnFormat) {
case TIME: return new ExprTimeValue(
DateFormatters.from(STRICT_HOUR_MINUTE_SECOND_FORMATTER.parse(value)).toLocalTime());
case DATE: return new ExprDateValue(
DateFormatters.from(STRICT_YEAR_MONTH_DAY_FORMATTER.parse(value)).toLocalDate());
default: return new ExprTimestampValue(
DateFormatters.from(DATE_TIME_FORMATTER.parse(value)).toInstant());
}
}

// if no formatters are available, use the default formatter
try {
return new ExprDateValue(
DateFormatters.from(STRICT_YEAR_MONTH_DAY_FORMATTER.parse(value)).toLocalDate());
} catch (DateTimeParseException ignored) {
// ignored
}

throw new IllegalArgumentException("Construct ExprDateValue from \"" + value
+ "\" failed, unsupported date format.");
throw new IllegalArgumentException(String.format(
"Construct %s from \"%s\" failed, unsupported format.", returnFormat, value));
}

private ExprValue createOpenSearchDateType(Content value, ExprType type) {
Expand All @@ -345,23 +285,11 @@ private ExprValue createOpenSearchDateType(Content value, ExprType type) {
}
} else {
// custom format
switch ((ExprCoreType) returnFormat) {
case TIME: return parseTimeString(value.stringValue(), dt);
case DATE: return parseDateString(value.stringValue(), dt);
default: return parseTimestampString(value.stringValue(), dt);
}
return parseDateTimeString(value.stringValue(), dt);
}
}

if (value.isString()) {
if (returnFormat == TIME) {
return parseTimeString(value.stringValue(), dt);
}
if (returnFormat == DATE) {
return parseDateString(value.stringValue(), dt);
}
// else timestamp/datetime
return parseTimestampString(value.stringValue(), dt);
return parseDateTimeString(value.stringValue(), dt);
}

return new ExprTimestampValue((Instant) value.objectValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ public void constructDatetime_fromCustomFormat() {
assertThrows(IllegalArgumentException.class,
() -> constructFromObject("customFormatV", "2015-01-01 12-10-30"));
assertEquals(
"Construct ExprTimestampValue from \"2015-01-01 12-10-30\" failed, "
+ "unsupported date format.",
"Construct TIMESTAMP from \"2015-01-01 12-10-30\" failed, "
+ "unsupported format.",
exception.getMessage());

assertEquals(
Expand All @@ -372,17 +372,17 @@ public void constructDatetimeFromUnsupportedFormat_ThrowIllegalArgumentException
assertThrows(IllegalArgumentException.class,
() -> constructFromObject("timestampV", "2015-01-01 12:10"));
assertEquals(
"Construct ExprTimestampValue from \"2015-01-01 12:10\" failed, "
+ "unsupported date format.",
"Construct TIMESTAMP from \"2015-01-01 12:10\" failed, "
+ "unsupported format.",
exception.getMessage());

// fail with missing seconds
exception =
assertThrows(IllegalArgumentException.class,
() -> constructFromObject("dateOrEpochMillisV", "2015-01-01 12:10"));
assertEquals(
"Construct ExprTimestampValue from \"2015-01-01 12:10\" failed, "
+ "unsupported date format.",
"Construct TIMESTAMP from \"2015-01-01 12:10\" failed, "
+ "unsupported format.",
exception.getMessage());
}

Expand All @@ -391,15 +391,15 @@ public void constructTimeFromUnsupportedFormat_ThrowIllegalArgumentException() {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class, () -> constructFromObject("timeV", "2015-01-01"));
assertEquals(
"Construct ExprTimeValue from \"2015-01-01\" failed, "
+ "unsupported time format.",
"Construct TIME from \"2015-01-01\" failed, "
+ "unsupported format.",
exception.getMessage());

exception = assertThrows(
IllegalArgumentException.class, () -> constructFromObject("timeStringV", "10:10"));
assertEquals(
"Construct ExprTimeValue from \"10:10\" failed, "
+ "unsupported time format.",
"Construct TIME from \"10:10\" failed, "
+ "unsupported format.",
exception.getMessage());
}

Expand All @@ -408,15 +408,15 @@ public void constructDateFromUnsupportedFormat_ThrowIllegalArgumentException() {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class, () -> constructFromObject("dateV", "12:10:10"));
assertEquals(
"Construct ExprDateValue from \"12:10:10\" failed, "
+ "unsupported date format.",
"Construct DATE from \"12:10:10\" failed, "
+ "unsupported format.",
exception.getMessage());

exception = assertThrows(
IllegalArgumentException.class, () -> constructFromObject("dateStringV", "abc"));
assertEquals(
"Construct ExprDateValue from \"abc\" failed, "
+ "unsupported date format.",
"Construct DATE from \"abc\" failed, "
+ "unsupported format.",
exception.getMessage());
}

Expand Down

0 comments on commit be8c34f

Please sign in to comment.