From 48cf382634dfc837818d97954d5ff8f908a36830 Mon Sep 17 00:00:00 2001 From: Heng Qian Date: Wed, 7 Aug 2024 11:52:07 +0800 Subject: [PATCH 1/2] Make explain of ProjectOperator with alias show both name and alias info Signed-off-by: Heng Qian --- .../sql/expression/NamedExpression.java | 2 +- .../opensearch/sql/executor/ExplainTest.java | 2 +- .../org/opensearch/sql/legacy/ExplainIT.java | 16 ++++++++++++++++ .../resources/expectedOutput/alias_explain.json | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 integ-test/src/test/resources/expectedOutput/alias_explain.json diff --git a/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java b/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java index 03118311a9..a813c2a6d6 100644 --- a/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java @@ -60,6 +60,6 @@ public T accept(ExpressionNodeVisitor visitor, C context) { @Override public String toString() { - return getNameOrAlias(); + return Strings.isNullOrEmpty(alias) ? name : name + " AS " + alias; } } diff --git a/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java b/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java index eaeae07242..7dbccbca84 100644 --- a/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java @@ -77,7 +77,7 @@ void can_explain_project_filter_table_scan() { new ExplainResponse( new ExplainResponseNode( "ProjectOperator", - Map.of("fields", "[name, age]"), + Map.of("fields", "[full_name AS name, age]"), singletonList( new ExplainResponseNode( "FilterOperator", diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java index 27f8eca3ef..054b3f5458 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java @@ -271,4 +271,20 @@ public void testContentTypeOfExplainRequestShouldBeJson() throws IOException { assertEquals("application/json; charset=UTF-8", response.getHeader("content-type")); } + + @Test + public void explainAlias() throws IOException { + + String expectedOutputFilePath = + TestUtils.getResourceFilePath("src/test/resources/expectedOutput/alias_explain.json"); + String expectedOutput = + Files.toString(new File(expectedOutputFilePath), StandardCharsets.UTF_8) + .replaceAll("\r", ""); + + String result = + explainQuery( + String.format("SELECT (age + 1) AS agePlusOne FROM %s LIMIT 10", TEST_INDEX_ACCOUNT)); + Assert.assertThat( + result.replaceAll("\\s+", ""), equalTo(expectedOutput.replaceAll("\\s+", ""))); + } } diff --git a/integ-test/src/test/resources/expectedOutput/alias_explain.json b/integ-test/src/test/resources/expectedOutput/alias_explain.json new file mode 100644 index 0000000000..3bcbb2eaaf --- /dev/null +++ b/integ-test/src/test/resources/expectedOutput/alias_explain.json @@ -0,0 +1,17 @@ +{ + "root": { + "name": "ProjectOperator", + "description": { + "fields": "[(age+1) AS agePlusOne]" + }, + "children": [ + { + "name": "OpenSearchIndexScan", + "description": { + "request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account,sourceBuilder={\"from\":0,\"size\":10,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}},searchDone=false)" + }, + "children": [] + } + ] + } +} From 460189ea58c826a14684fdb31ee441b143cfc31c Mon Sep 17 00:00:00 2001 From: Heng Qian Date: Fri, 9 Aug 2024 12:08:52 +0800 Subject: [PATCH 2/2] refine test cases Signed-off-by: Heng Qian --- .../src/test/java/org/opensearch/sql/legacy/ExplainIT.java | 5 ++++- .../src/test/resources/expectedOutput/alias_explain.json | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java index 054b3f5458..8f59b25f70 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java @@ -283,7 +283,10 @@ public void explainAlias() throws IOException { String result = explainQuery( - String.format("SELECT (age + 1) AS agePlusOne FROM %s LIMIT 10", TEST_INDEX_ACCOUNT)); + String.format( + "SELECT city, (age + 1) AS agePlusOne, (balance + 1000) as newBalance FROM %s LIMIT" + + " 10", + TEST_INDEX_ACCOUNT)); Assert.assertThat( result.replaceAll("\\s+", ""), equalTo(expectedOutput.replaceAll("\\s+", ""))); } diff --git a/integ-test/src/test/resources/expectedOutput/alias_explain.json b/integ-test/src/test/resources/expectedOutput/alias_explain.json index 3bcbb2eaaf..5665bae667 100644 --- a/integ-test/src/test/resources/expectedOutput/alias_explain.json +++ b/integ-test/src/test/resources/expectedOutput/alias_explain.json @@ -2,13 +2,13 @@ "root": { "name": "ProjectOperator", "description": { - "fields": "[(age+1) AS agePlusOne]" + "fields": "[city, (age+1) AS agePlusOne, (balance+1000) AS newBalance]" }, "children": [ { "name": "OpenSearchIndexScan", "description": { - "request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account,sourceBuilder={\"from\":0,\"size\":10,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}},searchDone=false)" + "request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account,sourceBuilder={\"from\":0,\"size\":10,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\",\"city\",\"balance\"],\"excludes\":[]}},searchDone=false)" }, "children": [] }