makssent opened a new issue, #40288:
URL: https://github.com/apache/shardingsphere/issues/40288

   ### 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 
[`01446d4fad4`](https://github.com/apache/shardingsphere/commit/01446d4fad48450e7fc3eb3b3631b25d6bf411df)
   
   ### ShardingSphere project
   
   ShardingSphere-Proxy
   
   ### Expected behavior
   
   The proxy should execute 
[`SELECT`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L5781-L5792),
 
[`UPDATE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6758)
 and 
[`DELETE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6714)
 with 
[`PLAN`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6370-L6373)
 as the Firebird server does.
   
   ### Actual behavior
   
   The proxy rejects each of these statements with a syntax error.
   
   The same statements were executed directly on the Firebird server and 
through the proxy, in one transaction with auto-commit off, after inserting a 
row with ID = 6701:
   
   | Steps | Firebird server | Proxy |
   | --- | --- | --- |
   | `SELECT ID FROM T_PLAIN WHERE ID = 6701 PLAN (T_PLAIN NATURAL)` | The row 
with ID = 6701 | 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: SELECT ID FROM 
T_PLAIN WHERE ID = 6701 PLAN (T_PLAIN NATURAL), null" |
   | `UPDATE T_PLAIN SET NAME = 'updated' WHERE ID = 6701 PLAN (T_PLAIN 
NATURAL)` | Success, 1 row updated | 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: UPDATE T_PLAIN SET 
NAME = 'updated' WHERE ID = 6701 PLAN (T_PLAIN NATURAL), null" |
   | `DELETE FROM T_PLAIN WHERE ID = -1 PLAN (T_PLAIN NATURAL)` | Success, no 
rows deleted | 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 '-1PLAN'" |
   
   So through the proxy an application cannot give a query or a data change its 
access plan.
   
   ### Reason analysis
   
   - The rule 
[`selectClause`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/DMLStatement.g4#L79-L81)
 of the Firebird grammar of ShardingSphere has no 
[`PLAN`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6370-L6373),
 and the grammar has no `PLAN` token, so the proxy rejects these statements 
with a syntax error.
   - The rules 
[`update`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/DMLStatement.g4#L38-L40)
 and 
[`delete`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/DMLStatement.g4#L63-L65)
 have no `PLAN` either. The Firebird server allows 
[`PLAN`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6370-L6373)
 in a [query 
specification](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L5998),
 in 
[`DELETE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6714),
 
[`UPDATE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6758),
 
[`MERGE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6631)
 and [`UPDATE OR 
INSERT`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/p
 arse.y#L6802).
   
   ### 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>?encoding=UTF8
       username: <user>
       password: <password>
   rules:
     - !SINGLE
       tables:
         - "*.*"
   ```
   
   3. Turn auto-commit off, execute `INSERT INTO T_PLAIN (ID, NAME) VALUES 
(6701, 'before')` and then the statements from the table under "Actual 
behavior" in the same transaction 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 SelectPlanClause {
   
       // The proxy; put the URL, user and password 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";
   
       // The Firebird server, to delete the rows of the example directly
       static final String SERVER_URL = "jdbc:firebirdsql://<host>/<database 
file>";
   
       static final String SERVER_USER = "<user>";
   
       static final String SERVER_PASSWORD = "<password>";
   
       public static void main(final String[] args) throws SQLException {
           executeOnServer("DELETE FROM T_PLAIN WHERE ID BETWEEN 6701 AND 
6709");
           try (Connection connection = DriverManager.getConnection(URL, USER, 
PASSWORD)) {
               connection.setAutoCommit(false);
               // Firebird server: "INSERT INTO T_PLAIN (ID, NAME) VALUES 
(6701, 'before') -> update count 1";
               // proxy: "INSERT INTO T_PLAIN (ID, NAME) VALUES (6701, 
'before') -> update count 1"
               execute(connection, "INSERT INTO T_PLAIN (ID, NAME) VALUES 
(6701, 'before')");
               // Firebird server: "SELECT ID FROM T_PLAIN WHERE ID = 6701 PLAN 
(T_PLAIN NATURAL) -> ID INTEGER: 6701";
               // proxy: "SELECT ID FROM T_PLAIN WHERE ID = 6701 PLAN (T_PLAIN 
NATURAL) -> error: Dynamic SQL Error; You have an error in your SQL syntax:
               // SELECT ID FROM T_PLAIN WHERE ID = 6701 PLAN (T_PLAIN 
NATURAL), null [SQLState:42000, ISC error code:335544569]"
               execute(connection, "SELECT ID FROM T_PLAIN WHERE ID = 6701 PLAN 
(T_PLAIN NATURAL)");
               // Firebird server: "UPDATE T_PLAIN SET NAME = 'updated' WHERE 
ID = 6701 PLAN (T_PLAIN NATURAL) -> update count 1";
               // proxy: "UPDATE T_PLAIN SET NAME = 'updated' WHERE ID = 6701 
PLAN (T_PLAIN NATURAL) -> error: Dynamic SQL Error; You have an error in your 
SQL syntax:
               // UPDATE T_PLAIN SET NAME = 'updated' WHERE ID = 6701 PLAN 
(T_PLAIN NATURAL), null [SQLState:42000, ISC error code:335544569]"
               execute(connection, "UPDATE T_PLAIN SET NAME = 'updated' WHERE 
ID = 6701 PLAN (T_PLAIN NATURAL)");
               // Firebird server: "DELETE FROM T_PLAIN WHERE ID = -1 PLAN 
(T_PLAIN NATURAL) -> update count 0";
               // proxy: "DELETE FROM T_PLAIN WHERE ID = -1 PLAN (T_PLAIN 
NATURAL) -> error: Dynamic SQL Error; You have an error in your SQL syntax: no 
viable alternative at input '-1PLAN' [SQLState:42000, ISC error code:335544569]"
               execute(connection, "DELETE FROM T_PLAIN WHERE ID = -1 PLAN 
(T_PLAIN NATURAL)");
               connection.commit();
           }
           executeOnServer("DELETE FROM T_PLAIN WHERE ID BETWEEN 6701 AND 
6709");
       }
   
       // 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());
           }
       }
   
       static void executeOnServer(final String sql) throws SQLException {
           try (Connection connection = DriverManager.getConnection(SERVER_URL, 
SERVER_USER, SERVER_PASSWORD)) {
               connection.createStatement().execute(sql);
           }
       }
   }
   ```


-- 
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]

Reply via email to