ClickHouse/clickhouse-js

proper way to send commands with SET?

movy opened this issue · 2 comments

movy commented

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

  • Client version: 0.2.2
  • Language version: v20.5.1
  • OS: Ubuntu 22.02

ClickHouse server

  • ClickHouse Server version: 23.7.1.2470

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' } ]