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 a regression issue of parsing datetime string with custom time format in Span #3079

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -46,13 +46,13 @@ public static Rounding<?> createRounding(SpanExpression span) {
if (DOUBLE.isCompatible(type)) {
return new DoubleRounding(interval);
}
if (type.equals(TIMESTAMP)) {
if (type.equals(TIMESTAMP) || type.typeName().equalsIgnoreCase(TIMESTAMP.typeName())) {
penghuo marked this conversation as resolved.
Show resolved Hide resolved
return new TimestampRounding(interval, span.getUnit().getName());
}
if (type.equals(DATE)) {
if (type.equals(DATE) || type.typeName().equalsIgnoreCase(DATE.typeName())) {
return new DateRounding(interval, span.getUnit().getName());
}
if (type.equals(TIME)) {
if (type.equals(TIME) || type.typeName().equalsIgnoreCase(TIME.typeName())) {
return new TimeRounding(interval, span.getUnit().getName());
}
return new UnknownRounding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.opensearch.sql.data.model.ExprTimestampValue;
import org.opensearch.sql.data.model.ExprTupleValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.opensearch.data.type.OpenSearchBinaryType;
Expand Down Expand Up @@ -227,6 +228,14 @@ private Optional<ExprType> type(String field) {
* @return Parsed value
*/
private static ExprValue parseDateTimeString(String value, OpenSearchDateType dataType) {
try {
String customFormat = dataType.getAllCustomFormatters().toString();
OpenSearchDateType dateType = OpenSearchDateType.of(customFormat);
LocalDate parsedDate = dateType.getParsedDateTime(value).toLocalDate();
return ExprValueUtils.dateValue(parsedDate);
} catch (Exception ignored) {
// nothing to do, try another old parser
}
List<DateFormatter> formatters = dataType.getAllNamedFormatters();
formatters.addAll(dataType.getAllCustomFormatters());
ExprCoreType returnFormat = dataType.getExprCoreType();
Expand Down Expand Up @@ -295,7 +304,7 @@ private static ExprValue createOpenSearchDateType(Content value, ExprType type)
}
} else {
// custom format
return parseDateTimeString(value.stringValue(), dt);
return parseDateTimeString(value.objectValue().toString(), dt);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could u elberate more? value is number value? why objectValue().toString()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is an instance of ObjectContent and isNumber() is true, stringValue() will return the string representation of the object, such as ObjectContent@1234. We should convert it to Java Object then use toString() here. Or value.longValue().toString(). Not sure the value here is a Long type, so value.objectValue().toString() would be best.

}
}
if (value.isString()) {
Expand Down
Loading