ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
205 stars 25 forks source link

Cannot get enums to work with query_params #209

Closed Looskie closed 10 months ago

Looskie commented 10 months ago

When I use query_params with an enum, it does not work.

Steps to reproduce

  1. Create a local ch server and run the table query
  2. Connect
  3. run the code example below

Expected behaviour

to work :D

Code example

await client.command({
  query: `INSERT INTO default.customers (id, customer_name, is_admin, created_at, random_enum, pi, card_number, uuid) VALUES ({v0: String}, {v1: String}, {v2: Boolean}, {v3: DateTime64}, {v4: Enum8}, {v5: Float32}, {v6: Int32}, {v7: UUID})`,
  query_params: {
      v0: 'd65d104b-8398-419a-9b7f-87d13872d5f7',
      v1: 'John Doe',
      v2: false,
      v3: new Date(),
      v4: 'A',
      v5: 3.14,
      v6: 123456789,
      v7: '123'
    }
})

Error log

Enum data type cannot be empty: While executing ValuesBlockInputFormat.

Configuration

Environment

ClickHouse server

I feel like I'm missing something so obvious

slvrtrn commented 10 months ago

There are several ways to bind an enum.

Either send a string representation of your enum (A/B/C) and bind as a String, index (0/1/2) and bind as a UInt8, or provide an entire type Enum8('A' = 0, 'B' = 1, 'C' = 2) (it complaints about precisely that, Enum8 != Enum8('A' = 0, 'B' = 1, 'C' = 2)).

See also: https://github.com/ClickHouse/clickhouse-js/blob/50f790f660dcb9d91e7c224c411dd8cbbe7055b2/packages/client-common/__tests__/integration/select_query_binding.test.ts#L201-L274

EDIT: this works.

  await client.command({
    query: `INSERT INTO default.customers (id, customer_name, is_admin, created_at, random_enum, pi, card_number, uuid)
            VALUES ({v0: String}, {v1: String}, {v2: Boolean}, {v3: DateTime64}, {v4: String}, {v5: Float32},
                    {v6: Int32}, {v7: UUID})`,
    query_params: {
      v0: 'd65d104b-8398-419a-9b7f-87d13872d5f7',
      v1: 'John Doe',
      v2: false,
      v3: new Date(),
      v4: 'A',
      v5: 3.14,
      v6: 123456789,
      v7: crypto.randomUUID(),
    },
  })
Looskie commented 10 months ago

sweet, thank you so much for the quick answer ❤️