Skip to content

Commit

Permalink
[CALCITE-6231] JDBC adapter generates "UNNEST" when it should generat…
Browse files Browse the repository at this point in the history
…e "UNNEST ... WITH ORDINALITY"
  • Loading branch information
YiwenWu authored and macroguo-ghy committed Jan 31, 2024
1 parent de39888 commit 351ddeb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.apache.calcite.sql.SqlMerge;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSampleSpec;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.SqlTableRef;
Expand Down Expand Up @@ -1272,8 +1273,9 @@ private static SqlCall as(SqlNode e, String alias) {

public Result visit(Uncollect e) {
final Result x = visitInput(e, 0);
final SqlNode unnestNode =
SqlStdOperatorTable.UNNEST.createCall(POS, x.asStatement());
final SqlOperator operator =
e.withOrdinality ? SqlStdOperatorTable.UNNEST_WITH_ORDINALITY : SqlStdOperatorTable.UNNEST;
final SqlNode unnestNode = operator.createCall(POS, x.asStatement());
final List<SqlNode> operands =
createAsFullOperands(e.getRowType(), unnestNode,
requireNonNull(x.neededAlias,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6376,6 +6376,31 @@ private void checkLiteral2(String expression, String expected) {
sql(sql).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6231">[CALCITE-6231]
* JDBC adapter generates "UNNEST" when it should generate "UNNEST ... WITH ORDINALITY" </a>.
*/
@Test void testUncollectExplicitAliasWithOrd() {
final String sql = "select did + 1\n"
+ "from unnest(select collect(\"department_id\") as deptid \n"
+ "from \"department\") with ordinality as t(did, pos)";

final String expected = "SELECT \"DEPTID\" + 1\n"
+ "FROM UNNEST (SELECT COLLECT(\"department_id\") AS \"DEPTID\"\n"
+ "FROM \"foodmart\".\"department\") WITH ORDINALITY AS \"t0\" (\"DEPTID\", \"ORDINALITY\")";
sql(sql).ok(expected);
}

@Test void testUncollectImplicitAliasWithOrd() {
final String sql = "select did + 1\n"
+ "from unnest(select collect(\"department_id\") \n"
+ "from \"department\") with ordinality as t(did, pos)";

final String expected = "SELECT \"col_0\" + 1\n"
+ "FROM UNNEST (SELECT COLLECT(\"department_id\")\n"
+ "FROM \"foodmart\".\"department\") WITH ORDINALITY AS \"t0\" (\"col_0\", \"ORDINALITY\")";
sql(sql).ok(expected);
}

@Test void testWithinGroup1() {
final String query = "select \"product_class_id\", collect(\"net_weight\") "
Expand Down

0 comments on commit 351ddeb

Please sign in to comment.