Skip to content

Fix getInputStream of memory query results returning serialized bytes - #40524

Open
thswlsqls wants to merge 5 commits into
apache:masterfrom
thswlsqls:40523
Open

thswlsqls wants to merge 5 commits into
apache:masterfrom
thswlsqls:40523

Conversation

@thswlsqls

@thswlsqls thswlsqls commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #40523.

Changes proposed in this pull request:

  • AbstractMemoryQueryResult#getInputStream serialized every value with ObjectOutputStream, so getBinaryStream, getAsciiStream and getUnicodeStream returned Java-serialized bytes under CONNECTION_STRICTLY.
  • Return null for SQL NULL, and the raw bytes for byte[] and Blob#getBinaryStream(), matching the getCharacterStream handling from Fix getCharacterStream of memory query results reading column index #40431.
  • For Oracle, return byte[] (RAW and LONG RAW) values as uppercase hexadecimal text for Ascii (US-ASCII) and Unicode (UTF-16BE) streams, as the Oracle driver does, and keep the raw bytes for Binary. The dialect opts in through a new DialectResultSetMapper#isHexadecimalBinaryCharacterStream default method (false elsewhere).
  • Encode Unicode streams as two-byte characters, high byte first (UTF-16BE), as ResultSet#getUnicodeStream requires, for text, Clob and SQLXML values.
  • For the other stream types, return Clob#getAsciiStream(), SQLXML#getBinaryStream() (or its string in the ASCII stream charset for Ascii), and encode other values from toString() in the ASCII stream charset for Ascii streams, and as UTF-8 for Binary under the existing character encoding TODO. The ASCII stream charset comes from a new DialectResultSetMapper#getAsciiStreamCharset default method: US-ASCII by default, as pgjdbc's getAsciiStream does, and ISO-8859-1 for Oracle, as the Oracle driver does.
  • Remove the serialization helper and declare throws SQLException on the override (already declared by QueryResult), since the Blob/Clob/SQLXML stream getters throw it.
  • Correct the QueryResult#getInputStream Javadoc: type is the stream type, and SQL NULL returns null.
  • Add VARCHAR, non-ASCII VARCHAR (Unicode and Ascii), VARBINARY (Ascii, and hexadecimal Ascii, Unicode and Binary), BLOB, CLOB (Ascii and Unicode), SQLXML (Binary, and non-ASCII Ascii and Unicode) and NULL cases to JDBCMemoryQueryResultTest; the hexadecimal VARBINARY cases fail without the Oracle change, the rest without the fix. Add a non-ASCII VARCHAR Ascii case with an ISO-8859-1 dialect charset, which fails without the dialect charset. The old test compared only the first serialized byte.

AI assistance: Claude Code was used to draft the change and its unit tests in infra/executor and database/connector (core and Oracle dialect). I reviewed, verified, and take responsibility for every line submitted.


Before committing this PR, I'm sure that I have checked the following options:

  • My code follows the code of conduct of this project.
  • I have self-reviewed the commit code.
  • I have (or in comment I request) added corresponding labels for the pull request. (Not done: I cannot set labels; a maintainer may add type: bug.)
  • I have passed maven check locally : ./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e. (Full build not run. Ran spotless:apply, checkstyle:check and apache-rat:check with -Pcheck -pl infra/executor (and database/connector/core, database/connector/dialect/oracle), then ./mvnw install -pl infra/executor -am -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e and ./mvnw test -pl infra/merge -am, all passing on JDK 21 with release 8.)
  • I have made corresponding changes to the documentation. (Not applicable: no documented behavior changes.)
  • I have added corresponding unit tests for my changes.
  • I have updated the Release Notes of the current development version. For more details, see Update Release Note
  • I have disclosed the tool and affected files or scope for any material AI assistance, as required by the AI-assisted contribution policy.

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR fixes memory query results returning Java-serialized bytes by returning raw bytes, LOB streams, or encoded text, and correctly handling SQL NULL. The approach addresses the serialization problem, but Ascii text streams still use the wrong encoding.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Blocking Issues: 1

Blocking Issues

  1. [P2] Preserve ASCII encoding for Ascii text streams

    • Evidence: AbstractMemoryQueryResult.java:108 ignores type and always uses UTF-8. The project-selected pgjdbc 42.7.13 explicitly uses US-ASCII for getAsciiStream.
    • Impact: For a PostgreSQL VARCHAR value of é, the memory path produces C3 A9, while the native driver path produces 3F. The stream content therefore changes with the connection mode. The added non-ASCII test covers only Unicode and cannot detect this issue.
    • Required Change: Apply the correct ASCII conversion for Ascii requests and add a regression test with non-ASCII input. The missing original column charset does not prevent converting an already-materialized String to ASCII.

Coverage

Public PR head: d8f5f61a7b3b28beea14492477646c920c1914ec. All 4 changed files were reviewed, with two independent assessments and coordinator evidence verification.
Local Spotless and Checkstyle passed, covering all 3 changed Java files. Unit-test attempts timed out during Maven prerequisites, so no test results were obtained.
This conclusion covers code correctness only; CI was not reviewed.

getInputStream ignored the stream type and always encoded text values as
UTF-8, so getAsciiStream on a memory query result returned C3 A9 for "é"
while pgjdbc returns 3F. Encode text values with US-ASCII for Ascii
requests and keep UTF-8 for the other stream types.
@thswlsqls

Copy link
Copy Markdown
Contributor Author

@terrymanu Thanks for the review. Done in 4805c95ec97: values encoded from toString() now use US-ASCII for Ascii streams, as pgjdbc's getAsciiStream does, and UTF-8 for the other stream types. assertGetAsciiInputStreamWithNonAsciiVarchar checks that é comes back as 3F and fails on the previous code. I also updated the PR description, which said the stream type did not affect the encoding.

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR fixes memory query results returning Java-serialized data by returning column bytes, LOB streams, or encoded text directly. ASCII encoding for ordinary text is now corrected, but Unicode encoding and SQLXML ASCII conversion remain incorrect.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Blocking Issues: 2

Blocking Issues

  1. [P2] Use the required encoding for Unicode streams

    • Evidence: AbstractMemoryQueryResult.java:108 uses UTF-8 for Unicode, while the JDBC contract requires two-byte Unicode characters, high byte first. A probe using the exact candidate source returned C3 A9 for é, instead of 00 E9. The added test also asserts the incorrect encoding.
    • Impact: Under CONNECTION_STRICTLY, getUnicodeStream() returns data that cannot be decoded correctly according to its public contract.
    • Required Change: Handle Unicode conversion separately, use the required byte layout, and correct the test expectations. Pgjdbc’s UTF-8 implementation and the method’s deprecated status do not supersede that contract.
  2. [P2] Honor ASCII requests in the SQLXML branch

    • Evidence: AbstractMemoryQueryResult.java:104–105 unconditionally calls SQLXML#getBinaryStream() and returns before reaching the ASCII conversion. A probe using actual pgjdbc 42.7.13 classes showed that, for <foo>é</foo>, the native ASCII getter converts é to 3F, while the memory path returns UTF-8 bytes C3 A9.
    • Impact: With a default PostgreSQL UTF-8 connection, SQLXML ASCII streams still contain non-ASCII bytes, and their contents change with the connection mode.
    • Required Change: Honor ASCII requests in the SQLXML branch and add a regression test containing non-ASCII XML. The existing SQLXML test covers only Binary.

Both issues already existed at the previously reviewed head, d8f5f61. They were missed in the previous review and were not introduced by the latest commit.

Coverage

Reviewed all 4 changed files at public PR head 4805c95ec97a71795e77326b57d76817e76a12df, with two independent assessments and coordinator verification.
Local Spotless and Checkstyle passed for all 3 changed Java files. Source and driver probes reproduced the issues above. Both focused Maven test attempts timed out before test execution, so no unit-test results were obtained.
This conclusion covers code correctness only; CI was not reviewed. Repository files and pre-existing workspace changes were preserved.

getUnicodeStream requires two-byte Unicode characters with the high byte first, so Unicode streams now encode the column text (including Clob and SQLXML values) as UTF-16BE instead of UTF-8. SQLXML Ascii streams now convert the XML string to US-ASCII instead of returning the binary stream.
@thswlsqls

Copy link
Copy Markdown
Contributor Author

@terrymanu Thanks. Fixed in 03d189f: Unicode streams are now encoded as UTF-16BE (é → 00 E9) for text, Clob and SQLXML values, and the SQLXML branch converts getString() to US-ASCII for Ascii (<foo>é</foo> → <foo>?</foo>). The test expectations are corrected, and the new non-ASCII SQLXML and Clob cases fail on the previous head. I also updated the PR description.

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR fixes memory query results returning Java-serialized data by returning column bytes, LOB streams, or encoded text, and correctly handling SQL NULL. The previous text Unicode and SQLXML ASCII issues are fixed, but binary values still bypass the requested stream-type conversion.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Blocking Issues: 1

Oracle RAW ASCII/Unicode streams still return incorrect content.

Blocking Issues

  1. [P2] Preserve Oracle RAW ASCII/Unicode conversions

    • Evidence: AbstractMemoryQueryResult.java:95 returns byte[] immediately, ignoring type. Oracle RAW values reach this branch through ResultSetMapper#getBytes. The Oracle documentation specifies hexadecimal representations for character streams. Probes using the Oracle 23.6 driver converter and the exact candidate source confirmed that, for input CA FE, the native ASCII/Unicode conversions return the string CAFE encoded as ASCII/UTF-16BE, respectively, while the current candidate returns the raw bytes CA FE for both.
    • Impact: Under CONNECTION_STRICTLY, supported RAW character-stream reads still violate their conversion contracts.
    • Required Change: Apply the appropriate ASCII/Unicode conversion for the database and column type, preserve raw bytes for Binary requests, and add Oracle RAW regression coverage.

    This branch already existed at the previously reviewed head, 4805c95. It was missed in the previous review and was not introduced by the latest commit.

Coverage

Reviewed all 4 changed files and the relevant call paths at the latest head, 03d189fdab679a3d5394e75041aa614ae07ec312, with independent assessments and coordinator verification.
Local Checkstyle and Spotless passed for all 3 changed Java files. The focused Maven test attempt timed out during dependency prerequisites before executing tests. Driver verification used source inspection and converter probes; no Oracle database was started.
This conclusion covers code correctness only; CI was not reviewed. Repository files and Git state were preserved.

Oracle returns the hexadecimal text of RAW and LONG RAW values for
getAsciiStream and getUnicodeStream, and the raw bytes only for
getBinaryStream. The memory query result returned the raw bytes for
every stream type.

Add DialectResultSetMapper#isHexadecimalBinaryCharacterStream, true for
Oracle, and use it in JDBCMemoryQueryResult so byte[] values are encoded
as uppercase hexadecimal in US-ASCII or UTF-16BE for those streams.
@thswlsqls

Copy link
Copy Markdown
Contributor Author

@terrymanu Thanks. byte[] streams now follow the dialect: a new DialectResultSetMapper#isHexadecimalBinaryCharacterStream (default false) returns true for Oracle, so Ascii and Unicode streams return the uppercase hexadecimal text in US-ASCII and UTF-16BE while Binary keeps the raw bytes. JDBCMemoryQueryResultTest covers all three for CA FE, and the two character-stream cases fail without the change; OracleResultSetMapperTest covers the flag.

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR fixes memory query results returning Java-serialized data under CONNECTION_STRICTLY by returning column bytes, LOB streams, or encoded text, and adds Oracle RAW conversion. The direction is sound, but two text-encoding defects and two testing-standard violations remain.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Blocking Issues: 4

Blocking Issues

  1. [P2] Preserve Oracle text ASCII conversion semantics

    • Evidence: AbstractMemoryQueryResult.java:119 universally uses US-ASCII, while the Oracle documentation specifies ISO-Latin-1 for its ASCII streams. An actual ojdbc8 23.6 converter returns E9 for é; the current memory path returns 3F.
    • Impact: Oracle VARCHAR characters become question marks, making results depend on the connection mode. The new RAW branch does not handle String values.
    • Required Change: Preserve dialect-specific text ASCII conversion and add non-ASCII regression coverage.
  2. [P2] Preserve Binary bytes for non-UTF-8 text

    • Evidence: The same return branch forces UTF-8 for Binary streams. Connector/J 8.4.0 reads through its raw-byte factory. An actual driver probe returned E9 for a latin1 VARCHAR containing é, while the memory path re-encoded it as C3 A9.
    • Impact: getBinaryStream() returns different column contents depending on the connection mode. The charset TODO does not establish a contract allowing this difference.
    • Required Change: Preserve the original bytes or actual result charset, and add a non-UTF-8 text regression case.
  3. [P2] Remove the constant-only test

    • Evidence: OracleResultSetMapperTest.java:81 only asserts that a method unconditionally returns true, contrary to the testing standards.
    • Impact: The new test violates an explicit repository rule.
    • Required Change: Remove this standalone test and its unused import, while retaining the stream-conversion tests.
  4. [P2] Use the applicable static mocking extension

    • Evidence: JDBCMemoryQueryResultTest.java:426 directly calls mockStatic without satisfying the documented exception conditions. Isolated verification confirmed that all 54 tests still pass using the existing extension with explicit default stubbing.
    • Impact: The new tests bypass the applicable project testing mechanism.
    • Required Change: Use AutoMockExtension, preserving the existing default mappings and stubbing semantics.

Issues 1 and 2 already existed at the previous head, 03d189fd, and were missed in previous reviews. Issues 3 and 4 were introduced by the latest commit.

Coverage

Reviewed all 8 changed files at public head 97422d41b964670122c5094b98e1e13dc3a99358, with two independent assessments and coordinator verification.
Local Checkstyle and Spotless passed for all 7 Java files (exit 0). The current memory-result test class passed 54/54 tests in isolation; driver probes reproduced the encoding differences.
The Maven reactor test attempt timed out during project scanning (exit 124), so full testing remains incomplete. Probes did not connect to a database. This conclusion covers code correctness only; CI was not reviewed.

Add DialectResultSetMapper#getAsciiStreamCharset, defaulting to US-ASCII,
and return ISO-8859-1 for Oracle so that ASCII streams of character
values in memory query results match the Oracle driver.

Remove the constant-only Oracle mapper test, and switch
JDBCMemoryQueryResultTest to AutoMockExtension with explicit default
stubbing of DatabaseTypedSPILoader.
@thswlsqls

Copy link
Copy Markdown
Contributor Author

@terrymanu Thanks. Issues 1, 3 and 4 are addressed in b308ea6: a new DialectResultSetMapper#getAsciiStreamCharset (US-ASCII by default) returns ISO-8859-1 for Oracle, so Ascii streams of character values now return E9 for é (assertGetAsciiInputStreamWithNonAsciiVarcharAndDialectAsciiStreamCharset); the constant-only Oracle test is removed; and JDBCMemoryQueryResultTest uses AutoMockExtension with explicit default stubbing.

For issue 2, the memory loader keeps character columns as the String from getString, so the original bytes are gone before getBinaryStream is called. I see two ways to keep them: (A) also load getBytes for character columns, which roughly doubles row memory under CONNECTION_STRICTLY, or (B) a dialect hook that resolves the column charset from driver-specific result metadata and encodes with it. Which would you prefer here, or should the Binary case of text columns move to a separate issue?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResultSet getBinaryStream returns Java-serialized bytes instead of the column value for memory query results

2 participants