Skip to content

ORDER BY on queries with large TEXT columns causes protocol parsing error #3

@coogle

Description

@coogle

Description

Executing a SELECT query with an ORDER BY clause on a result set containing large TEXT columns causes a MySQLProtocolException:

MySQLProtocolException: Wrong first byte, while decoding getVariableEncInt

Removing the ORDER BY clause from the same query resolves the error. The issue is reproducible on both mysql_client 0.0.27 and mysql_client_plus 0.0.31.

Environment

  • mysql_client_plus: 0.0.31
  • Dart SDK: 3.10.4
  • Platform: macOS (Flutter desktop app)
  • MySQL: 8.x (Docker)
  • Auth: caching_sha2_password (default SSL / secure: true)

Reproduction

Failing query

SELECT
  id,
  name,
  ST_AsText(geometry, 'axis-order=long-lat') AS wkt,
  category,
  source
FROM polygons
WHERE is_active = 1
ORDER BY category, source
  • ~1,500 rows returned
  • wkt column contains WKT strings ranging from ~800 bytes to ~18 KB

Working query (identical but without ORDER BY)

SELECT
  id,
  name,
  ST_AsText(geometry, 'axis-order=long-lat') AS wkt,
  category,
  source
FROM polygons
WHERE is_active = 1

Connection code

final conn = await MySQLConnection.createConnection(
    host: '127.0.0.1',
    port: 3306,
    userName: 'user',
    password: 'password',
    databaseName: 'mydb',
);
await conn.connect();

// This throws MySQLProtocolException with ORDER BY,
// works fine without it.
final result = await conn.execute(sql);

Observations

  • The error occurs on the very first query after a fresh connection — not a stale/reused connection issue.
  • Adding LIMIT 5 to the ORDER BY query does not fix it (still fails).
  • The same query without ORDER BY works with the full ~1,500 row result set.
  • Queries without large TEXT columns work fine with ORDER BY.

Root cause hypothesis

When MySQL sorts a result set containing large TEXT columns, it buffers the rows differently before sending them (likely using temporary tables or filesort with different packet framing). The library's packet parser misreads the packet boundaries in the sorted response, causing it to interpret a data byte as a length-encoded integer prefix, which then fails validation.

Workaround

Remove ORDER BY from the SQL query and sort the results client-side in Dart.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions