From 7525bb15e8ffb42365c6a7e5f7b92592ff25247d Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Fri, 16 Jun 2023 14:27:56 -0700 Subject: [PATCH] Remove backticks on `by` field in stats (#1728) Signed-off-by: Joshua Li --- .../ppl/PrometheusDataSourceCommandsIT.java | 6 +++--- .../opensearch/sql/ppl/parser/AstBuilder.java | 11 +++++----- .../sql/ppl/parser/AstBuilderTest.java | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java index 4ac94a0d78..9006e00ff8 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java @@ -113,8 +113,8 @@ public void testMetricAvgAggregationCommand() { verifySchema(response, schema("agg", "double"), schema("span(@timestamp,15s)", "timestamp"), - schema("`handler`", "string"), - schema("`job`", "string")); + schema("handler", "string"), + schema("job", "string")); Assertions.assertTrue(response.getInt("size") > 0); Assertions.assertEquals(4, response.getJSONArray("datarows").getJSONArray(0).length()); JSONArray firstRow = response.getJSONArray("datarows").getJSONArray(0); @@ -132,7 +132,7 @@ public void testMetricAvgAggregationCommandWithAlias() { verifySchema(response, schema("agg", "double"), schema("span(@timestamp,15s)", "timestamp"), - schema("`handler`", "string"), + schema("handler", "string"), schema("job", "string")); Assertions.assertTrue(response.getInt("size") > 0); Assertions.assertEquals(4, response.getJSONArray("datarows").getJSONArray(0).length()); diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java index 292ca795ae..323f99a5af 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java @@ -201,7 +201,8 @@ public UnresolvedPlan visitStatsCommand(StatsCommandContext ctx) { .map(OpenSearchPPLParser.StatsByClauseContext::fieldList) .map(expr -> expr.fieldExpression().stream() .map(groupCtx -> - (UnresolvedExpression) new Alias(getTextInQuery(groupCtx), + (UnresolvedExpression) new Alias( + StringUtils.unquoteIdentifier(getTextInQuery(groupCtx)), internalVisitExpression(groupCtx))) .collect(Collectors.toList())) .orElse(Collections.emptyList()); @@ -429,10 +430,10 @@ public UnresolvedPlan visitAdCommand(AdCommandContext ctx) { public UnresolvedPlan visitMlCommand(OpenSearchPPLParser.MlCommandContext ctx) { ImmutableMap.Builder builder = ImmutableMap.builder(); ctx.mlArg() - .forEach(x -> { - builder.put(x.argName.getText(), - (Literal) internalVisitExpression(x.argValue)); - }); + .forEach(x -> { + builder.put(x.argName.getText(), + (Literal) internalVisitExpression(x.argValue)); + }); return new ML(builder.build()); } diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java index 65341b66c1..599f6bdd75 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java @@ -282,6 +282,27 @@ public void testStatsCommandWithByClause() { )); } + @Test + public void testStatsCommandWithByClauseInBackticks() { + assertEqual("source=t | stats count(a) by `b` DEDUP_SPLITVALUES=false", + agg( + relation("t"), + exprList( + alias( + "count(a)", + aggregate("count", field("a")) + ) + ), + emptyList(), + exprList( + alias( + "b", + field("b") + )), + defaultStatsArgs() + )); + } + @Test public void testStatsCommandWithAlias() { assertEqual("source=t | stats count(a) as alias",