Skip to content

Commit

Permalink
Make explain of ProjectOperator with alias show both name and alias info
Browse files Browse the repository at this point in the history
Signed-off-by: Heng Qian <[email protected]>
  • Loading branch information
qianheng-aws committed Aug 7, 2024
1 parent 7e73f12 commit 48cf382
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) {

@Override
public String toString() {
return getNameOrAlias();
return Strings.isNullOrEmpty(alias) ? name : name + " AS " + alias;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/legacy/ExplainIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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+", "")));
}
}
17 changes: 17 additions & 0 deletions integ-test/src/test/resources/expectedOutput/alias_explain.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
}

0 comments on commit 48cf382

Please sign in to comment.