makssent opened a new issue, #40241: URL: https://github.com/apache/shardingsphere/issues/40241
### Before reporting a bug - [x] I have searched open and closed [GitHub issues](https://github.com/apache/shardingsphere/issues). - [x] I have read the [ShardingSphere documentation](https://shardingsphere.apache.org/document/current/en/overview). ### ShardingSphere version master, commit [`8a10a2f4822`](https://github.com/apache/shardingsphere/commit/8a10a2f4822841b45561796ad02c4709ae69e2e2) ### ShardingSphere project ShardingSphere-Proxy ### Expected behavior The proxy should report a syntax error as the Firebird server does, with SQL error code -104 ([`isc_sqlerr`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1260-L1275)) and the position of the error. ### Actual behavior The proxy answers both statements with an error without SQL error code -104 and without the position. The same statements were executed directly on the Firebird server and through the proxy: | Statement | Firebird server | Proxy | | --- | --- | --- | | `SELECT FROM T_PLAIN` | Error [`isc_dsql_token_unk_err`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1269-L1274) "Dynamic SQL Error; SQL error code = -104; Token unknown - line 1, column 8; FROM" | Error [`isc_dsql_error`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L250) "Dynamic SQL Error; You have an error in your SQL syntax: no viable alternative at input 'FROM'" | | `SELECT ID FROM T_PLAIN WHERE` | Error [`isc_command_end_err2`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1262-L1265) "Dynamic SQL Error; SQL error code = -104; Unexpected end of command - line 1, column 24" | Error [`isc_dsql_error`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L250) "Dynamic SQL Error; You have an error in your SQL syntax: no viable alternative at input '<EOF>'" | So through the proxy an application gets other error codes for a syntax error than from the Firebird server, `isc_dsql_error` instead of [`isc_dsql_token_unk_err`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1269-L1274) and [`isc_command_end_err2`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1262-L1265), and cannot show the user where the error is. ### Reason analysis [`ShardingSphereSQLParserEngine#getException`](https://github.com/apache/shardingsphere/blob/8a10a2f4822841b45561796ad02c4709ae69e2e2/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java#L64-L68) turns a syntax error of the parser into [`DialectSQLParsingException`](https://github.com/apache/shardingsphere/blob/8a10a2f4822841b45561796ad02c4709ae69e2e2/database/exception/core/src/main/java/org/apache/shardingsphere/database/exception/core/exception/syntax/sql/DialectSQLParsingException.java#L29), and [`FirebirdDialectExceptionMapper#convert`](https://github.com/apache/shardingsphere/blob/8a10a2f4822841b45561796ad02c4709ae69e2e2/database/exception/dialect/firebird/src/main/java/org/apache/shardingsphere/database/exception/firebird/mapper/FirebirdDialectExceptionMapper.java#L95-L97) maps it to [`isc_dsql_error`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L250) with the text of the exception, without [`isc_sqlerr`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L117) -104 and without the line and column. ### Steps to reproduce #### Environment - Firebird server 5.0. - Client: Jaybird 6.0.7. #### Steps 1. Create the table on the Firebird server: ```sql CREATE TABLE T_PLAIN (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(20)); ``` 2. Start ShardingSphere-Proxy with this configuration: ```yaml # global.yaml authority: users: - user: root@% password: root privilege: type: ALL_PERMITTED props: proxy-frontend-database-protocol-type: Firebird ``` ```yaml # database-firebird.yaml databaseName: firebird_db dataSources: ds_0: url: jdbc:firebirdsql://<host>/<database file> username: <user> password: <password> rules: - !SINGLE tables: - "*.*" ``` 3. Execute the statements from the table under "Actual behavior" through the proxy and directly on the Firebird server, and compare the results. "Example code" runs all of them. ### Example code The statements from the table on Jaybird 6.0.7. The comment above each call shows what it prints against the Firebird server and through the proxy. ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.List; public class SyntaxErrorCode { // The proxy; put the URL of the Firebird server here to compare static final String URL = "jdbc:firebirdsql://<proxy host>:<proxy port>/firebird_db?encoding=UTF8&wireCrypt=DISABLED"; static final String USER = "root"; static final String PASSWORD = "root"; public static void main(final String[] args) throws SQLException { try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) { // Firebird server: "SELECT FROM T_PLAIN -> error: Dynamic SQL Error; SQL error code = -104; Token unknown - line 1, column 8; FROM ..."; // proxy: "SELECT FROM T_PLAIN -> error: Dynamic SQL Error; You have an error in your SQL syntax: no viable alternative at input 'FROM' ..." execute(connection, "SELECT FROM T_PLAIN"); // Firebird server: "SELECT ID FROM T_PLAIN WHERE -> error: Dynamic SQL Error; SQL error code = -104; Unexpected end of command - line 1, column 24 ..."; // proxy: "SELECT ID FROM T_PLAIN WHERE -> error: Dynamic SQL Error; You have an error in your SQL syntax: no viable alternative at input '<EOF>' ..." execute(connection, "SELECT ID FROM T_PLAIN WHERE"); } } // Executes the statement and prints the described columns with the rows, the update count or the error static void execute(final Connection connection, final String sql) { try (Statement statement = connection.createStatement()) { if (!statement.execute(sql)) { System.out.println(sql + " -> update count " + statement.getUpdateCount()); return; } try (ResultSet resultSet = statement.getResultSet()) { ResultSetMetaData metaData = resultSet.getMetaData(); List<String> columns = new ArrayList<>(); for (int i = 1; i <= metaData.getColumnCount(); i++) { int type = metaData.getColumnType(i); boolean text = Types.CHAR == type || Types.VARCHAR == type; columns.add(metaData.getColumnLabel(i) + " " + metaData.getColumnTypeName(i) + (text ? "(" + metaData.getPrecision(i) + ")" : "")); } List<String> rows = new ArrayList<>(); while (resultSet.next()) { List<String> values = new ArrayList<>(); for (int i = 1; i <= metaData.getColumnCount(); i++) { values.add(String.valueOf(resultSet.getObject(i))); } rows.add(String.join(", ", values)); } System.out.println(sql + " -> " + String.join(", ", columns) + ": " + (rows.isEmpty() ? "no rows" : String.join("; ", rows))); } } catch (final SQLException ex) { System.out.println(sql + " -> error: " + ex.getMessage()); } } } ``` -- 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]
