Skip to content

Commit fb7bc31

Browse files
committed
Support decimal for span used in bin command
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 234f608 commit fb7bc31

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,4 +1206,12 @@ public void testBinWithEvalCreatedDottedFieldName() throws IOException {
12061206
rows(false, "go", "opentelemetry", 16, 1, "12-14"),
12071207
rows(true, "rust", "opentelemetry", 12, 1, "14-16"));
12081208
}
1209+
1210+
@Test
1211+
public void testBinWithDecimalSpan() throws IOException {
1212+
JSONObject result =
1213+
executeQuery("source=events_null | bin cpu_usage span=7.5 | stats count() by cpu_usage");
1214+
verifySchema(result, schema("count()", "bigint"), schema("cpu_usage", "string"));
1215+
verifyDataRows(result, rows(3, "37.5-45.0"), rows(2, "45.0-52.5"), rows(1, "52.5-60.0"));
1216+
}
12091217
}

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ timechartParameter
270270
spanLiteral
271271
: SPANLENGTH
272272
| INTEGER_LITERAL
273+
| DECIMAL_LITERAL
273274
;
274275

275276
evalCommand

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ private List<UnresolvedExpression> multiFieldRelevanceArguments(
814814
public UnresolvedExpression visitSpanLiteral(OpenSearchPPLParser.SpanLiteralContext ctx) {
815815
if (ctx.INTEGER_LITERAL() != null) {
816816
return AstDSL.intLiteral(Integer.parseInt(ctx.INTEGER_LITERAL().getText()));
817+
} else if (ctx.DECIMAL_LITERAL() != null) {
818+
return AstDSL.decimalLiteral(new BigDecimal(ctx.DECIMAL_LITERAL().getText()));
817819
} else {
818820
return AstDSL.stringLiteral(ctx.getText());
819821
}

ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.opensearch.sql.ast.dsl.AstDSL.allFields;
1515
import static org.opensearch.sql.ast.dsl.AstDSL.and;
1616
import static org.opensearch.sql.ast.dsl.AstDSL.argument;
17+
import static org.opensearch.sql.ast.dsl.AstDSL.bin;
1718
import static org.opensearch.sql.ast.dsl.AstDSL.booleanLiteral;
1819
import static org.opensearch.sql.ast.dsl.AstDSL.caseWhen;
1920
import static org.opensearch.sql.ast.dsl.AstDSL.cast;
@@ -1604,5 +1605,18 @@ public void testVisitSpanLiteral() {
16041605
.limit(10)
16051606
.useOther(true)
16061607
.build());
1608+
1609+
// Test span literal with decimal value
1610+
assertEqual(
1611+
"source=events_null | bin cpu_usage span=7.5 | stats count() by cpu_usage",
1612+
agg(
1613+
bin(
1614+
relation("events_null"),
1615+
field("cpu_usage"),
1616+
argument("span", decimalLiteral(new java.math.BigDecimal("7.5")))),
1617+
exprList(alias("count()", aggregate("count", allFields()))),
1618+
emptyList(),
1619+
exprList(alias("cpu_usage", field("cpu_usage"))),
1620+
defaultStatsArgs()));
16071621
}
16081622
}

0 commit comments

Comments
 (0)