Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No data written when using WriteTableAsync using Clickhouse clickhouse/clickhouse-server:latest #77

Open
aidanmorgan opened this issue Apr 19, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@aidanmorgan
Copy link

aidanmorgan commented Apr 19, 2023

I have an interesting bug when trying to bulk insert data into a table using the WriteTableAsync function. I've tried to provide a simple reproduction here:

Database Structure:

CREATE DATABASE IF NOT EXISTS issue77_db;

CREATE TABLE IF NOT EXISTS issue77_db.issue77_table (
    words String,
    time DateTime64(9)
)
ENGINE=MergeTree()
ORDER BY time;

Using the clickhouse/clickhouse-server:latest docker image the following insert works:

using Octonica.ClickHouseClient;

await using var conn = new ClickHouseConnection("Host=localhost");
conn.Open();

var cmd = conn.CreateCommand("INSERT INTO issue77_db.issue77_table(words, time) VALUES({words}, {time})");
cmd.Parameters.AddWithValue("words", "direct_insert");
cmd.Parameters.AddWithValue("time", DateTimeOffset.UtcNow);

var res = cmd.ExecuteNonQuery();
Console.WriteLine(res);

Running a bulk insert for this however does not work, doing a SELECT * against the table returns zero rows.:

using Octonica.ClickHouseClient;

var cancelToken = new CancellationTokenSource();

await using var conn = new ClickHouseConnection("Host=localhost");
await conn.OpenAsync();

var writer = await conn.CreateColumnWriterAsync("INSERT INTO issue77_db.issue77_table(words, time) VALUES", cancelToken.Token);

await writer.WriteTableAsync(new object[]
{
    new List<string>() {"aa", "bb", "cc", "dd"},
    new List<DateTimeOffset>()
    {
        DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(3)), 
        DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(2)),
        DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(1)), 
        DateTimeOffset.UtcNow
    }
}, 4, cancelToken.Token);

Checking the logs for the server, there seems to be a repeated error log printed:

<Information> TCPHandler: Client has dropped the connection, cancel the query.
<Information> executeQuery: Code: 394. DB::Exception: Query was cancelled or a client has unexpectedly dropped the connection. (QUERY_WAS_CANCELLED) (version 23.3.1.2823 (official build)) 

Switching to the yandex/clickhouse-server:latest image returns four rows.

@aidanmorgan aidanmorgan changed the title Issue with WriteTableAsync using Clickhouse clickhouse/clickhouse-server:latest No data written when using WriteTableAsync using Clickhouse clickhouse/clickhouse-server:latest Apr 19, 2023
@SergeyMirvoda
Copy link
Contributor

@aidanmorgan
Hi!
Looks like another protocol change(

could you please submit the specific version numbers of both clickhouse versions.

select version()

21.3.2.5

@aidanmorgan
Copy link
Author

aidanmorgan commented Apr 19, 2023

Hi @SergeyMirvoda!

the yandex/clickhouse-server:latest version reports as 22.1.3.7
the clickhouse/clickhouse-server:latest version reports as: 23.3.1.2823

I did a bit of digging, and it looks like the issue is caused by a NullReferenceException that is getting swallowed internally:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Octonica.ClickHouseClient.ClickHouseConnection.CreateColumnWriter(String insertFormatCommand, Boolean async, CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.ExecuteFromThreadPool(Thread threadPoolThread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()

@kboniadi
Copy link

kboniadi commented Jul 7, 2023

Hi,

I'm running into the same issue. No data is being written when using the bulk insert example in the readme. My Clickhouse server version is

select version()
23.5.1.3174

@victor-sushko victor-sushko added the bug Something isn't working label Jul 30, 2023
@victor-sushko
Copy link
Contributor

The error seems to be caused by the lack of a backward compatibility in the ClickHouse server. Catching up with the current protocol revision should solve this problem.

@victor-sushko
Copy link
Contributor

This error is caused by a really useful feature in the ClickHouse server: (limited) support of ACID transactions.

In the current version of ClickHouseClient the method ClickHouseColumnWriter.EndWrite commits a transaction but also closes the session. In the next version the method ClickHouseColumnWriter.Commit will be available. This method will allow to commit a transaction without closing the session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants