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

Yunpeng Niu commented on CALCITE-3463:
--------------------------------------

Hi [~danny0405],

Yes, I think it is a good idea for us to go back to the standard. As specified 
in my original code, I have set the `Lex` to `MYSQL` and `Conformance` to 
`MYSQL_5`. Thus, let's discuss under the context of MySQL 5.7, which is the 
latest stable version under MySQL 5 series.

Here, we refer to the JOIN syntax specified at 
[https://dev.mysql.com/doc/refman/5.7/en/join.html] If you read this page, you 
will notice
{code:sql}
table_reference:
    table_factor
  | joined_table

table_factor:
    tbl_name [PARTITION (partition_names)]
        [[AS] alias] [index_hint_list]
  | table_subquery [AS] alias
  | ( table_references )
{code}
Thus, I think this syntax (i.e., use bracket to specify the join precedence) is 
valid for MySQL 5.

 

Hi [~yanlin-Lynn],

Thanks for your checking on MySQL 8.0. I have tried to check on MySQL 5.7 as 
well, it also really works, with no parsing error.
{noformat}
mysql> SELECT * FROM departments d INNER JOIN
    -> (dept_manager m INNER JOIN employees e ON m.emp_no = e.emp_no)
    -> ON d.dept_no = m.dept_no;
+---------+--------------------+--------+---------+------------+------------+--------+------------+-------------+--------------+--------+------------+
| dept_no | dept_name          | emp_no | dept_no | from_date  | to_date    | 
emp_no | birth_date | first_name  | last_name    | gender | hire_date  |
+---------+--------------------+--------+---------+------------+------------+--------+------------+-------------+--------------+--------+------------+
| d001    | Marketing          | 110022 | d001    | 1985-01-01 | 1991-10-01 | 
110022 | 1956-09-12 | Margareta   | Markovitch   | M      | 1985-01-01 |
| d001    | Marketing          | 110039 | d001    | 1991-10-01 | 9999-01-01 | 
110039 | 1963-06-21 | Vishwani    | Minakawa     | M      | 1986-04-12 |
| d002    | Finance            | 110085 | d002    | 1985-01-01 | 1989-12-17 | 
11{noformat}
 

Let's continue the discussion and see how to solve this issue.

 

Regards.

> Parse SQL Query - Non-query expression encountered in illegal context
> ---------------------------------------------------------------------
>
>                 Key: CALCITE-3463
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3463
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.21.0
>         Environment: OS: macOS Catalina 10.15
> Java: java 12.0.2 2019-07-16
>            Reporter: Yunpeng Niu
>            Priority: Major
>         Attachments: image-2019-10-31-21-11-41-225.png
>
>
> I tried to ask calcite-core to parse the following SQL query:
>  
> {code:java}
> final SqlParser.Config parserConfig = SqlParser.configBuilder()
>     .setConformance(SqlConformanceEnum.MYSQL_5)
>     .setLex(Lex.MYSQL)
>     .build();
> String query = "SELECT * FROM departments d INNER JOIN (dept_manager m INNER 
> JOIN employees e ON m.emp_no = e.emp_no) ON d.dept_no = m.dept_no";
> SqlParser.create(query, parserConfig).parseQuery();
> {code}
>  
>  
> But there is the following exception:
> {noformat}
> Exception in thread "main" org.apache.calcite.sql.parser.SqlParseException: 
> Non-query expression encountered in illegal contextException in thread "main" 
> org.apache.calcite.sql.parser.SqlParseException: Non-query expression 
> encountered in illegal context at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.convertException(SqlParserImpl.java:362)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.normalizeException(SqlParserImpl.java:147)
>  at 
> org.apache.calcite.sql.parser.SqlParser.handleException(SqlParser.java:148) 
> at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:163) at 
> org.apache.calcite.Runner.main(Runner.java:60)Caused by: 
> org.apache.calcite.runtime.CalciteException: Non-query expression encountered 
> in illegal context at 
> java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native
>  Method) at 
> java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>  at 
> java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>  at 
> java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
>  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 
> 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:835) at 
> org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:820) at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.checkNonQueryExpression(SqlParserImpl.java:306)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression3(SqlParserImpl.java:13780)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2b(SqlParserImpl.java:13449)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression2(SqlParserImpl.java:13490)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.Expression(SqlParserImpl.java:13421)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.LeafQueryOrExpr(SqlParserImpl.java:13398)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.QueryOrExpr(SqlParserImpl.java:12874)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.OrderedQueryOrExpr(SqlParserImpl.java:478)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.ParenthesizedExpression(SqlParserImpl.java:639)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.TableRef2(SqlParserImpl.java:8145)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.TableRef(SqlParserImpl.java:8068)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.FromClause(SqlParserImpl.java:7969)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.SqlSelect(SqlParserImpl.java:3722)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.LeafQuery(SqlParserImpl.java:604)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.LeafQueryOrExpr(SqlParserImpl.java:13404)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.QueryOrExpr(SqlParserImpl.java:12874)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.OrderedQueryOrExpr(SqlParserImpl.java:478)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.SqlStmt(SqlParserImpl.java:3626)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.SqlStmtEof(SqlParserImpl.java:3664)
>  at 
> org.apache.calcite.sql.parser.impl.SqlParserImpl.parseSqlStmtEof(SqlParserImpl.java:194)
>  at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:161) 
> ... 1 more
> Process finished with exit code 1
> {noformat}
>  
> I have tried to run the above query in MySQL database and I am sure it is 
> valid syntax. Can anyone help to explain why the above exception is thrown? 
> Thanks. And how should I fix this?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to