ClickHouse / clickhouse-js

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

proper way to send commands with SET? #201

Closed movy closed 11 months ago

movy commented 11 months ago

Describe the bug

I am trying to change output_format_json_quote_64bit_integers via SET command and cannot get client to respect new setting: regardless of the setting, Int64 values are always quoted

Code example

import { createClient } from '@clickhouse/client'

const client = createClient({
    host: 'http://127.0.0.1:8123',
    password: 'password',
    username: 'default',
})

for (const setting in [0, 1]) {
    await client.command({
        query: `SET output_format_json_quote_64bit_integers = ${setting}`,
        clickhouse_settings: { wait_end_of_query: 1 }
    })

    const rows = await client.query({
        query: `SELECT toInt64(${setting})`,
        format: 'JSONEachRow'
    })

    console.log({ setting }, await rows.json())
}

Output:

{ setting: '0' } [ { 'toInt64(0)': '0' } ]
{ setting: '1' } [ { 'toInt64(1)': '1' } ]

Environment

ClickHouse server

slvrtrn commented 11 months ago

SET and other session-level commands should work if you provide the session id to the client.

void (async () => {
  const client = createClient({
    session_id: crypto.randomUUID(),
  })
  for (const setting in [0, 1]) {
    await client.command({
      query: `SET output_format_json_quote_64bit_integers = ${setting}`,
      clickhouse_settings: { wait_end_of_query: 1 }
    })

    const rows = await client.query({
      query: `SELECT toInt64(${setting})`,
      format: 'JSONEachRow'
    })

    console.log({ setting }, await rows.json())
  }
  await client.close()
})()

Prints:

{ setting: '0' } [ { 'toInt64(0)': 0 } ]
{ setting: '1' } [ { 'toInt64(1)': '1' } ]

Additionally, you can just use clickhouse_settings for that:

void (async () => {
  const client = createClient()
  const rows1 = await client.query({
    query: 'SELECT toInt64(0)',
    format: 'JSONEachRow',
    clickhouse_settings: {
      output_format_json_quote_64bit_integers: 0,
    }
  })
  console.log(await rows1.json())
  const rows2 = await client.query({
    query: 'SELECT toInt64(1)',
    format: 'JSONEachRow',
    clickhouse_settings: {
      output_format_json_quote_64bit_integers: 1,
    }
  })
  console.log(await rows2.json())
  await client.close()
})()

yields the same

[ { 'toInt64(0)': 0 } ]
[ { 'toInt64(1)': '1' } ]
slvrtrn commented 11 months ago

@movy, added an example based on your code: https://github.com/ClickHouse/clickhouse-js/blob/main/examples/session_level_commands.ts