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

   ### 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 these statements as the Firebird server does. For 
the Firebird server 
[`KEY`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L9076)
 is a [non-reserved 
word](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L8940-L8941),
 
[`TEXT`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L8933-L8936)
 is not a keyword, and 
[`COMMENT`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L1212-L1213)
 is a keyword only as the first word of a statement, so each of them can be a 
[column 
alias](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L6067-L6069).
   
   ### 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:
   
   | Statement | Firebird server | Proxy |
   | --- | --- | --- |
   | `SELECT ID AS KEY FROM T_PLAIN WHERE ID = -1` | Success, column KEY of 
type INTEGER, no rows | 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 'KEY'" |
   | `SELECT NAME AS TEXT FROM T_PLAIN WHERE ID = -1` | Success, column TEXT of 
type VARCHAR, no rows | 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 'TEXT'" |
   | `SELECT ID AS COMMENT FROM T_PLAIN WHERE ID = -1` | Success, column 
COMMENT of type INTEGER, no rows | 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 'COMMENT'" |
   
   So through the proxy an application cannot use these words as column 
aliases, although the Firebird server accepts them.
   
   ### Reason analysis
   
   - In the Firebird grammar of ShardingSphere 
[`KEY`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/Keyword.g4#L106-L108),
 
[`COMMENT`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/FirebirdKeyword.g4#L978-L980)
 and 
[`TEXT`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/FirebirdKeyword.g4#L1014-L1016)
 are keyword tokens.
   - The rule 
[`alias`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/DMLStatement.g4#L112-L114)
 accepts only an 
[`identifier`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/BaseRule.g4#L65-L67)
 or a string, and the keyword tokens allowed in an identifier, 
[`unreservedWord`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/BaseRule.g4#L69-L86),
 do not include 
[`KEY`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/Keyword.g4#L106-L108),
 
[`TEXT`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/fire
 bird/src/main/antlr4/imports/firebird/FirebirdKeyword.g4#L1014-L1016) and 
[`COMMENT`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/FirebirdKeyword.g4#L978-L980).
   - The open pull request #40178 and the pull request #40179, merged after 
this commit, add other words to 
[`unreservedWord`](https://github.com/apache/shardingsphere/blob/01446d4fad48450e7fc3eb3b3631b25d6bf411df/parser/sql/engine/dialect/firebird/src/main/antlr4/imports/firebird/BaseRule.g4#L69-L86),
 but not these three.
   
   ### 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 KeywordAliases {
   
       // 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 ID AS KEY FROM T_PLAIN WHERE ID = -1 
-> KEY INTEGER: no rows";
               // proxy: "SELECT ID AS KEY FROM T_PLAIN WHERE ID = -1 -> error: 
Dynamic SQL Error; You have an error in your SQL syntax: no viable alternative 
at input 'KEY' ..."
               execute(connection, "SELECT ID AS KEY FROM T_PLAIN WHERE ID = 
-1");
               // Firebird server: "SELECT NAME AS TEXT FROM T_PLAIN WHERE ID = 
-1 -> TEXT VARCHAR(20): no rows";
               // proxy: "SELECT NAME AS TEXT FROM T_PLAIN WHERE ID = -1 -> 
error: Dynamic SQL Error; You have an error in your SQL syntax: no viable 
alternative at input 'TEXT' ..."
               execute(connection, "SELECT NAME AS TEXT FROM T_PLAIN WHERE ID = 
-1");
               // Firebird server: "SELECT ID AS COMMENT FROM T_PLAIN WHERE ID 
= -1 -> COMMENT INTEGER: no rows";
               // proxy: "SELECT ID AS COMMENT FROM T_PLAIN WHERE ID = -1 -> 
error: Dynamic SQL Error; You have an error in your SQL syntax: no viable 
alternative at input 'COMMENT' ..."
               execute(connection, "SELECT ID AS COMMENT FROM T_PLAIN WHERE ID 
= -1");
           }
       }
   
       // 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]

Reply via email to