From fa8fc728a96e53a04b88a07e8421ed49e4c5f226 Mon Sep 17 00:00:00 2001 From: Aleksander Krzeszowski Date: Tue, 27 Aug 2024 19:03:29 +0200 Subject: [PATCH] Fix co-variant array conversion breaking shared ArrayPool (#519) --- ClickHouse.Client.Tests/BulkCopyTests.cs | 24 ++++++++++++++++++++ ClickHouse.Client/Copy/ClickHouseBulkCopy.cs | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ClickHouse.Client.Tests/BulkCopyTests.cs b/ClickHouse.Client.Tests/BulkCopyTests.cs index 50451f2d..3cb47bbe 100644 --- a/ClickHouse.Client.Tests/BulkCopyTests.cs +++ b/ClickHouse.Client.Tests/BulkCopyTests.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -430,6 +431,29 @@ public async Task ShouldThrowExceptionOnInnerException(double fraction) Assert.ThrowsAsync(async () => await bulkCopy.WriteToServerAsync(data, CancellationToken.None)); } + [Test] + public async Task ShouldNotAffectSharedArrayPool() + { + var targetTable = "test." + SanitizeTableName($"array_pool"); + + await connection.ExecuteStatementAsync($"DROP TABLE IF EXISTS {targetTable}"); + await connection.ExecuteStatementAsync($"CREATE TABLE IF NOT EXISTS {targetTable} (int Int32, str String, dt DateTime) ENGINE Null"); + + const int poolSize = 8; + using var bulkCopy = new ClickHouseBulkCopy(connection) + { + DestinationTableName = targetTable, + BatchSize = poolSize + }; + + await bulkCopy.InitAsync(); + await bulkCopy.WriteToServerAsync(Enumerable.Repeat(new object[] { 0, "a", DateTime.Now }, 100)); + + var rentedArray = ArrayPool.Shared.Rent(poolSize); + Assert.DoesNotThrow(() => { rentedArray[0] = 1; }); + ArrayPool.Shared.Return(rentedArray); + } + private static string SanitizeTableName(string input) { var builder = new StringBuilder(); diff --git a/ClickHouse.Client/Copy/ClickHouseBulkCopy.cs b/ClickHouse.Client/Copy/ClickHouseBulkCopy.cs index 5f22ec67..97de3bbf 100644 --- a/ClickHouse.Client/Copy/ClickHouseBulkCopy.cs +++ b/ClickHouse.Client/Copy/ClickHouseBulkCopy.cs @@ -218,7 +218,7 @@ private IEnumerable IntoBatches(IEnumerable rows, string query, // Convenience argument collection private struct Batch : IDisposable { - public object[] Rows; + public object[][] Rows; public int Size; public string Query; public ClickHouseType[] Types; @@ -227,7 +227,7 @@ public void Dispose() { if (Rows != null) { - ArrayPool.Shared.Return(Rows); + ArrayPool.Shared.Return(Rows); Rows = null; } }