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 4 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 @@ -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 @@ -6,8 +6,10 @@
package org.opensearch.sql.ppl;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_FORMATS;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.schema;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
import static org.opensearch.sql.util.MatcherUtils.verifySchema;
import static org.opensearch.sql.util.MatcherUtils.verifySome;

Expand All @@ -20,6 +22,7 @@ public class DateTimeImplementationIT extends PPLIntegTestCase {
@Override
public void init() throws IOException {
loadIndex(Index.DATE);
loadIndex(Index.DATE_FORMATS);
}

@Test
Expand Down Expand Up @@ -176,4 +179,15 @@ public void nullDateTimeInvalidDateValueMonth() throws IOException {
verifySchema(result, schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows(new Object[] {null}));
}

@Test
public void testSpanDatetimeWithCustomFormat() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | eval a = 1 | stats count() as cnt by span(yyyy-MM-dd, 1d) as span",
TEST_INDEX_DATE_FORMATS));
verifySchema(result, schema("cnt", null, "integer"), schema("span", null, "date"));
verifyDataRows(result, rows(2, "1984-04-12"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,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