Skip to content

Commit

Permalink
IGNITE-22738 Sql: Fix nested subquery optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-plekhanov committed Jul 15, 2024
1 parent 2448ffa commit 6dc7edb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ private static boolean pullOutSubQryFromTableList(
remapColumns(
parent,
subSel,
// reference equality used intentionally here
col -> wrappedSubQry == col.expressionInFrom(),
// In case of several nested subqueries, inner subqueries are wrapped into alias of outer subqueries,
// to check column belonging correctly we should unwrap aliases.
// Reference equality used intentionally here.
col -> GridSqlAlias.unwrap(wrappedSubQry) == GridSqlAlias.unwrap(col.expressionInFrom()),
subTbl
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,22 @@ public void testDifferentOperatorsWithSubqueryUnderSelect() {
}
}

/**
* Case when select subquery from another subquery, which is select from another subquery.
*/
@Test
public void testSeveralNestedSubqueries() {
String innerSql = "SELECT id as id0, name as name0 FROM dep";
String outerSqlTemplate = "SELECT id%d as id%d, name%d as name%d FROM %s WHERE id%d > %d";

String curSql = innerSql;

for (int i = 0; i < 5; i++) {
curSql = String.format(outerSqlTemplate, i, i + 1, i, i + 1, '(' + curSql + ')', i, i);
check(curSql, 1);
}
}

/**
* @param sql Sql.
* @param expSelectClauses Expected select clauses.
Expand Down

0 comments on commit 6dc7edb

Please sign in to comment.