You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "makssent (via GitHub)" <gi...@apache.org> on 2026/10/26 13:52:22 UTC
[I] Firebird Proxy cannot prepare statements with a parameter in LIKE [shardingsphere]
makssent opened a new issue, #40371:
URL: https://github.com/apache/shardingsphere/issues/40371
### 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 [`f0b3e2fe124`](https://github.com/apache/shardingsphere/commit/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8)
### ShardingSphere project
ShardingSphere-Proxy
### Expected behavior
The proxy should prepare and execute statements with a parameter marker in [`[NOT] LIKE`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L7119-L7133) (as the pattern, as the `ESCAPE` character or as the left operand) as the Firebird server does, and describe the parameters in the order of the markers with the types the Firebird server gives them ([`ComparativeBoolNode::dsqlPass`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/BoolNodes.cpp#L465-L478), [`ParameterNode::setParameterType`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.cpp#L9583-L9700)):
- a parameter takes the type of the other operand as `VARCHAR`: `NAME LIKE ?`, `? LIKE NAME` and both parameters of `NAME LIKE ? ESCAPE ?` are `VARCHAR(20)` for the column `NAME VARCHAR(20)`; the `ESCAPE` parameter takes the type of the pattern;
- a non-text operand gives `VARCHAR(30)` ([`LIKE_PARAM_LEN`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.h#L1594)), for example `ID LIKE ?` for the `INTEGER` column `ID`;
- a string literal gives its length after the doubled quotes are decoded ([`Parser.cpp`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/Parser.cpp#L472-L474)): `? LIKE 'a''b%'` is `VARCHAR(4)`;
- the length in bytes and the character set of the parameter follow the connection character set, so with `UTF8` the parameter of `? LIKE 'é'` accepts the value `é`;
- `NULL` gives `VARCHAR(1)` with the character set `NONE` ([`NullNode::make`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.cpp#L9043-L9059)): `? LIKE NULL`;
- a parameter inside a comparison that is an operand of `LIKE` keeps the type of the comparison ([`BoolAsValueNode`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/parse.y#L7517-L7521)): `(ID = ?) LIKE 'TRUE'` is `INTEGER`. The proxy already describes this case as the Firebird server does, and it should stay so.
### Actual behavior
The proxy rejects every statement with a parameter in `LIKE` at prepare, and `UPDATE` and `DELETE` with such a condition change no rows.
The table `T_PLAIN` has the rows with ID = 4301 (NAME 'alpha'), ID = 4302 (NAME 'beta_1') and ID = 4303 (NAME 'a%'). The same statements were prepared and executed directly on the Firebird server and through the proxy, with a `UTF8` connection:
| Steps | Firebird server | Proxy |
| --- | --- | --- |
| `SELECT ID FROM T_PLAIN WHERE NAME LIKE ? ORDER BY ID` with 'al%' | The statement has one VARCHAR(20) parameter, the row with ID = 4301 is returned | Error [`isc_random`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L63) "Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null" at prepare |
| `SELECT ID FROM T_PLAIN WHERE NAME LIKE ? ESCAPE ? ORDER BY ID` with 'beta#_%' and '#' | The statement has two VARCHAR(20) parameters, the row with ID = 4302 is returned | The same error at prepare |
| `SELECT ID FROM T_PLAIN WHERE ? LIKE NAME ORDER BY ID` with 'a%' | The statement has one VARCHAR(20) parameter, the row with ID = 4303 is returned | The same error at prepare |
| `SELECT ID FROM T_PLAIN WHERE ID LIKE ? ORDER BY ID` with '430%' | The statement has one VARCHAR(30) parameter, the rows with ID = 4301, 4302 and 4303 are returned | The same error at prepare |
| `SELECT ID FROM T_PLAIN WHERE ? LIKE 'a''b%' ORDER BY ID` with `a'bc` | The statement has one VARCHAR(4) parameter, the rows with ID = 4301, 4302 and 4303 are returned | The same error at prepare |
| `SELECT ID FROM T_PLAIN WHERE ? LIKE 'é' ORDER BY ID` with 'é' | The statement has one VARCHAR(1) parameter that accepts 'é', the rows with ID = 4301, 4302 and 4303 are returned | The same error at prepare |
| `SELECT ID FROM T_PLAIN WHERE ? LIKE NULL ORDER BY ID` with 'a' | The statement has one VARCHAR(1) parameter, no rows are returned | The same error at prepare |
| `UPDATE T_PLAIN SET NAME = NAME \|\| '!' WHERE NAME LIKE ?` with 'be%' | The statement has one VARCHAR(20) parameter, the row with ID = 4302 is updated | The same error at prepare |
| `DELETE FROM T_PLAIN WHERE NAME LIKE ?` with 'al%' | The statement has one VARCHAR(20) parameter, the row with ID = 4301 is deleted | The same error at prepare |
| Read the rows of `T_PLAIN` directly on the Firebird server | The rows with ID = 4302 (NAME 'beta_1!') and ID = 4303 (NAME 'a%') | The rows with ID = 4301 (NAME 'alpha'), ID = 4302 (NAME 'beta_1') and ID = 4303 (NAME 'a%') |
So through the proxy an application cannot filter, update or delete rows with a `LIKE` pattern passed as a parameter: the statement fails at prepare. The connection stays usable after the error.
### Reason analysis
- [`FirebirdStatementVisitor#createBinaryOperationExpressionFromLike`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/parser/sql/engine/dialect/firebird/src/main/java/org/apache/shardingsphere/sql/parser/engine/firebird/visitor/statement/FirebirdStatementVisitor.java#L341-L349) builds `LIKE` as a `BinaryOperationExpression` whose right operand is a `ListExpression` of the pattern and the `ESCAPE` expression. [`FirebirdPrepareStatementCommandExecutor#processExpr`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prepare/FirebirdPrepareStatementCommandExecutor.java#L410-L421) pairs a parameter with a column only when the right operand of a binary operation is the parameter itself and the left operand is a column. So neither a parameter in the pattern or in `ESCAPE` nor a parameter on the
left of `LIKE` gets a type.
- [`FirebirdPrepareStatementCommandExecutor#processParameters`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prepare/FirebirdPrepareStatementCommandExecutor.java#L317-L335) describes every parameter without a column as `VARCHAR` of a column without a table ([`processCustomColumn`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prepare/FirebirdPrepareStatementCommandExecutor.java#L490-L495)), and [`resolveColumnLength`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prepare/FirebirdPrepareStatementCommandExec
utor.java#L535-L541) finds no length for it and returns `null`.
- [`FirebirdReturnColumnPacket#write`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdReturnColumnPacket.java#L75-L80) writes the length of a `VARCHAR` parameter from this `null` and throws `NullPointerException`. [`FirebirdPacketCodecEngine#encode`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/database/protocol/dialect/firebird/src/main/java/org/apache/shardingsphere/database/protocol/firebird/codec/FirebirdPacketCodecEngine.java#L191-L204) replaces the prepare answer with the error [`isc_random`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/include/firebird/impl/msg/jrd.h#L63) "Unknown exception.", and the client does not execute the statement.
- Pairing the `LIKE` parameters with columns is not enough to describe them as the Firebird server does ([`ComparativeBoolNode::dsqlPass`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/BoolNodes.cpp#L465-L478), [`ParameterNode::setParameterType`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.cpp#L9583-L9700)): [`processParameters`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prepare/FirebirdPrepareStatementCommandExecutor.java#L317-L335) describes the parameters paired with columns first and the other parameters after them, not in the order of the markers; [`processColumn`](https://github.com/apache/shardingsphere/blob/f0b3e2fe1249b9a24d0319fe26677b85a496a8e8/proxy/frontend/dialect/firebird/src/main/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/statement/prep
are/FirebirdPrepareStatementCommandExecutor.java#L497-L506) gives a paired parameter the column type as is, while for `LIKE` the Firebird server forces `VARCHAR` (`VARCHAR(30)` for `ID LIKE ?`); and the rules for a string literal, `NULL` and the connection character set from "Expected behavior" have no counterpart. The `LIKE` rule must not reach into a comparison inside an operand, such as `(ID = ?) LIKE 'TRUE'`: the Firebird server passes the type of `LIKE` into an operand only through `||` ([`ExprNodes.cpp`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.cpp#L3999-L4004)) and `+ - * /` ([`ExprNodes.cpp`](https://github.com/FirebirdSQL/firebird/blob/v5.0.4/src/dsql/ExprNodes.cpp#L626-L631)).
### 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. Execute the steps 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
All cases on Jaybird 6.0.7 with a `UTF8` connection, one method per case. The rows are prepared and read directly on the Firebird server. The comment above each method shows what it prints against the Firebird server and through the proxy.
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
public class LikeParameter {
// 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 prepare the rows and to read them directly
static final String SERVER_URL = "jdbc:firebirdsql://<host>/<database file>?encoding=UTF8";
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");
executeOnServer("INSERT INTO T_PLAIN (ID, NAME) VALUES (4301, 'alpha')");
executeOnServer("INSERT INTO T_PLAIN (ID, NAME) VALUES (4302, 'beta_1')");
executeOnServer("INSERT INTO T_PLAIN (ID, NAME) VALUES (4303, 'a%')");
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
columnLikeParameter(connection);
columnLikeParameterEscapeParameter(connection);
parameterLikeColumn(connection);
integerColumnLikeParameter(connection);
parameterLikeQuotedLiteral(connection);
parameterLikeNonAsciiLiteral(connection);
parameterLikeNull(connection);
updateWhereColumnLikeParameter(connection);
deleteWhereColumnLikeParameter(connection);
}
printRows();
executeOnServer("DELETE FROM T_PLAIN");
}
// Firebird server: "NAME LIKE ?: parameters VARCHAR(20), rows 4301";
// proxy: "NAME LIKE ?: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void columnLikeParameter(final Connection connection) {
query(connection, "NAME LIKE ?", "SELECT ID FROM T_PLAIN WHERE NAME LIKE ? ORDER BY ID", "al%");
}
// Firebird server: "NAME LIKE ? ESCAPE ?: parameters VARCHAR(20), VARCHAR(20), rows 4302";
// proxy: "NAME LIKE ? ESCAPE ?: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void columnLikeParameterEscapeParameter(final Connection connection) {
query(connection, "NAME LIKE ? ESCAPE ?", "SELECT ID FROM T_PLAIN WHERE NAME LIKE ? ESCAPE ? ORDER BY ID", "beta#_%", "#");
}
// Firebird server: "? LIKE NAME: parameters VARCHAR(20), rows 4303";
// proxy: "? LIKE NAME: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void parameterLikeColumn(final Connection connection) {
query(connection, "? LIKE NAME", "SELECT ID FROM T_PLAIN WHERE ? LIKE NAME ORDER BY ID", "a%");
}
// Firebird server: "ID LIKE ?: parameters VARCHAR(30), rows 4301, 4302, 4303";
// proxy: "ID LIKE ?: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void integerColumnLikeParameter(final Connection connection) {
query(connection, "ID LIKE ?", "SELECT ID FROM T_PLAIN WHERE ID LIKE ? ORDER BY ID", "430%");
}
// Firebird server: "? LIKE 'a''b%': parameters VARCHAR(4), rows 4301, 4302, 4303";
// proxy: "? LIKE 'a''b%': Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void parameterLikeQuotedLiteral(final Connection connection) {
query(connection, "? LIKE 'a''b%'", "SELECT ID FROM T_PLAIN WHERE ? LIKE 'a''b%' ORDER BY ID", "a'bc");
}
// Firebird server: "? LIKE 'é': parameters VARCHAR(1), rows 4301, 4302, 4303";
// proxy: "? LIKE 'é': Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void parameterLikeNonAsciiLiteral(final Connection connection) {
query(connection, "? LIKE 'é'", "SELECT ID FROM T_PLAIN WHERE ? LIKE 'é' ORDER BY ID", "é");
}
// Firebird server: "? LIKE NULL: parameters VARCHAR(1), rows ";
// proxy: "? LIKE NULL: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void parameterLikeNull(final Connection connection) {
query(connection, "? LIKE NULL", "SELECT ID FROM T_PLAIN WHERE ? LIKE NULL ORDER BY ID", "a");
}
// Firebird server: "UPDATE ... NAME LIKE ?: parameters VARCHAR(20), 1 row updated";
// proxy: "UPDATE ... NAME LIKE ?: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void updateWhereColumnLikeParameter(final Connection connection) {
update(connection, "UPDATE ... NAME LIKE ?", "UPDATE T_PLAIN SET NAME = NAME || '!' WHERE NAME LIKE ?", "be%");
}
// Firebird server: "DELETE ... NAME LIKE ?: parameters VARCHAR(20), 1 row deleted";
// proxy: "DELETE ... NAME LIKE ?: Unknown exception. More details: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.columnLength" is null [SQLState:HY000, ISC error code:335544382]"
static void deleteWhereColumnLikeParameter(final Connection connection) {
update(connection, "DELETE ... NAME LIKE ?", "DELETE FROM T_PLAIN WHERE NAME LIKE ?", "al%");
}
static void query(final Connection connection, final String label, final String sql, final Object... values) {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
String parameters = describeParameters(statement);
bind(statement, values);
List<String> ids = new ArrayList<>();
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
ids.add(resultSet.getString(1));
}
}
System.out.println(label + ": parameters " + parameters + ", rows " + String.join(", ", ids));
} catch (final SQLException ex) {
System.out.println(label + ": " + ex.getMessage().replace('\n', ' '));
}
}
static void update(final Connection connection, final String label, final String sql, final Object... values) {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
String parameters = describeParameters(statement);
bind(statement, values);
int count = statement.executeUpdate();
System.out.println(label + ": parameters " + parameters + ", " + count + (1 == count ? " row " : " rows ") + (sql.startsWith("UPDATE") ? "updated" : "deleted"));
} catch (final SQLException ex) {
System.out.println(label + ": " + ex.getMessage().replace('\n', ' '));
}
}
static String describeParameters(final PreparedStatement statement) throws SQLException {
ParameterMetaData metaData = statement.getParameterMetaData();
List<String> result = new ArrayList<>();
for (int i = 1; i <= metaData.getParameterCount(); i++) {
int type = metaData.getParameterType(i);
boolean text = Types.VARCHAR == type || Types.CHAR == type;
result.add(metaData.getParameterTypeName(i) + (text ? "(" + metaData.getPrecision(i) + ")" : ""));
}
return String.join(", ", result);
}
static void bind(final PreparedStatement statement, final Object... values) throws SQLException {
for (int i = 0; i < values.length; i++) {
statement.setObject(i + 1, values[i]);
}
}
// Reads the rows directly on the Firebird server.
// Firebird server: "rows: 4302 beta_1!; 4303 a%";
// proxy: "rows: 4301 alpha; 4302 beta_1; 4303 a%"
static void printRows() throws SQLException {
List<String> rows = new ArrayList<>();
try (
Connection connection = DriverManager.getConnection(SERVER_URL, SERVER_USER, SERVER_PASSWORD);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT ID, NAME FROM T_PLAIN ORDER BY ID")) {
while (resultSet.next()) {
rows.add(resultSet.getInt(1) + " " + resultSet.getString(2));
}
}
System.out.println("rows: " + String.join("; ", rows));
}
static void executeOnServer(final String sql) throws SQLException {
try (
Connection connection = DriverManager.getConnection(SERVER_URL, SERVER_USER, SERVER_PASSWORD);
Statement statement = connection.createStatement()) {
statement.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: notifications-unsubscribe@shardingsphere.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org