Skip to content

Commit 2416e20

Browse files
committed
Support param timefield to specify span field in timechart
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent ab813d9 commit 2416e20

File tree

6 files changed

+89
-76
lines changed

6 files changed

+89
-76
lines changed

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteTimechartCommandIT.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.json.JSONObject;
1414
import org.junit.jupiter.api.Test;
1515
import org.opensearch.client.ResponseException;
16+
import org.opensearch.sql.common.utils.StringUtils;
1617
import org.opensearch.sql.ppl.PPLIntegTestCase;
1718

1819
public class CalciteTimechartCommandIT extends PPLIntegTestCase {
@@ -64,7 +65,7 @@ public void testTimechartWithMinuteSpanAndGroupBy() throws IOException {
6465
}
6566

6667
@Test
67-
public void testTimechartWithoutTimestampField() throws IOException {
68+
public void testTimechartWithoutTimestampField() {
6869
Throwable exception =
6970
assertThrows(
7071
ResponseException.class,
@@ -74,6 +75,16 @@ public void testTimechartWithoutTimestampField() throws IOException {
7475
verifyErrorMessageContains(exception, "Field [@timestamp] not found.");
7576
}
7677

78+
@Test
79+
public void testTimechartWithCustomTimeField() throws IOException {
80+
JSONObject result =
81+
executeQuery(
82+
StringUtils.format(
83+
"source=%s | timechart timefield=birthdate span=1year count()", TEST_INDEX_BANK));
84+
verifySchema(result, schema("birthdate", "timestamp"), schema("count()", "bigint"));
85+
verifyDataRows(result, rows("2017-01-01 00:00:00", 2), rows("2018-01-01 00:00:00", 5));
86+
}
87+
7788
@Test
7889
public void testTimechartWithMinuteSpanNoGroupBy() throws IOException {
7990
JSONObject result = executeQuery("source=events | timechart span=1m avg(cpu_usage)");

ppl/src/main/antlr/OpenSearchPPLLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ LIMIT: 'LIMIT';
144144
USEOTHER: 'USEOTHER';
145145
OTHERSTR: 'OTHERSTR';
146146
NULLSTR: 'NULLSTR';
147+
TIMEFIELD: 'TIMEFIELD';
147148
INPUT: 'INPUT';
148149
OUTPUT: 'OUTPUT';
149150
PATH: 'PATH';

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ timechartParameter
318318
: LIMIT EQUAL integerLiteral
319319
| SPAN EQUAL spanLiteral
320320
| USEOTHER EQUAL (booleanLiteral | ident)
321+
| TIMEFIELD EQUAL (ident | stringLiteral)
321322
;
322323

323324
spanLiteral
@@ -1551,6 +1552,7 @@ searchableKeyWord
15511552
| SED
15521553
| MAX_MATCH
15531554
| OFFSET_FIELD
1555+
| TIMEFIELD
15541556
| patternMethod
15551557
| patternMode
15561558
// AGGREGATIONS AND WINDOW

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
import org.opensearch.sql.ast.expression.SearchAnd;
6464
import org.opensearch.sql.ast.expression.SearchExpression;
6565
import org.opensearch.sql.ast.expression.SearchGroup;
66-
import org.opensearch.sql.ast.expression.Span;
67-
import org.opensearch.sql.ast.expression.SpanUnit;
6866
import org.opensearch.sql.ast.expression.UnresolvedArgument;
6967
import org.opensearch.sql.ast.expression.UnresolvedExpression;
7068
import org.opensearch.sql.ast.expression.WindowFrame;
@@ -743,41 +741,28 @@ private List<UnresolvedExpression> parseAggTerms(
743741
/** Timechart command. */
744742
@Override
745743
public UnresolvedPlan visitTimechartCommand(OpenSearchPPLParser.TimechartCommandContext ctx) {
746-
UnresolvedExpression binExpression =
747-
AstDSL.span(AstDSL.implicitTimestampField(), AstDSL.intLiteral(1), SpanUnit.m);
748-
Integer limit = 10;
749-
Boolean useOther = true;
750-
// Process timechart parameters
751-
for (OpenSearchPPLParser.TimechartParameterContext paramCtx : ctx.timechartParameter()) {
752-
UnresolvedExpression param = internalVisitExpression(paramCtx);
753-
if (param instanceof Span) {
754-
binExpression = param;
755-
} else if (param instanceof Literal literal) {
756-
if (DataType.BOOLEAN.equals(literal.getType())) {
757-
useOther = (Boolean) literal.getValue();
758-
} else if (DataType.INTEGER.equals(literal.getType())
759-
|| DataType.LONG.equals(literal.getType())) {
760-
limit = (Integer) literal.getValue();
761-
}
762-
}
763-
}
744+
List<Argument> arguments = ArgumentFactory.getArgumentList(ctx, expressionBuilder);
745+
ArgumentMap argMap = ArgumentMap.of(arguments);
746+
Literal spanLiteral = argMap.getOrDefault("spanliteral", AstDSL.stringLiteral("1m"));
747+
String timeFieldName =
748+
Optional.ofNullable(argMap.get("timefield"))
749+
.map(l -> (String) l.getValue())
750+
.orElse(OpenSearchConstants.IMPLICIT_FIELD_TIMESTAMP);
751+
Field spanField = AstDSL.field(timeFieldName);
752+
Alias span =
753+
AstDSL.alias(timeFieldName, AstDSL.spanFromSpanLengthLiteral(spanField, spanLiteral));
764754
UnresolvedExpression aggregateFunction = parseAggTerms(List.of(ctx.statsAggTerm())).getFirst();
765-
766755
UnresolvedExpression byField =
767-
ctx.fieldExpression() != null ? internalVisitExpression(ctx.fieldExpression()) : null;
768-
List<Argument> arguments =
769-
List.of(
770-
new Argument("limit", AstDSL.intLiteral(limit)),
771-
new Argument("useother", AstDSL.booleanLiteral(useOther)));
772-
binExpression = AstDSL.alias(OpenSearchConstants.IMPLICIT_FIELD_TIMESTAMP, binExpression);
773-
if (byField != null) {
774-
byField =
775-
AstDSL.alias(
776-
StringUtils.unquoteIdentifier(getTextInQuery(ctx.fieldExpression())), byField);
777-
}
756+
Optional.ofNullable(ctx.fieldExpression())
757+
.map(
758+
f ->
759+
AstDSL.alias(
760+
StringUtils.unquoteIdentifier(getTextInQuery(f)),
761+
internalVisitExpression(f)))
762+
.orElse(null);
778763
return Chart.builder()
779764
.aggregationFunction(aggregateFunction)
780-
.rowSplit(binExpression)
765+
.rowSplit(span)
781766
.columnSplit(byField)
782767
.arguments(arguments)
783768
.build();

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public UnresolvedExpression visitMaxOption(OpenSearchPPLParser.MaxOptionContext
747747
return new Argument("max", (Literal) this.visit(ctx.integerLiteral()));
748748
}
749749

750-
private QualifiedName visitIdentifiers(List<? extends ParserRuleContext> ctx) {
750+
public QualifiedName visitIdentifiers(List<? extends ParserRuleContext> ctx) {
751751
return new QualifiedName(
752752
ctx.stream()
753753
.map(RuleContext::getText)
@@ -976,47 +976,6 @@ public UnresolvedExpression visitTimeModifierValue(
976976
return AstDSL.stringLiteral(osDateMathExpression);
977977
}
978978

979-
@Override
980-
public UnresolvedExpression visitTimechartParameter(
981-
OpenSearchPPLParser.TimechartParameterContext ctx) {
982-
UnresolvedExpression timechartParameter;
983-
if (ctx.SPAN() != null) {
984-
// Convert span=1h to span(@timestamp, 1h)
985-
Literal spanLiteral = (Literal) visit(ctx.spanLiteral());
986-
timechartParameter =
987-
AstDSL.spanFromSpanLengthLiteral(AstDSL.implicitTimestampField(), spanLiteral);
988-
} else if (ctx.LIMIT() != null) {
989-
Literal limit = (Literal) visit(ctx.integerLiteral());
990-
if ((Integer) limit.getValue() < 0) {
991-
throw new IllegalArgumentException("Limit must be a non-negative number");
992-
}
993-
timechartParameter = limit;
994-
} else if (ctx.USEOTHER() != null) {
995-
UnresolvedExpression useOther;
996-
if (ctx.booleanLiteral() != null) {
997-
useOther = visit(ctx.booleanLiteral());
998-
} else if (ctx.ident() != null) {
999-
QualifiedName ident = visitIdentifiers(List.of(ctx.ident()));
1000-
String useOtherValue = ident.toString();
1001-
if ("true".equalsIgnoreCase(useOtherValue) || "t".equalsIgnoreCase(useOtherValue)) {
1002-
useOther = AstDSL.booleanLiteral(true);
1003-
} else if ("false".equalsIgnoreCase(useOtherValue) || "f".equalsIgnoreCase(useOtherValue)) {
1004-
useOther = AstDSL.booleanLiteral(false);
1005-
} else {
1006-
throw new IllegalArgumentException(
1007-
"Invalid useOther value: " + ctx.ident().getText() + ". Expected true/false or t/f");
1008-
}
1009-
} else {
1010-
throw new IllegalArgumentException("value for useOther must be a boolean or identifier");
1011-
}
1012-
timechartParameter = useOther;
1013-
} else {
1014-
throw new IllegalArgumentException(
1015-
String.format("A parameter of timechart must be a span, limit or useOther, got %s", ctx));
1016-
}
1017-
return timechartParameter;
1018-
}
1019-
1020979
/**
1021980
* Process time range expressions (EARLIEST='value' or LATEST='value') It creates a Comparison
1022981
* filter like @timestamp >= timeModifierValue

ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SortFieldContext;
3333
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.StreamstatsCommandContext;
3434
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SuffixSortFieldContext;
35+
import org.opensearch.sql.ppl.parser.AstExpressionBuilder;
3536

3637
/** Util class to get all arguments as a list from the PPL command. */
3738
public class ArgumentFactory {
@@ -232,6 +233,60 @@ public static List<Argument> getArgumentList(ChartCommandContext ctx) {
232233
return arguments;
233234
}
234235

236+
public static List<Argument> getArgumentList(
237+
OpenSearchPPLParser.TimechartCommandContext timechartCtx,
238+
AstExpressionBuilder expressionBuilder) {
239+
List<Argument> arguments = new ArrayList<>();
240+
for (OpenSearchPPLParser.TimechartParameterContext ctx : timechartCtx.timechartParameter()) {
241+
if (ctx.SPAN() != null) {
242+
arguments.add(
243+
new Argument("spanliteral", (Literal) expressionBuilder.visit(ctx.spanLiteral())));
244+
} else if (ctx.LIMIT() != null) {
245+
Literal limit = getArgumentValue(ctx.integerLiteral());
246+
if ((Integer) limit.getValue() < 0) {
247+
throw new IllegalArgumentException("Limit must be a non-negative number");
248+
}
249+
arguments.add(new Argument("limit", limit));
250+
} else if (ctx.USEOTHER() != null) {
251+
Literal useOther;
252+
if (ctx.booleanLiteral() != null) {
253+
useOther = getArgumentValue(ctx.booleanLiteral());
254+
} else if (ctx.ident() != null) {
255+
String identLiteral = expressionBuilder.visitIdentifiers(List.of(ctx.ident())).toString();
256+
if ("true".equalsIgnoreCase(identLiteral) || "t".equalsIgnoreCase(identLiteral)) {
257+
useOther = AstDSL.booleanLiteral(true);
258+
} else if ("false".equalsIgnoreCase(identLiteral) || "f".equalsIgnoreCase(identLiteral)) {
259+
useOther = AstDSL.booleanLiteral(false);
260+
} else {
261+
throw new IllegalArgumentException(
262+
"Invalid useOther value: "
263+
+ ctx.ident().getText()
264+
+ ". Expected true/false or t/f");
265+
}
266+
} else {
267+
throw new IllegalArgumentException("value for useOther must be a boolean or identifier");
268+
}
269+
arguments.add(new Argument("useother", useOther));
270+
} else if (ctx.TIMEFIELD() != null) {
271+
Literal timeField;
272+
if (ctx.ident() != null) {
273+
timeField =
274+
AstDSL.stringLiteral(
275+
expressionBuilder.visitIdentifiers(List.of(ctx.ident())).toString());
276+
} else {
277+
timeField = getArgumentValue(ctx.stringLiteral());
278+
}
279+
arguments.add(new Argument("timefield", timeField));
280+
} else {
281+
throw new IllegalArgumentException(
282+
String.format(
283+
"A parameter of timechart must be a span, limit, useother, or timefield, got %s",
284+
ctx));
285+
}
286+
}
287+
return arguments;
288+
}
289+
235290
/**
236291
* Get list of {@link Argument}.
237292
*

0 commit comments

Comments
 (0)