[jira] [Resolved] (CALCITE-2153) SQL parser fails to parse valid multi nested join subqueries

2018-01-29 Thread Julian Hyde (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2153?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Julian Hyde resolved CALCITE-2153.
--
Resolution: Duplicate

This is a duplicate of CALCITE-35, I believe. A long-standing issue, we'd like 
to fix, but we've not so far managed to resolve ambiguities in the parser.

> SQL parser fails to parse valid multi nested join subqueries
> 
>
> Key: CALCITE-2153
> URL: https://issues.apache.org/jira/browse/CALCITE-2153
> Project: Calcite
>  Issue Type: Bug
>Reporter: Samuel Waggoner
>Assignee: Julian Hyde
>Priority: Major
>
> I started working on a unit test in SqlParserTest (it's hard to predict exact 
> output, so I just have a placeholder empty string for expectation right now). 
> {code:java}
> @Test public void testInnerJoinOnSubqueryWithAggregation() {
>  final String sql = "select *\n"
>  + "from table1 \n"
>  + "inner join (( \n"
>  + " select * \n"
>  + " from table2 ) \n"
>  + " inner join ( \n"
>  + "   select * \n"
>  + "   from table3) \n"
>  + " on table2.field1 = table3.field1) \n"
>  + "on table1.field1 = table2.field1 \n";
>  sql(sql).ok("");
> }{code}
> I believe this is valid SQL, but parsing fails with this exception
>  
> {code:java}
> java.lang.RuntimeException: Error while parsing SQL: select *
> from table1 
> inner join (( 
>  select * 
>  from table2 ) 
>  inner join ( 
>  select * 
>  from table3) 
>  on table2.field1 = table3.field1) 
> on table1.field1 = table2.field1
>  at 
> org.apache.calcite.sql.parser.SqlParserTest$TesterImpl.parseStmtAndHandleEx(SqlParserTest.java:8201)
>  at 
> org.apache.calcite.sql.parser.SqlParserTest$TesterImpl.check(SqlParserTest.java:8186)
>  at 
> org.apache.calcite.sql.parser.SqlParserTest$Sql.ok(SqlParserTest.java:8384)
>  at 
> org.apache.calcite.sql.parser.SqlParserTest.testInnerJoinOnSubqueryWithAggregation(SqlParserTest.java:8156)
>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>  at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>  at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>  at java.lang.reflect.Method.invoke(Method.java:498)
>  at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>  at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>  at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>  at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
>  at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
>  at 
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
>  at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
>  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
> Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered 
> "inner" at line 6, column 3.
> Was expecting one of:
>  ")" ...
>  "ORDER" ...
>  "LIMIT" ...
>  "OFFSET" ...
>  "FETCH" ...
>  "UNION" ...
>  "INTERSECT" ...
>  "EXCEPT" ...
>  "MINUS" ...
>  "NOT" ...
>  "IN" ...
>  "<" ...
>  "<=" ...
>  ">" ...
>  ">=" ...
>  "=" ...
>  "<>" ...
>  "!=" ...
>  "BETWEEN" ...
>  "LIKE" ...
>  "SIMILAR" ...
>  "+" ...
>  "-" ...
>  "*" ...
>  "/" ...
>  "%" ...
>  "||" ...
>  "AND" ...
>  "OR" ...
>  "IS" ...
>  "MEMBER" ...
>  "SUBMULTISET" ...
>  "CONTAINS" ...
>  "OVERLAPS" ...
>  "EQUALS" ...
>  "PRECEDES" ...
>  "SUCCEEDS" ...
>  "IMMEDIATELY" ...
>  "MULTISET" ...
>  "[" ...
>  "YEAR" ...
>  "MONTH" ...
>  "DAY" ...
>  "HOUR" ...
>  "MINUTE" ...
>  "SECOND" ...
>  
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
>  at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
>  at 

[jira] [Commented] (CALCITE-2152) SQL parser unable to parse SQL with nested joins produced by RelToSqlConverter

2018-01-29 Thread Julian Hyde (JIRA)

[ 
https://issues.apache.org/jira/browse/CALCITE-2152?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16344364#comment-16344364
 ] 

Julian Hyde commented on CALCITE-2152:
--

You are hitting a known limitation to the parser, see CALCITE-35. We would like 
to fix this, but it has proven hard.

A workaround might be to generate "(SELECT FROM x JOIN y ON ...) AS xy" rather 
than "(x JOIN y ON ...)". The effect is slightly different, because in the 
former the columns all belong to one table alias "xy", and in the latter they 
belong to two aliases "x" and "y".

> SQL parser unable to parse SQL with nested joins produced by RelToSqlConverter
> --
>
> Key: CALCITE-2152
> URL: https://issues.apache.org/jira/browse/CALCITE-2152
> Project: Calcite
>  Issue Type: Bug
>Reporter: Samuel Waggoner
>Assignee: Julian Hyde
>Priority: Major
>
> I created this test case in RelToSqlConverterTest:
> {code:java}
> @Test
> public void testNestedJoin() {
> final String query = "select *"
> + "from \"sales_fact_1997\" "
> + "inner join (select * from \"customer\""
> + "inner join \"employee\" on (\"customer\".\"city\" = 
> \"employee\".\"store_id\") ) AS \"customer_employee\""
> + "on (\"sales_fact_1997\".\"store_id\" = \"customer_employee\".\"city\")";
> String result = sql(query).exec();
> System.out.println(result);
> sql(result).exec();
> }
> {code}
> result looks like this:
> {code:java}
> SELECT *
>  FROM "foodmart"."sales_fact_1997"
>  INNER JOIN ("foodmart"."customer"
>  INNER JOIN "foodmart"."employee" ON "customer"."city" = 
> "employee"."store_id") ON "sales_fact_1997"."store_id" = 
> "customer"."city"{code}
>  
> The resulting stack trace:
>  
> {code:java}
> java.lang.RuntimeException: org.apache.calcite.sql.parser.SqlParseException: 
> Non-query expression encountered in illegal context
> at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2420)
>  at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.testNestedJoin(RelToSqlConverterTest.java:2314)
>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>  at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>  at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>  at java.lang.reflect.Method.invoke(Method.java:498)
>  at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>  at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>  at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>  at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
>  at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
>  at 
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
>  at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
>  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
>  Caused by: org.apache.calcite.sql.parser.SqlParseException: Non-query 
> expression encountered in illegal context
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
>  at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
>  at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:163)
>  at org.apache.calcite.prepare.PlannerImpl.parse(PlannerImpl.java:179)
>  at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2407)
>  ... 23 more
>  Caused by: org.apache.calcite.runtime.CalciteException: Non-query expression 
> encountered in illegal context
>  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>  at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>  at 
> 

[jira] [Updated] (CALCITE-2153) SQL parser fails to parse valid multi nested join subqueries

2018-01-29 Thread Samuel Waggoner (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2153?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Samuel Waggoner updated CALCITE-2153:
-
Description: 
I started working on a unit test in SqlParserTest (it's hard to predict exact 
output, so I just have a placeholder empty string for expectation right now). 
{code:java}
@Test public void testInnerJoinOnSubqueryWithAggregation() {

 final String sql = "select *\n"
 + "from table1 \n"
 + "inner join (( \n"
 + " select * \n"
 + " from table2 ) \n"
 + " inner join ( \n"
 + "   select * \n"
 + "   from table3) \n"
 + " on table2.field1 = table3.field1) \n"
 + "on table1.field1 = table2.field1 \n";

 sql(sql).ok("");
}{code}
I believe this is valid SQL, but parsing fails with this exception

 
{code:java}
java.lang.RuntimeException: Error while parsing SQL: select *
from table1 
inner join (( 
 select * 
 from table2 ) 
 inner join ( 
 select * 
 from table3) 
 on table2.field1 = table3.field1) 
on table1.field1 = table2.field1

 at 
org.apache.calcite.sql.parser.SqlParserTest$TesterImpl.parseStmtAndHandleEx(SqlParserTest.java:8201)
 at 
org.apache.calcite.sql.parser.SqlParserTest$TesterImpl.check(SqlParserTest.java:8186)
 at org.apache.calcite.sql.parser.SqlParserTest$Sql.ok(SqlParserTest.java:8384)
 at 
org.apache.calcite.sql.parser.SqlParserTest.testInnerJoinOnSubqueryWithAggregation(SqlParserTest.java:8156)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
 at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
 at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
 at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
 at 
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
 at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered "inner" 
at line 6, column 3.
Was expecting one of:
 ")" ...
 "ORDER" ...
 "LIMIT" ...
 "OFFSET" ...
 "FETCH" ...
 "UNION" ...
 "INTERSECT" ...
 "EXCEPT" ...
 "MINUS" ...
 "NOT" ...
 "IN" ...
 "<" ...
 "<=" ...
 ">" ...
 ">=" ...
 "=" ...
 "<>" ...
 "!=" ...
 "BETWEEN" ...
 "LIKE" ...
 "SIMILAR" ...
 "+" ...
 "-" ...
 "*" ...
 "/" ...
 "%" ...
 "||" ...
 "AND" ...
 "OR" ...
 "IS" ...
 "MEMBER" ...
 "SUBMULTISET" ...
 "CONTAINS" ...
 "OVERLAPS" ...
 "EQUALS" ...
 "PRECEDES" ...
 "SUCCEEDS" ...
 "IMMEDIATELY" ...
 "MULTISET" ...
 "[" ...
 "YEAR" ...
 "MONTH" ...
 "DAY" ...
 "HOUR" ...
 "MINUTE" ...
 "SECOND" ...
 
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
 at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
 at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:163)
 at 
org.apache.calcite.sql.parser.SqlParserTest$TesterImpl.parseStmtAndHandleEx(SqlParserTest.java:8199)
 ... 25 more
Caused by: org.apache.calcite.sql.parser.impl.ParseException: Encountered 
"inner" at line 6, column 3.
Was expecting one of:
 ")" ...
 "ORDER" ...
 "LIMIT" ...
 "OFFSET" ...
 "FETCH" ...
 "UNION" ...
 "INTERSECT" ...
 "EXCEPT" ...
 "MINUS" ...
 "NOT" ...
 "IN" ...
 "<" ...
 "<=" ...
 ">" ...
 ">=" ...
 "=" ...
 "<>" ...
 "!=" ...
 "BETWEEN" ...
 "LIKE" ...
 "SIMILAR" ...
 "+" ...
 "-" ...
 "*" ...
 "/" ...
 "%" ...
 "||" ...
 "AND" ...
 "OR" ...
 "IS" ...
 "MEMBER" ...
 "SUBMULTISET" ...
 "CONTAINS" ...
 "OVERLAPS" ...
 "EQUALS" ...
 "PRECEDES" ...
 "SUCCEEDS" ...
 "IMMEDIATELY" ...
 "MULTISET" ...
 "[" ...
 "YEAR" ...
 "MONTH" ...
 "DAY" ...
 "HOUR" ...
 "MINUTE" ...
 

[jira] [Updated] (CALCITE-2153) SQL parser fails to parse valid multi nested join subqueries

2018-01-29 Thread Samuel Waggoner (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2153?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Samuel Waggoner updated CALCITE-2153:
-
Description: 
I started working on a unit test in SqlParserTest (it's hard to predict exact 
output, so just have placeholder empty string for expectation right now). 
{code:java}
@Test public void testInnerJoinOnSubqueryWithAggregation() {

 final String sql = "select *\n"
 + "from table1 \n"
 + "inner join (( \n"
 + " select * \n"
 + " from table2 ) \n"
 + " inner join ( \n"
 + "   select * \n"
 + "   from table3) \n"
 + " on table2.field1 = table3.field1) \n"
 + "on table1.field1 = table2.field1 \n";

 sql(sql).ok("");
}{code}

  was:
{code:java}
@Test public void testInnerJoinOnSubqueryWithAggregation() {

 final String sql = "select *\n"
 + "from table1 \n"
 + "inner join (( \n"
 + " select * \n"
 + " from table2 ) \n"
 + " inner join ( \n"
 + " select * \n"
 + " from table3) \n"
 + " on table2.field1 = table3.field1) \n"
 + "on table1.field1 = table2.field1 \n";

 sql(sql).ok("");
}{code}


> SQL parser fails to parse valid multi nested join subqueries
> 
>
> Key: CALCITE-2153
> URL: https://issues.apache.org/jira/browse/CALCITE-2153
> Project: Calcite
>  Issue Type: Bug
>Reporter: Samuel Waggoner
>Assignee: Julian Hyde
>Priority: Major
>
> I started working on a unit test in SqlParserTest (it's hard to predict exact 
> output, so just have placeholder empty string for expectation right now). 
> {code:java}
> @Test public void testInnerJoinOnSubqueryWithAggregation() {
>  final String sql = "select *\n"
>  + "from table1 \n"
>  + "inner join (( \n"
>  + " select * \n"
>  + " from table2 ) \n"
>  + " inner join ( \n"
>  + "   select * \n"
>  + "   from table3) \n"
>  + " on table2.field1 = table3.field1) \n"
>  + "on table1.field1 = table2.field1 \n";
>  sql(sql).ok("");
> }{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Updated] (CALCITE-2153) SQL parser fails to parse valid multi nested join subqueries

2018-01-29 Thread Samuel Waggoner (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2153?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Samuel Waggoner updated CALCITE-2153:
-
Summary: SQL parser fails to parse valid multi nested join subqueries  
(was: SQL parser fails to parse valid triply nested join query)

> SQL parser fails to parse valid multi nested join subqueries
> 
>
> Key: CALCITE-2153
> URL: https://issues.apache.org/jira/browse/CALCITE-2153
> Project: Calcite
>  Issue Type: Bug
>Reporter: Samuel Waggoner
>Assignee: Julian Hyde
>Priority: Major
>
> {code:java}
> @Test public void testInnerJoinOnSubqueryWithAggregation() {
>  final String sql = "select *\n"
>  + "from table1 \n"
>  + "inner join (( \n"
>  + " select * \n"
>  + " from table2 ) \n"
>  + " inner join ( \n"
>  + " select * \n"
>  + " from table3) \n"
>  + " on table2.field1 = table3.field1) \n"
>  + "on table1.field1 = table2.field1 \n";
>  sql(sql).ok("");
> }{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Created] (CALCITE-2153) SQL parser fails to parse valid triply nested join query

2018-01-29 Thread Samuel Waggoner (JIRA)
Samuel Waggoner created CALCITE-2153:


 Summary: SQL parser fails to parse valid triply nested join query
 Key: CALCITE-2153
 URL: https://issues.apache.org/jira/browse/CALCITE-2153
 Project: Calcite
  Issue Type: Bug
Reporter: Samuel Waggoner
Assignee: Julian Hyde


{code:java}
@Test public void testInnerJoinOnSubqueryWithAggregation() {

 final String sql = "select *\n"
 + "from table1 \n"
 + "inner join (( \n"
 + " select * \n"
 + " from table2 ) \n"
 + " inner join ( \n"
 + " select * \n"
 + " from table3) \n"
 + " on table2.field1 = table3.field1) \n"
 + "on table1.field1 = table2.field1 \n";

 sql(sql).ok("");
}{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Updated] (CALCITE-2152) SQL parser unable to parse SQL with nested joins produced by RelToSqlConverter

2018-01-29 Thread Samuel Waggoner (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2152?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Samuel Waggoner updated CALCITE-2152:
-
Summary: SQL parser unable to parse SQL with nested joins produced by 
RelToSqlConverter  (was: SQL parser unable to parse SQL produced by 
RelToSqlConverter)

> SQL parser unable to parse SQL with nested joins produced by RelToSqlConverter
> --
>
> Key: CALCITE-2152
> URL: https://issues.apache.org/jira/browse/CALCITE-2152
> Project: Calcite
>  Issue Type: Bug
>Reporter: Samuel Waggoner
>Assignee: Julian Hyde
>Priority: Major
>
> I created this test case in RelToSqlConverterTest:
> {code:java}
> @Test
> public void testNestedJoin() {
> final String query = "select *"
> + "from \"sales_fact_1997\" "
> + "inner join (select * from \"customer\""
> + "inner join \"employee\" on (\"customer\".\"city\" = 
> \"employee\".\"store_id\") ) AS \"customer_employee\""
> + "on (\"sales_fact_1997\".\"store_id\" = \"customer_employee\".\"city\")";
> String result = sql(query).exec();
> System.out.println(result);
> sql(result).exec();
> }
> {code}
> result looks like this:
> {code:java}
> SELECT *
>  FROM "foodmart"."sales_fact_1997"
>  INNER JOIN ("foodmart"."customer"
>  INNER JOIN "foodmart"."employee" ON "customer"."city" = 
> "employee"."store_id") ON "sales_fact_1997"."store_id" = 
> "customer"."city"{code}
>  
> The resulting stack trace:
>  
> {code:java}
> java.lang.RuntimeException: org.apache.calcite.sql.parser.SqlParseException: 
> Non-query expression encountered in illegal context
> at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2420)
>  at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.testNestedJoin(RelToSqlConverterTest.java:2314)
>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>  at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>  at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>  at java.lang.reflect.Method.invoke(Method.java:498)
>  at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>  at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>  at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>  at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>  at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
>  at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
>  at 
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
>  at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
>  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
>  Caused by: org.apache.calcite.sql.parser.SqlParseException: Non-query 
> expression encountered in illegal context
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
>  at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
>  at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:163)
>  at org.apache.calcite.prepare.PlannerImpl.parse(PlannerImpl.java:179)
>  at 
> org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2407)
>  ... 23 more
>  Caused by: org.apache.calcite.runtime.CalciteException: Non-query expression 
> encountered in illegal context
>  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>  at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>  at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>  at 
> org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:463)
>  at 

[jira] [Updated] (CALCITE-2152) SQL parser unable to parse SQL produced by RelToSqlConverter

2018-01-29 Thread Samuel Waggoner (JIRA)

 [ 
https://issues.apache.org/jira/browse/CALCITE-2152?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Samuel Waggoner updated CALCITE-2152:
-
Description: 
I created this test case in RelToSqlConverterTest:
{code:java}
@Test
public void testNestedJoin() {
final String query = "select *"
+ "from \"sales_fact_1997\" "
+ "inner join (select * from \"customer\""
+ "inner join \"employee\" on (\"customer\".\"city\" = 
\"employee\".\"store_id\") ) AS \"customer_employee\""
+ "on (\"sales_fact_1997\".\"store_id\" = \"customer_employee\".\"city\")";

String result = sql(query).exec();
System.out.println(result);
sql(result).exec();
}
{code}

result looks like this:
{code:java}
SELECT *
 FROM "foodmart"."sales_fact_1997"
 INNER JOIN ("foodmart"."customer"
 INNER JOIN "foodmart"."employee" ON "customer"."city" = "employee"."store_id") 
ON "sales_fact_1997"."store_id" = "customer"."city"{code}
 

The resulting stack trace:

 
{code:java}
java.lang.RuntimeException: org.apache.calcite.sql.parser.SqlParseException: 
Non-query expression encountered in illegal context
at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2420)
 at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.testNestedJoin(RelToSqlConverterTest.java:2314)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
 at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
 at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
 at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
 at 
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
 at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
 Caused by: org.apache.calcite.sql.parser.SqlParseException: Non-query 
expression encountered in illegal context
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
 at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
 at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:163)
 at org.apache.calcite.prepare.PlannerImpl.parse(PlannerImpl.java:179)
 at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2407)
 ... 23 more
 Caused by: org.apache.calcite.runtime.CalciteException: Non-query expression 
encountered in illegal context
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:463)
 at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:572)
 at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:803)
 at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:788)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.checkNonQueryExpression(SqlParserImpl.java:294)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression3(SqlParserImpl.java:3323)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2b(SqlParserImpl.java:3069)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2(SqlParserImpl.java:3095)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression(SqlParserImpl.java:3048)
 at 

[jira] [Created] (CALCITE-2152) SQL parser unable to parse SQL produced by RelToSqlConverter

2018-01-29 Thread Samuel Waggoner (JIRA)
Samuel Waggoner created CALCITE-2152:


 Summary: SQL parser unable to parse SQL produced by 
RelToSqlConverter
 Key: CALCITE-2152
 URL: https://issues.apache.org/jira/browse/CALCITE-2152
 Project: Calcite
  Issue Type: Bug
Reporter: Samuel Waggoner
Assignee: Julian Hyde


I created this test case in RelToSqlConverterTest
```

@Test
public void testNestedJoin() {
 final String query = "select *"
 + "from \"sales_fact_1997\" "
 + "inner join (select * from \"customer\""
 + "inner join \"employee\" on (\"customer\".\"city\" = 
\"employee\".\"store_id\") ) AS \"customer_employee\""
 + "on (\"sales_fact_1997\".\"store_id\" = \"customer_employee\".\"city\")";

 String result = sql(query).exec();
 System.out.println(result);
 sql(result).exec();
}
```

result looks like this:
```SELECT *
FROM "foodmart"."sales_fact_1997"
INNER JOIN ("foodmart"."customer"
INNER JOIN "foodmart"."employee" ON "customer"."city" = "employee"."store_id") 
ON "sales_fact_1997"."store_id" = "customer"."city"```

Stack Trace is:
```

java.lang.RuntimeException: org.apache.calcite.sql.parser.SqlParseException: 
Non-query expression encountered in illegal context

at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2420)
 at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest.testNestedJoin(RelToSqlConverterTest.java:2314)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
 at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
 at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
 at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
 at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
 at 
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
 at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.calcite.sql.parser.SqlParseException: Non-query 
expression encountered in illegal context
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:350)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:131)
 at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:138)
 at org.apache.calcite.sql.parser.SqlParser.parseStmt(SqlParser.java:163)
 at org.apache.calcite.prepare.PlannerImpl.parse(PlannerImpl.java:179)
 at 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest$Sql.exec(RelToSqlConverterTest.java:2407)
 ... 23 more
Caused by: org.apache.calcite.runtime.CalciteException: Non-query expression 
encountered in illegal context
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:463)
 at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:572)
 at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:803)
 at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:788)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.checkNonQueryExpression(SqlParserImpl.java:294)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression3(SqlParserImpl.java:3323)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2b(SqlParserImpl.java:3069)
 at 
org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2(SqlParserImpl.java:3095)
 at