twalthr commented on code in PR #29333: URL: https://github.com/apache/flink/pull/29333#discussion_r4145520219
########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/parse/CalciteParserTest.java: ########## @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.planner.parse; + +import org.apache.flink.sql.parser.impl.FlinkSqlParserImpl; +import org.apache.flink.sql.parser.validate.FlinkSqlConformance; +import org.apache.flink.table.api.SqlParserEOFException; +import org.apache.flink.table.api.SqlParserException; + +import org.apache.calcite.config.Lex; +import org.apache.calcite.sql.parser.SqlParser; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for the exception conversion in {@link CalciteParser}. */ +class CalciteParserTest { + + private static final String NON_QUERY = "Non-query expression encountered in illegal context"; + + private final CalciteParser parser = + new CalciteParser( + SqlParser.config() + .withParserFactory(FlinkSqlParserImpl.FACTORY) + .withConformance(FlinkSqlConformance.DEFAULT) + .withLex(Lex.JAVA) + .withIdentifierMaxLength(256)); + + /** + * Errors that the grammar raises through {@code SqlUtil.newContextException}. Calcite keeps + * their position only in {@code SqlParseException#getPos()}, so the message has to name it. + */ + private static Stream<Arguments> statementsWithContextErrors() { + return Stream.of( + Arguments.of( + "misspelled keyword", + "ELECT 1", + "From line 1, column 1 to line 1, column 5: " + NON_QUERY), + Arguments.of( + "non-query INSERT body", + "INSERT INTO t 1", + "At line 1, column 15: " + NON_QUERY), Review Comment: I looked into how different SQL engines format parse errors for invalid syntax (e.g., `ELECT 1,` and `SELECT a FROM b b b`). If we are looking to parse these errors or understand the context provided by each engine, **PostgreSQL** and **Oracle** are the only ones that provide a visual pointer to the exact character. Here is a quick summary of the behavior across major vendors: | Engine | Includes Query Snippet? | Visual Pointer? | Output Example | | :--- | :---: | :---: | :--- | | **PostgreSQL** | ✅ Yes (Exact line) | ✅ Yes (`^`) | `syntax error at or near "b"` | | **Oracle (CLI)** | ✅ Yes (Exact line) | ✅ Yes (`*`) | `ORA-00933: SQL command not properly ended` | | **MySQL / MariaDB**| ✅ Yes (Trailing snippet)| ❌ No | `syntax to use near 'b' at line 1` | | **SQL Server** | ❌ No | ❌ No | `Incorrect syntax near 'b'.` | | **SQLite** | ❌ No | ❌ No | `Error: near "b": syntax error` | --- ### Detailed Error Outputs <details> <summary>🐘 <b>PostgreSQL</b></summary> Returns the line number, prints the exact line of code, and points directly to the offending token with a caret (`^`). **`ELECT 1,`** ```text ERROR: syntax error at or near "ELECT" LINE 1: ELECT 1, ^ ``` **`SELECT a FROM b b b`** ```text ERROR: syntax error at or near "b" LINE 1: SELECT a FROM b b b ^ ``` </details> <details> <summary>🐬 <b>MySQL / MariaDB</b></summary> Prints the remainder of the query *starting from the exact point* where the parser failed. **`ELECT 1,`** ```text ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELECT 1,' at line 1 ``` **`SELECT a FROM b b b`** ```text ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'b' at line 1 ``` </details> <details> <summary>🔴 <b>Oracle Database (SQL*Plus / SQLcl)</b></summary> The CLI tools intercept the character offset from the engine, print the line of code, and place an asterisk (`*`) under the failure point. **`ELECT 1,`** ```text ELECT 1, * ERROR at line 1: ORA-00900: invalid SQL statement ``` **`SELECT a FROM b b b`** ```text SELECT a FROM b b b * ERROR at line 1: ORA-00933: SQL command not properly ended ``` </details> <details> <summary>🪟 <b>Microsoft SQL Server (T-SQL)</b></summary> Outputs a generic error code, line number, and the specific token, but does *not* print any portion of the query back. **`ELECT 1,`** ```text Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'ELECT'. ``` **`SELECT a FROM b b b`** ```text Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'b'. ``` </details> <details> <summary>🪶 <b>SQLite</b></summary> Provides minimal error messages identifying the token where the parser failed, with no line numbers or query snippets. **`ELECT 1,`** ```text Error: near "ELECT": syntax error ``` **`SELECT a FROM b b b`** ```text Error: near "b": syntax error ``` </details> ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/parse/CalciteParserTest.java: ########## @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.planner.parse; + +import org.apache.flink.sql.parser.impl.FlinkSqlParserImpl; +import org.apache.flink.sql.parser.validate.FlinkSqlConformance; +import org.apache.flink.table.api.SqlParserEOFException; +import org.apache.flink.table.api.SqlParserException; + +import org.apache.calcite.config.Lex; +import org.apache.calcite.sql.parser.SqlParser; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for the exception conversion in {@link CalciteParser}. */ +class CalciteParserTest { + + private static final String NON_QUERY = "Non-query expression encountered in illegal context"; + + private final CalciteParser parser = + new CalciteParser( + SqlParser.config() + .withParserFactory(FlinkSqlParserImpl.FACTORY) + .withConformance(FlinkSqlConformance.DEFAULT) + .withLex(Lex.JAVA) + .withIdentifierMaxLength(256)); + + /** + * Errors that the grammar raises through {@code SqlUtil.newContextException}. Calcite keeps + * their position only in {@code SqlParseException#getPos()}, so the message has to name it. + */ + private static Stream<Arguments> statementsWithContextErrors() { + return Stream.of( + Arguments.of( + "misspelled keyword", + "ELECT 1", + "From line 1, column 1 to line 1, column 5: " + NON_QUERY), + Arguments.of( + "non-query INSERT body", + "INSERT INTO t 1", + "At line 1, column 15: " + NON_QUERY), Review Comment: How about we further improve this message by including parts of the query in the error. Other vendors seem to do this too. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
