Skip to content

Expand writing type support for String/FixedString types and add ReadStringsAsByteArrays setting#174

Merged
alex-clickhouse merged 10 commits intomainfrom
string-writing
Feb 3, 2026
Merged

Expand writing type support for String/FixedString types and add ReadStringsAsByteArrays setting#174
alex-clickhouse merged 10 commits intomainfrom
string-writing

Conversation

@alex-clickhouse
Copy link
Copy Markdown
Collaborator

@alex-clickhouse alex-clickhouse commented Jan 15, 2026

Note

High Risk
Changes core type mapping/serialization for String/FixedString (including default FixedString read type shifting to string), plus new connection-level behavior that can affect all query results. This is broadly user-facing and could be a breaking behavior change for consumers expecting FixedString as byte[].

Overview
Adds a new connection string/client setting ReadStringsAsByteArrays that switches how String and FixedString values are materialized on read (byte[] vs UTF-8 string), and threads this through ClickHouseConnection/TypeSettings/binary type decoding.

Expands BulkCopy/type writers for String and FixedString to accept Stream and ReadOnlyMemory<byte> inputs (including buffering non-seekable streams and validating FixedString(N) lengths with clearer errors). Updates tests, JSON handling for FixedString values, public API metadata, release notes, and adds a new examples/DataTypes_004_StringHandling.cs walkthrough.

Written by Cursor Bugbot for commit 0b96ab2. This will update automatically on new commits. Configure here.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces binary data handling capabilities for ClickHouse String and FixedString types, along with a new connection setting to read these types as raw byte arrays instead of UTF-8 strings.

Changes:

  • Adds ReadStringsAsByteArrays connection setting to control whether String/FixedString columns are read as byte[] or string
  • Extends String/FixedString write support to accept ReadOnlyMemory<byte> (NET6+) and Stream (seekable and non-seekable) via BulkCopy
  • Updates type system infrastructure to propagate the new setting through TypeSettings, ClickHouseClientSettings, and connection string builder

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
RELEASENOTES.md Documents the new binary write support and ReadStringsAsByteArrays setting
ClickHouse.Driver/Types/TypeConverter.cs Propagates ReadStringsAsByteArrays setting to StringType instances during type parsing
ClickHouse.Driver/Types/StringType.cs Adds ReadAsByteArray property and implements binary read/write for byte[], ReadOnlyMemory, and Stream
ClickHouse.Driver/Types/FixedStringType.cs Adds ReadAsByteArray property, implements binary read with UTF-8 decode fallback, and Stream/ReadOnlyMemory write support
ClickHouse.Driver/Types/BinaryTypeDecoder.cs Passes ReadStringsAsByteArrays setting when decoding String/FixedString from binary protocol
ClickHouse.Driver/TypeSettings.cs Adds readStringsAsByteArrays parameter to TypeSettings record
ClickHouse.Driver/ADO/ClickHouseDefaults.cs Defines default value (false) for ReadStringsAsByteArrays setting
ClickHouse.Driver/ADO/ClickHouseConnectionStringBuilder.cs Adds ReadStringsAsByteArrays property with connection string serialization
ClickHouse.Driver/ADO/ClickHouseConnection.cs Passes ReadStringsAsByteArrays to TypeSettings constructor
ClickHouse.Driver/ADO/ClickHouseClientSettings.cs Adds ReadStringsAsByteArrays property with copy constructor, equality, and ToString support
ClickHouse.Driver.Tests/Types/DynamicTests.cs Tests ReadStringsAsByteArrays with Dynamic type wrapping String and FixedString
ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Comprehensive tests for ReadOnlyMemory, Stream (seekable/non-seekable), invalid UTF-8, and default behavior

Comment thread ClickHouse.Driver/Types/FixedStringType.cs
Comment thread ClickHouse.Driver/Types/FixedStringType.cs Outdated
Comment thread ClickHouse.Driver/Types/StringType.cs Outdated
Comment thread ClickHouse.Driver/ADO/ClickHouseClientSettings.cs
Comment thread ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Outdated
Comment thread ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Outdated
Comment thread ClickHouse.Driver/Types/FixedStringType.cs Outdated
Comment thread ClickHouse.Driver/Types/StringType.cs
Comment thread ClickHouse.Driver/Types/FixedStringType.cs
Comment thread ClickHouse.Driver/Types/FixedStringType.cs
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 16, 2026

Codecov Report

❌ Patch coverage is 92.20779% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ClickHouse.Driver/Types/StringType.cs 85.71% 1 Missing and 2 partials ⚠️
ClickHouse.Driver/Types/FixedStringType.cs 94.59% 0 Missing and 2 partials ⚠️
ClickHouse.Driver/Types/JsonType.cs 50.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Comment thread ClickHouse.Driver.Tests/Utilities/TestUtilities.cs Outdated
Comment thread ClickHouse.Driver/Types/StringType.cs
@alex-clickhouse alex-clickhouse changed the title Add binary data support for String/FixedString types and ReadStringsAsByteArrays setting Expand writing type support for String/FixedString types and add ReadStringsAsByteArrays setting Jan 19, 2026
Comment thread ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Outdated
@alex-clickhouse alex-clickhouse marked this pull request as draft January 19, 2026 12:49
{
return bytes;
}
return Encoding.UTF8.GetString(bytes);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe it's better to make this a property of ClientSettings?
ClientSettings.StringEncoding, ClientSettings.FixedStringEncoding?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps I'm misunderstanding your comment, but the value comes from ClientSettings (passed to TypeSettings -> ReadAsByteArray property).

Do you think separate settings for String and FixedString would be valuable?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Encoding.UTF8 - encoding hard-coded in FixedStringType. Probably better to make it as a ClientSettings part.

@alex-clickhouse alex-clickhouse marked this pull request as ready for review January 28, 2026 15:02
Comment thread ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Outdated
Comment thread ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs Outdated
public async Task ShouldInsertFixedStringFromReadOnlyMemory()
{
var targetTable = "test.bulk_fixedstring_memory";
var testData = new byte[] { 121, 122, 123, 124 };
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.

Looks like we have only the happy path. Can we also test if we have content with a size of 4+
var testData = new byte[] { 121, 122, 123, 124, 111, 111}; -> not happy path to test FixedString

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We have that below in eg WriteToServerAsync_FixedStringWithReadOnlyMemoryTooLong_ThrowsArgumentException

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread ClickHouse.Driver.Tests/Utilities/TestUtilities.cs
@alex-clickhouse alex-clickhouse merged commit 50ad75c into main Feb 3, 2026
18 checks passed
@alex-clickhouse alex-clickhouse deleted the string-writing branch February 3, 2026 09:27
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.

4 participants