Skip to content

fix(spanner): implement DbDataReader.GetChars - #16046

Open
alaricd wants to merge 2 commits into
googleapis:mainfrom
alaricd:codex/spanner-getchars
Open

alaricd wants to merge 2 commits into
googleapis:mainfrom
alaricd:codex/spanner-getchars

Conversation

@alaricd

@alaricd alaricd commented Oct 29, 2026

Copy link
Copy Markdown

Summary

Implement ranged character reads for Spanner STRING values, with length queries when the buffer is null. Reject null and non-STRING values with InvalidCastException, and add tests for ranged reads and these cases.

Resolves #16042.

Microsoft contract

Microsoft documents DbDataReader.GetChars as reading a requested character range from a column into a buffer: https://learn.microsoft.com/dotnet/api/system.data.common.dbdatareader.getchars. Its DataTableReader implementation documents InvalidCastException when the column does not contain character data and accepts nullable buffers: https://learn.microsoft.com/dotnet/api/system.data.datatablereader.getchars

Compatibility

This replaces NotSupportedException for Spanner STRING values with the documented read behavior. Other value types and SQL NULL values report InvalidCastException; callers that depended on the previous unsupported exception for strings will observe the new supported result.

Validation

dotnet test apis/Google.Cloud.Spanner.Data/Google.Cloud.Spanner.Data.Tests/Google.Cloud.Spanner.Data.Tests.csproj --no-restore -f net8.0 -p:NoWarn=CS8981%3BxUnit2004%3BxUnit2013 — 2,834 passed, 0 failed.

Discovery

Found using pengdows.AdoNetApiTest.conformance.

@alaricd
alaricd requested a review from a team as a code owner October 29, 2026 07:01
@google-cla

google-cla Bot commented Oct 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements the GetChars method in SpannerDataReader to support reading string data into character arrays, along with corresponding unit tests. A review comment suggests simplifying and correcting the buffer range validation checks to prevent misleading exception messages and improve clarity.

Comment on lines +202 to +204
GaxPreconditions.CheckArgumentRange(bufferOffset, nameof(bufferOffset), 0, buffer.Length);
GaxPreconditions.CheckArgumentRange(length, nameof(length), 0, buffer.Length);
GaxPreconditions.CheckArgumentRange(bufferOffset + length, nameof(length), 0, buffer.Length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The validation checks for length and bufferOffset + length can be simplified and made more precise. Currently, checking bufferOffset + length using nameof(length) as the parameter name can produce misleading exception messages (e.g., showing the sum of offset and length as the invalid 'length' value). By checking length against buffer.Length - bufferOffset, we can ensure that the requested length fits within the remaining buffer space. This mathematically guarantees that bufferOffset + length <= buffer.Length without needing a third check, and provides a much clearer error message if the validation fails.

            GaxPreconditions.CheckArgumentRange(bufferOffset, nameof(bufferOffset), 0, buffer.Length);
            GaxPreconditions.CheckArgumentRange(length, nameof(length), 0, buffer.Length - bufferOffset);

@alaricd
alaricd force-pushed the codex/spanner-getchars branch from 1528406 to f1545c7 Compare October 29, 2026 07:25
@alaricd alaricd changed the title Implement GetChars for Spanner STRING columns fix(spanner): implement DbDataReader.GetChars Oct 29, 2026
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Oct 29, 2026
@amanda-tarafa amanda-tarafa self-assigned this Oct 29, 2026
@amanda-tarafa
amanda-tarafa marked this pull request as draft October 29, 2026 16:28
@amanda-tarafa
amanda-tarafa self-requested a review October 29, 2026 16:28
@alaricd
alaricd marked this pull request as ready for review October 29, 2026 18:41

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

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpannerDataReader.GetChars should support STRING columns

2 participants