TimonKK / clickhouse

NodeJS client for ClickHouse
Apache License 2.0
221 stars 122 forks source link

feat: allow inserting Map using singlequoted JSON #123

Open mtrimolet opened 2 years ago

mtrimolet commented 2 years ago

Inserting a value for a Map field can be done using a key-value format which is a kind of singlequoted JSON (see Map(key, value) | ClickHouse Docs.

This is a proposal to use this feature from CH, enabling insertion for Map fields.

TimonKK commented 2 years ago

Able to you add some test for null insert?

mtrimolet commented 2 years ago

It seems the Map type can't be a Nullable by design (error below).

Query : CREATE TABLE ... rec3 Nullable(Map(String, UInt8)) [cut for clarity] Result : Error: Nested type Map(String, UInt8) cannot be inside Nullable type. (ILLEGAL_TYPE_OF_ARGUMENT) (version 21.12.4.1 (official build))

As for the other way around, yes I can add this in the tests if you confirm that it's what you intended.

Query : CREATE TABLE ... rec3 Map(String, Nullable(UInt8)) [cut for clarity] Row Data (JS) : { ... rec3: { a: null } ... } [cut for clarity] Result : OK

mtrimolet commented 2 years ago

There's a point to be made about nested Map/Array (see citation below), for which I expect this implementation to fail. But maybe it should be issued separately as it concerns more general aspects of this lib.

From Map(key, value) | ClickHouse Docs :

Parameters

  • key — [...]
  • value — The value part of the pair. Arbitrary type, including Map and Array.
Sh4yy commented 2 years ago

Any updates on this? Is there a solution that I can use for now?

mtrimolet commented 2 years ago

There's a point to be made about nested Map/Array (see citation below), for which I expect this implementation to fail.

Actually it seems to work just fine.

Sh4yy commented 2 years ago

Yep, I'm using a fork of your PR at the moment. I would appreciate it if this gets merged so I can switch over.