Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make explain of ProjectOperator with alias show both name and alias info #2911

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you should add a new method getNameWithAlias() in this class similar to getNameOrAlias().
And I have some concerns when to use getNameOrAlias() and when to use getNameWithAlias()?
Could you deep dive in it?

Copy link
Contributor Author

@qianheng-aws qianheng-aws Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNameOrAlias are used to get the field name to use in other operator. While in its toString, I just want to add more info than the field name, and it won't break the usage since it's just for explanation, no execution affection.

I think we don't need to add such a new function as it will only be used in this toString method if added, we should add it unless it can be reused in other places as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add it unless it can be reused in other places as well.

I doubt are there any other places to replace to getNameWithAlias either as toString does. Can you help to confirm them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having scanned over all the usage of getNameOrAlias, I don't see any other places need to be replaced. They are all related to execution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @qianheng-aws . @dai-chen Can u help to double confirm it since u are the author of this class?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I can't recall the specific details. I think it should be safe to change the toString method only.

}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,23 @@ 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 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+", "")));
}
}
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": "[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\",\"city\",\"balance\"],\"excludes\":[]}},searchDone=false)"
},
"children": []
}
]
}
}
Loading