apache / apache/shardingsphere
opengauss + pyscopg: pbe message error
- Dominant language
- Java
- Stars
- 20.8k
- Forks
- 6.9k
- Avg merge
- 11h 35m
- Merged PRs (30d)
- 326
Description
opengauss version: 7.0
python version: 3.7
SQL
```sql
CREATE TABLE t4(id BIGINT PRIMARY KEY, v1 int, v2 int, v3 varchar(255));
insert into t4 values(1, 2, 3, 'abc');
insert into t4 values(2, 2, 3, 'abc');
insert into t4 values(3, 2, 3, 'abc');
```
And when I tried to select data from t4, using this:
```python
result = cursor.execute(
sql.Composed(
[ sql.SQL("select v1 from t4 order by %s::int limit %s")]),
(2, 10),
prepare=False,
binary=False)
```
I got this:
```
psycopg.errors.SystemError: Unknown exception.
More details: java.lang.ClassCastException: class java.lang.Short cannot be cast to class java.lang.Integer (java.lang.Short and java.lang.Integer are in module java.base of loader 'bootstrap')
```
It seems it interpret the parameter whose initial type is `int2` as `short`, and then put it as Short object.
I modified this part:
```
--- a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLInt2BinaryProtocolValue.java
+++ b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLInt2BinaryProtocolValue.java
@@ -31,7 +31,7 @@ public final class PostgreSQLInt2BinaryProtocolValue implements PostgreSQLBinary
@Override
public Object read(final PostgreSQLPacketPayload payload, final int parameterValueLength) {
- return payload.getByteBuf().readShort();
+ return payload.readInt2();
}
```
For the reason that ByteBuf does not have `readInt2()`, I think `payload.readInt2()` and `payload.gerByteBuf().readInt2()` are equivalent but I am not sure about it. I do not know if this is a valid modification and whether this is the root cause of this error
Contributor guide
Research direction
Start with db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLInt2BinaryProtocolValue.java and inspect its read method alongside the payload API. Reproduce the supplied psycopg query, then verify that the int2 parameter is represented with the type expected by the query without the ClassCastException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, postgresql, python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100