Skip to content

Commit

Permalink
[CALCITE-6563] RelToSqlConverter should not merge two window functions
Browse files Browse the repository at this point in the history
  • Loading branch information
suibianwanwank committed Sep 4, 2024
1 parent b1308fe commit 68d3148
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1910,11 +1910,15 @@ private boolean needNewSubQuery(
return true;
}

// Cannot merge two window functions
boolean containsOver = containsOver(node);
if (rel instanceof Project
&& ((Project) rel).containsOver()
&& maxClause == Clause.SELECT) {
// Cannot merge a Project that contains windowed functions onto an
// underlying Project
&& containsOver) {
return true;
}

if (rel instanceof Window && containsOver) {
return true;
}

Expand Down Expand Up @@ -2006,6 +2010,31 @@ private boolean hasSortByOrdinal(@UnknownInitialization Result this,
return false;
}

private boolean containsOver(@UnknownInitialization Result this,
@Nullable SqlNode node) {
if (node == null) {
return false;
}
if (node.getKind() == SqlKind.WINDOW) {
return true;
}
if (node instanceof SqlSelect) {
final SqlNodeList selectList = ((SqlSelect) node).getSelectList();
for (SqlNode child : selectList) {
if (containsOver(child)) {
return true;
}
}
} else if (node instanceof SqlBasicCall) {
for (SqlNode operand : ((SqlBasicCall) node).getOperandList()) {
if (containsOver(operand)) {
return true;
}
}
}
return false;
}

/** Returns whether an {@link Aggregate} contains nested operands that
* match the predicate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4847,6 +4847,41 @@ private void checkLiteral2(String expression, String expected) {
.withPostgresql().ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6563">[CALCITE-6563]
* RelToSqlConverter should not merge two window functions</a>. */
@Test void testConvertNestWindowToSql() {
String query0 = " SELECT "
+ "RANK() OVER (ORDER BY \"daily_sales\" DESC) AS \"rank1\" "
+ "FROM ( SELECT \"product_name\", "
+ "SUM(\"product_id\") OVER (PARTITION BY \"product_name\") AS \"daily_sales\" "
+ "FROM \"product\" ) subquery";
String expected00 = "SELECT RANK() OVER (ORDER BY \"$1\" DESC) AS \"$0\"\n"
+ "FROM (SELECT \"product_name\", SUM(\"product_id\") OVER (PARTITION BY \"product_name\" "
+ "RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"$1\"\n"
+ "FROM \"foodmart\".\"product\") AS \"t0\"";
String expected01 = "SELECT RANK() OVER (ORDER BY \"daily_sales\" DESC) AS \"rank1\"\n"
+ "FROM (SELECT \"product_name\", SUM(\"product_id\") OVER (PARTITION BY \"product_name\""
+ " RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS \"daily_sales\"\n"
+ "FROM \"foodmart\".\"product\") AS \"t\"";
RuleSet rules = RuleSets.ofList(CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW);
// PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW rule will remove alias
sql(query0).optimize(rules, null).ok(expected00);
sql(query0).ok(expected01);

String query1 = " SELECT \"product_id\","
+ "RANK() OVER (ORDER BY \"product_name\" DESC) AS \"rank1\" "
+ "FROM (SELECT \"product_id\", \"product_name\" FROM \"product\") a";
String expected10 = "SELECT \"product_id\","
+ " RANK() OVER (ORDER BY \"product_name\" DESC) AS \"$1\"\n"
+ "FROM \"foodmart\".\"product\"";
String expected11 = "SELECT \"product_id\","
+ " RANK() OVER (ORDER BY \"product_name\" DESC) AS \"rank1\"\n"
+ "FROM \"foodmart\".\"product\"";
sql(query1).optimize(rules, null).ok(expected10);
sql(query1).ok(expected11);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1798">[CALCITE-1798]
* Generate dialect-specific SQL for FLOOR operator</a>. */
Expand Down

0 comments on commit 68d3148

Please sign in to comment.