ClickHouse.Client icon indicating copy to clipboard operation
ClickHouse.Client copied to clipboard

Inserting data, how?

Open rogeralsing opened this issue 2 years ago • 4 comments

I'm stuck on the most trivial aspect of just inserting data and I don't find anything in the docs that helps me with this

The calling code:

var span = new Span
{
    timestamp = DateTimeOffset.Now,
    trace_id = "1234567890",
    model = "test"
};
await InsertSpan(connection, span);

The entity:

[PublicAPI]
public class Span
{
    public DateTimeOffset timestamp { get; set; }
    public string trace_id { get; set; }
    public string model { get; set; }
}

The code to insert an entity:

async Task InsertSpan(IDbConnection conn, Span span)
{
    var sqlInsert = 
"""
INSERT
INTO spans (timestamp, trace_id, model) 
VALUES (@timestamp, @trace_id, @model)
""";
    var res = await conn.ExecuteAsync(sqlInsert, span);
    Console.WriteLine(res);
}

It throws with the following exception:

Unhandled exception. ClickHouse.Client.ClickHouseServerException (0x0000003E): Code: 62. DB::Exception: Cannot parse expression of type DateTime64(9) here: @timestamp, @trace_id, @model): While executing ValuesBlockInputFormat. (SYNTAX_ERROR) (version 23.2.1.2537 (official build))

Is it a me problem? is it a library problem? is it a Clickhouse issue?

rogeralsing avatar Mar 10 '23 16:03 rogeralsing

@rogeralsing This can be due to this documented behavior https://github.com/DarkWanderer/ClickHouse.Client/wiki/ORM-support#dapper.

wahmedswl avatar Mar 19 '23 06:03 wahmedswl

Thanks @wahmedswl , I have it working now 👍🏻 One thing I wish for here, is better error messages, Clickhouse itself seems to be pretty weak in this area, and the client lib isn't super clear either here.

If anon objects are not supported, it would be pretty neat with an exception pointing this out and even linking to the docs. just random errors like "failed to parse datetime" can be a real timesink when you don't quite know what you are looking for.

rogeralsing avatar Mar 19 '23 08:03 rogeralsing

@rogeralsing Yes, you are right. Can you contribute this? Might be @DarkWanderer has some plans to support anonymous objects?

wahmedswl avatar Mar 19 '23 09:03 wahmedswl

Hi

Sorry for late response. No, I do not plan to support anonymous objects directly in the library. However, I do plan to maintain support for Dapper, and Dapper.Contrib has support for insertion - so this is on the table

Meanwhile, data insertion can be done in the "plain SQL" way: https://github.com/DarkWanderer/ClickHouse.Client/blob/c9ad6c562eb30ec46a73fdc33dd773017f27b44d/ClickHouse.Client.Tests/ParameterizedInsertTests.cs#L12-L25

DarkWanderer avatar Apr 15 '23 08:04 DarkWanderer