Skip to content

Commit

Permalink
[CALCITE-6369] Expanding "star" gives ArrayIndexOutOfBoundsException …
Browse files Browse the repository at this point in the history
…with redundant columns and USING

Fixes expanding * when columns are also specified in the projection list
  • Loading branch information
normanj-bitquill authored and mihaibudiu committed Jun 25, 2024
1 parent 1566f73 commit d74b283
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases,
if (!identifier.isStar()) {
return false;
}
final int originalSize = selectItems.size();
final SqlParserPos startPosition = identifier.getParserPosition();
switch (identifier.names.size()) {
case 1:
Expand Down Expand Up @@ -736,7 +737,7 @@ private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases,
if (!hasDynamicStruct || Bug.CALCITE_2400_FIXED) {
// If some fields before star identifier,
// we should move offset.
int offset = calculatePermuteOffset(selectItems);
int offset = Math.min(calculatePermuteOffset(selectItems), originalSize);
new Permute(from, offset).permute(selectItems, fields);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8318,6 +8318,74 @@ private void checkLiteral2(String expression, String expected) {
.withPostgresql().ok(expectedQuery);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6369">[CALCITE-6369]
* Expanding "star" gives ArrayIndexOutOfBoundsException with redundant columns and USING</a>.
*/
@Test void testUsingClauseWithStarInProjection() {
final String query = "select \"employee_id\", * from \"employee\" e0 join "
+ "\"employee\" e1 using (\"employee_id\")";
final String expectedQuery = "SELECT \"employee\".\"employee_id\", "
+ "\"employee\".\"employee_id\" AS \"employee_id0\", \"employee\".\"full_name\", "
+ "\"employee\".\"first_name\", \"employee\".\"last_name\", \"employee\".\"position_id\", "
+ "\"employee\".\"position_title\", \"employee\".\"store_id\", "
+ "\"employee\".\"department_id\", \"employee\".\"birth_date\", "
+ "\"employee\".\"hire_date\", \"employee\".\"end_date\", \"employee\".\"salary\", "
+ "\"employee\".\"supervisor_id\", \"employee\".\"education_level\", "
+ "\"employee\".\"marital_status\", \"employee\".\"gender\", "
+ "\"employee\".\"management_role\", \"employee0\".\"full_name\" AS \"full_name0\", "
+ "\"employee0\".\"first_name\" AS \"first_name0\", "
+ "\"employee0\".\"last_name\" AS \"last_name0\", "
+ "\"employee0\".\"position_id\" AS \"position_id0\", "
+ "\"employee0\".\"position_title\" AS \"position_title0\", "
+ "\"employee0\".\"store_id\" AS \"store_id0\", "
+ "\"employee0\".\"department_id\" AS \"department_id0\", "
+ "\"employee0\".\"birth_date\" AS \"birth_date0\", "
+ "\"employee0\".\"hire_date\" AS \"hire_date0\", "
+ "\"employee0\".\"end_date\" AS \"end_date0\", \"employee0\".\"salary\" AS \"salary0\", "
+ "\"employee0\".\"supervisor_id\" AS \"supervisor_id0\", "
+ "\"employee0\".\"education_level\" AS \"education_level0\", "
+ "\"employee0\".\"marital_status\" AS \"marital_status0\", "
+ "\"employee0\".\"gender\" AS \"gender0\", "
+ "\"employee0\".\"management_role\" AS \"management_role0\"\n"
+ "FROM \"foodmart\".\"employee\"\n"
+ "INNER JOIN \"foodmart\".\"employee\" AS \"employee0\" ON "
+ "\"employee\".\"employee_id\" = \"employee0\".\"employee_id\"";
sql(query).withPostgresql().ok(expectedQuery);
}

@Test void testUsingClauseWithStarAndAsInProjection() {
final String query = "select \"employee_id\" as \"eid\", * from \"employee\" e0 join "
+ "\"employee\" e1 using (\"employee_id\")";
final String expectedQuery = "SELECT \"employee\".\"employee_id\" AS \"eid\", "
+ "\"employee\".\"employee_id\", \"employee\".\"full_name\", \"employee\".\"first_name\", "
+ "\"employee\".\"last_name\", \"employee\".\"position_id\", "
+ "\"employee\".\"position_title\", \"employee\".\"store_id\", "
+ "\"employee\".\"department_id\", \"employee\".\"birth_date\", "
+ "\"employee\".\"hire_date\", \"employee\".\"end_date\", \"employee\".\"salary\", "
+ "\"employee\".\"supervisor_id\", \"employee\".\"education_level\", "
+ "\"employee\".\"marital_status\", \"employee\".\"gender\", "
+ "\"employee\".\"management_role\", \"employee0\".\"full_name\" AS \"full_name0\", "
+ "\"employee0\".\"first_name\" AS \"first_name0\", "
+ "\"employee0\".\"last_name\" AS \"last_name0\", "
+ "\"employee0\".\"position_id\" AS \"position_id0\", "
+ "\"employee0\".\"position_title\" AS \"position_title0\", "
+ "\"employee0\".\"store_id\" AS \"store_id0\", "
+ "\"employee0\".\"department_id\" AS \"department_id0\", "
+ "\"employee0\".\"birth_date\" AS \"birth_date0\", "
+ "\"employee0\".\"hire_date\" AS \"hire_date0\", "
+ "\"employee0\".\"end_date\" AS \"end_date0\", \"employee0\".\"salary\" AS \"salary0\", "
+ "\"employee0\".\"supervisor_id\" AS \"supervisor_id0\", "
+ "\"employee0\".\"education_level\" AS \"education_level0\", "
+ "\"employee0\".\"marital_status\" AS \"marital_status0\", "
+ "\"employee0\".\"gender\" AS \"gender0\", "
+ "\"employee0\".\"management_role\" AS \"management_role0\"\n"
+ "FROM \"foodmart\".\"employee\"\n"
+ "INNER JOIN \"foodmart\".\"employee\" AS \"employee0\" ON "
+ "\"employee\".\"employee_id\" = \"employee0\".\"employee_id\"";
sql(query).withPostgresql().ok(expectedQuery);
}

/** Fluid interface to run tests. */
static class Sql {
private final CalciteAssert.SchemaSpec schemaSpec;
Expand Down

0 comments on commit d74b283

Please sign in to comment.