Source:
<code>private boolean doWeHandle(SubqueryNode subqueryNode) throws StandardException { boolean existsSubquery = subqueryNode.isEXISTS(); boolean notExistsSubquery = subqueryNode.isNOT_EXISTS(); ResultSetNode subqueryResultSet = subqueryNode.getResultSet(); if (!(existsSubquery || notExistsSubquery)) { return false; } if (!(subqueryResultSet instanceof SelectNode)) { return false; } SelectNode subquerySelectNode = (SelectNode) subqueryResultSet; if (notExistsSubquery && outerSelectNode.getFromList().size() > 1) { return false; } if (!subqueryNode.getUnderTopAndNode()) { return false; } if ((subqueryNode.getOffset() != null || subqueryNode.getFetchFirst() != null)) { return false; } if (!ColumnUtils.isSubtreeCorrelated(subqueryNode)) { return true; } FromList fromList = subqueryResultSet.getFromList(); if (subqueryNode.singleFromBaseTable(fromList) != null) { <START> boolean hasAggregation = subquerySelectNode.hasAggregatesInSelectList() || subquerySelectNode.hasHavingClause(); <END> if (!hasAggregation && !subquerySelectNode.getOriginalWhereClauseHadSubqueries()) return false; } if (subqueryResultSet.getFromList().containsNode(UnionNode.class)) { return isUnionSubqueryOk(subqueryNode, subquerySelectNode.getNestingLevel(), notExistsSubquery); } ValueNode whereClause = subquerySelectNode.getWhereClause(); return whereClause == null || isWhereClauseOk(whereClause, notExistsSubquery, subquerySelectNode.getNestingLevel()); }</code><technical_language>rewrite work properly subquerySelectNode.groupByList.isRollup() is true subquerySelectNode.whereAggregates is empty? wondering flattening for cases if restricted, or, if presence of whereAggregates trigger this rewrite</technical_language>

Target:
private boolean doWeHandle(SubqueryNode subqueryNode) throws StandardException { boolean existsSubquery = subqueryNode.isEXISTS(); boolean notExistsSubquery = subqueryNode.isNOT_EXISTS(); ResultSetNode subqueryResultSet = subqueryNode.getResultSet(); if (!(existsSubquery || notExistsSubquery)) { return false; } if (!(subqueryResultSet instanceof SelectNode)) { return false; } SelectNode subquerySelectNode = (SelectNode) subqueryResultSet; if (notExistsSubquery && outerSelectNode.getFromList().size() > 1) { return false; } if (!subqueryNode.getUnderTopAndNode()) { return false; } if ((subqueryNode.getOffset() != null || subqueryNode.getFetchFirst() != null)) { return false; } if (!ColumnUtils.isSubtreeCorrelated(subqueryNode)) { return true; } FromList fromList = subqueryResultSet.getFromList(); if (subqueryNode.singleFromBaseTable(fromList) != null) { boolean hasAggregation = subquerySelectNode.hasAggregatesInSelectList() || subquerySelectNode.hasHavingClause() || (subquerySelectNode.getGroupByList()!= null && subquerySelectNode.getGroupByList().isRollup()); if (!hasAggregation && !subquerySelectNode.getOriginalWhereClauseHadSubqueries()) return false; } if (subqueryResultSet.getFromList().containsNode(UnionNode.class)) { return isUnionSubqueryOk(subqueryNode, subquerySelectNode.getNestingLevel(), notExistsSubquery); } ValueNode whereClause = subquerySelectNode.getWhereClause(); return whereClause == null || isWhereClauseOk(whereClause, notExistsSubquery, subquerySelectNode.getNestingLevel()); }