Skip to content

Commit

Permalink
Bug fix, support subquery in from doesn't have alias (opendistro-for-…
Browse files Browse the repository at this point in the history
…elasticsearch#418)

(cherry picked from commit 5e5f485)
  • Loading branch information
penghuo committed May 2, 2020
1 parent 014ae01 commit bd9fbe6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ private Select pushSelect(List<SQLSelectItem> selectItems, Select subquerySelect
* for example, subquerySelectItem is "SUM(emp.empno) as TEMP",
* and final select list is TEMP. then return true.
*/
if (fieldAliasRewriter.containsKey(field.getAlias())) {
field.setAlias(fieldAliasRewriter.get(field.getAlias()).apply(field.getAlias()));
String fieldIdentifier = Strings.isNullOrEmpty(field.getAlias()) ? field.getName() : field.getAlias();
if (fieldAliasRewriter.containsKey(fieldIdentifier)) {
field.setAlias(fieldAliasRewriter.get(fieldIdentifier).apply(fieldIdentifier));
} else {
fieldIterator.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.hitAll;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvInt;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvString;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.rows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.schema;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifyDataRows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifySchema;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
Expand Down Expand Up @@ -353,4 +357,26 @@ public void selectFromSubqueryCountAndSum() throws IOException {
assertThat(result.query("/aggregations/count/value"), equalTo(1000));
assertThat(result.query("/aggregations/balance/value"), equalTo(25714837.0));
}

@Test
public void selectFromSubqueryWithoutAliasShouldPass() throws IOException {
JSONObject response = executeJdbcRequest(
StringUtils.format(
"SELECT a.firstname AS my_first, a.lastname AS my_last, a.age AS my_age " +
"FROM (SELECT firstname, lastname, age " +
"FROM %s " +
"WHERE age = 40 and account_number = 291) AS a",
TEST_INDEX_ACCOUNT));

verifySchema(response,
schema("firstname", "my_first", "text"),
schema("lastname", "my_last", "text"),
schema("age", "my_age", "long"));
verifyDataRows(response,
rows("Lynn", "Pollard", 40));
}

private JSONObject executeJdbcRequest(String query) {
return new JSONObject(executeQuery(query, "jdbc"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public void selectFromSubqueryShouldPass() throws SqlParseException {
assertEquals("balance1", select.getFields().get(1).getAlias());
}

@Test
public void selectFromSubqueryWithoutAliasShouldPass() throws SqlParseException {
Select select = parseSelect(
StringUtils.format(
"SELECT t.age as finalAge, t.balance as finalBalance " +
"FROM (SELECT age, balance FROM %s/account) t",
TEST_INDEX_ACCOUNT));

assertEquals(2, select.getFields().size());
assertEquals("age", select.getFields().get(0).getName());
assertEquals("finalAge", select.getFields().get(0).getAlias());
assertEquals("balance", select.getFields().get(1).getName());
assertEquals("finalBalance", select.getFields().get(1).getAlias());
}

@Test
public void selectFromSubqueryShouldIgnoreUnusedField() throws SqlParseException {
Select select = parseSelect(
Expand Down

0 comments on commit bd9fbe6

Please sign in to comment.