You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "jzs1997 (via GitHub)" <gi...@apache.org> on 2025/03/13 07:59:58 UTC

[I] opengauss + pyscopg: pbe message error [shardingsphere]

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

   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
   
   
   


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


Re: [I] opengauss + pyscopg: pbe message error [shardingsphere]

Posted by "linghengqian (via GitHub)" <gi...@apache.org>.
linghengqian commented on issue #34972:
URL: https://github.com/apache/shardingsphere/issues/34972#issuecomment-2720320837

   - But isn't your change part of an old bug? I mean the merged https://github.com/apache/shardingsphere/pull/22338 .
   - CC PR Original author @TeslaCN .


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] opengauss + pyscopg: pbe message error [shardingsphere]

Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu commented on issue #34972:
URL: https://github.com/apache/shardingsphere/issues/34972#issuecomment-3642247010

   ##  Understanding
     In openGauss 7.0 + psycopg, running select … order by %s::int limit %s via Proxy (extended query, prepare=False/binary=False) triggers java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer. You’re checking whether Proxy mishandles int2
     parameters.
   
   ##  Root Cause
     For LIMIT/OFFSET params, PaginationContext#getValue assumes Integer/Long; when the client marks the param as int2 (OID 21), PostgreSQLInt2BinaryProtocolValue returns a Short, and the subsequent (int) obj cast throws CCE. The client usage is valid per PostgreSQL/
     OpenGauss protocol; the incompatibility is on Proxy’s side for small integer params.
   
   ##  Analysis
   
     - Protocol: PostgreSQL int2 binary encoding is a signed 2-byte integer (official doc: https://www.postgresql.org/docs/current/protocol-message-types.html). Switching to payload.readInt2() would read as unsigned, breaking negative smallints, so that isn’t a correct fix.
     - Current code: PaginationContext#getValue only handles Long/Integer and otherwise does (int) obj, so Short and other Number subtypes fail.
     - Trigger: Any binary-bound param declared as int2 (or other non-int4 numeric) for LIMIT/ORDER BY becomes Short and hits the cast.
     - Workaround: Explicitly cast the placeholder to ::int4 or send params as text/INT4 to avoid the CCE temporarily; the server-side compatibility still needs a fix.
   
   ##  Conclusion
     This is a Proxy compatibility gap for small integer pagination params, not a misuse. A safer server-side fix is to accept any Number and widen it, while keeping PostgreSQLInt2BinaryProtocolValue using readShort() to preserve signed semantics. Example direction
     (illustrative):
   
   ```java
     // PaginationContext#getValue
     if (paginationValueSegment instanceof ParameterMarkerPaginationValueSegment) {
         Object obj = params == null || params.isEmpty() ? 0L : params.get(((ParameterMarkerPaginationValueSegment) paginationValueSegment).getParameterIndex());
         if (obj == null) {
             return null;
         }
         if (obj instanceof Number) {
             return ((Number) obj).longValue(); // unify Short/Integer/Long, etc.
         }
         throw new IllegalArgumentException("Unsupported pagination param type: " + obj.getClass());
     }
   ```
   
     We warmly welcome community contributors to submit a PR with this compatibility fix and add tests covering int2 pagination parameters—thanks!


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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org