@@ -256,6 +256,65 @@ private static String toSql(RelNode root, SqlDialect dialect,
256256 sql(query).withMysql().ok(expected);
257257 }
258258
259+ /** Test case for
260+ * <a href="https://issues.apache.org/jira/browse/CALCITE-2152">[CALCITE-2152]
261+ * SQL parser unable to parse SQL with nested joins produced by RelToSqlConverter</a>. */
262+ @Test public void testNestedJoin() {
263+ final String query = "select *\n"
264+ + "from \"sales_fact_1997\"\n"
265+ + "inner join (select * from \"customer\"\n"
266+ + "inner join \"employee\" on (\"customer\".\"city\" = \"employee\".\"store_id\") ) AS \"customer_employee\"\n"
267+ + "on (\"sales_fact_1997\".\"store_id\" = \"customer_employee\".\"city\")";
268+ String expected = "SELECT \"sales_fact_1997\".\"product_id\", \"sales_fact_1997\".\"time_id\","
269+ + " \"sales_fact_1997\".\"customer_id\", \"sales_fact_1997\".\"promotion_id\","
270+ + " \"sales_fact_1997\".\"store_id\", \"sales_fact_1997\".\"store_sales\","
271+ + " \"sales_fact_1997\".\"store_cost\", \"sales_fact_1997\".\"unit_sales\","
272+ + " \"t0\".\"customer_id\" AS \"customer_id0\", \"t0\".\"account_num\", \"t0\".\"lname\","
273+ + " \"t0\".\"fname\", \"t0\".\"mi\", \"t0\".\"address1\", \"t0\".\"address2\","
274+ + " \"t0\".\"address3\", \"t0\".\"address4\", \"t0\".\"city\", \"t0\".\"state_province\","
275+ + " \"t0\".\"postal_code\", \"t0\".\"country\", \"t0\".\"customer_region_id\","
276+ + " \"t0\".\"phone1\", \"t0\".\"phone2\", \"t0\".\"birthdate\", \"t0\".\"marital_status\","
277+ + " \"t0\".\"yearly_income\", \"t0\".\"gender\", \"t0\".\"total_children\","
278+ + " \"t0\".\"num_children_at_home\", \"t0\".\"education\", \"t0\".\"date_accnt_opened\","
279+ + " \"t0\".\"member_card\", \"t0\".\"occupation\", \"t0\".\"houseowner\","
280+ + " \"t0\".\"num_cars_owned\", \"t0\".\"fullname\", \"t0\".\"employee_id\","
281+ + " \"t0\".\"full_name\", \"t0\".\"first_name\", \"t0\".\"last_name\","
282+ + " \"t0\".\"position_id\", \"t0\".\"position_title\", \"t0\".\"store_id\" AS \"store_id0\","
283+ + " \"t0\".\"department_id\", \"t0\".\"birth_date\", \"t0\".\"hire_date\","
284+ + " \"t0\".\"end_date\", \"t0\".\"salary\", \"t0\".\"supervisor_id\","
285+ + " \"t0\".\"education_level\", \"t0\".\"marital_status0\", \"t0\".\"gender0\","
286+ + " \"t0\".\"management_role\"\n"
287+ + "FROM \"foodmart\".\"sales_fact_1997\"\n"
288+ + "INNER JOIN (SELECT \"t\".\"customer_id\", \"t\".\"account_num\", \"t\".\"lname\","
289+ + " \"t\".\"fname\", \"t\".\"mi\", \"t\".\"address1\", \"t\".\"address2\","
290+ + " \"t\".\"address3\", \"t\".\"address4\", \"t\".\"city\", \"t\".\"state_province\","
291+ + " \"t\".\"postal_code\", \"t\".\"country\", \"t\".\"customer_region_id\","
292+ + " \"t\".\"phone1\", \"t\".\"phone2\", \"t\".\"birthdate\", \"t\".\"marital_status\","
293+ + " \"t\".\"yearly_income\", \"t\".\"gender\", \"t\".\"total_children\","
294+ + " \"t\".\"num_children_at_home\", \"t\".\"education\", \"t\".\"date_accnt_opened\","
295+ + " \"t\".\"member_card\", \"t\".\"occupation\", \"t\".\"houseowner\","
296+ + " \"t\".\"num_cars_owned\", \"t\".\"fullname\", \"employee\".\"employee_id\","
297+ + " \"employee\".\"full_name\", \"employee\".\"first_name\", \"employee\".\"last_name\","
298+ + " \"employee\".\"position_id\", \"employee\".\"position_title\", \"employee\".\"store_id\","
299+ + " \"employee\".\"department_id\", \"employee\".\"birth_date\", \"employee\".\"hire_date\","
300+ + " \"employee\".\"end_date\", \"employee\".\"salary\", \"employee\".\"supervisor_id\","
301+ + " \"employee\".\"education_level\", \"employee\".\"marital_status\" AS \"marital_status0\","
302+ + " \"employee\".\"gender\" AS \"gender0\", \"employee\".\"management_role\","
303+ + " CAST(\"t\".\"city\" AS INTEGER) AS \"city0\"\n"
304+ + "FROM (SELECT \"customer_id\","
305+ + " \"account_num\", \"lname\", \"fname\", \"mi\", \"address1\", \"address2\","
306+ + " \"address3\", \"address4\", \"city\", \"state_province\", \"postal_code\","
307+ + " \"country\", \"customer_region_id\", \"phone1\", \"phone2\", \"birthdate\","
308+ + " \"marital_status\", \"yearly_income\", \"gender\", \"total_children\","
309+ + " \"num_children_at_home\", \"education\", \"date_accnt_opened\", \"member_card\","
310+ + " \"occupation\", \"houseowner\", \"num_cars_owned\", \"fullname\","
311+ + " CAST(\"city\" AS INTEGER) AS \"city0\"\n"
312+ + "FROM \"foodmart\".\"customer\") AS \"t\"\n"
313+ + "INNER JOIN \"foodmart\".\"employee\" ON \"t\".\"city0\" = \"employee\".\"store_id\") AS \"t0\""
314+ + " ON \"sales_fact_1997\".\"store_id\" = \"t0\".\"city0\"";
315+ sql(query).ok(expected);
316+ }
317+
259318 /**
260319 * Test for <a href="https://issues.apache.org/jira/browse/CALCITE-4723">[CALCITE-4723]</a>
261320 * Check whether JDBC adapter generates "GROUP BY ()" against Oracle, DB2, MSSQL.
0 commit comments