Skip to content

Commit

Permalink
Add default_format setting (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn authored Sep 1, 2023
1 parent c8f24d7 commit 50f790f
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .docker/clickhouse/single_node_tls/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM clickhouse/clickhouse-server:23.5-alpine
FROM clickhouse/clickhouse-server:23.8-alpine
COPY .docker/clickhouse/single_node_tls/certificates /etc/clickhouse-server/certs
RUN chown clickhouse:clickhouse -R /etc/clickhouse-server/certs \
&& chmod 600 /etc/clickhouse-server/certs/* \
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.2 (Common, Node.js & Web)

### New features

- Added `default_format` setting, which allows to perform `exec` calls without `FORMAT` clause.

## 0.2.1 (Common, Node.js & Web)

### Breaking changes
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '2.3'

services:
clickhouse1:
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.5-alpine}'
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.8-alpine}'
ulimits:
nofile:
soft: 262144
Expand All @@ -19,7 +19,7 @@ services:
- './.docker/clickhouse/users.xml:/etc/clickhouse-server/users.xml'

clickhouse2:
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.5-alpine}'
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.8-alpine}'
ulimits:
nofile:
soft: 262144
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.8'
services:
clickhouse:
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.5-alpine}'
image: 'clickhouse/clickhouse-server:${CLICKHOUSE_VERSION-23.8-alpine}'
container_name: 'clickhouse-js-clickhouse-server'
ports:
- '8123:8123'
Expand Down
17 changes: 17 additions & 0 deletions examples/default_format_setting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createClient, ResultSet } from '@clickhouse/client' // or '@clickhouse/client-web'

void (async () => {
const client = createClient()
const format = 'JSONCompactEachRowWithNamesAndTypes'
const { stream, query_id } = await client.exec({
// this query fails without `default_format` setting
// as it does not have the FORMAT clause
query: `SELECT database, name, engine FROM system.tables LIMIT 5`,
clickhouse_settings: {
default_format: format,
},
})
const rs = new ResultSet(stream, format, query_id)
console.log(await rs.json())
await client.close()
})()
6 changes: 5 additions & 1 deletion karma.config.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const webpackConfig = require('./webpack.dev.js')

const TEST_TIMEOUT_MS = 120_000

module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
Expand Down Expand Up @@ -43,14 +45,16 @@ module.exports = function (config) {
autoWatch: false,
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless', 'FirefoxHeadless'],
browserNoActivityTimeout: TEST_TIMEOUT_MS,
browserDisconnectTimeout: TEST_TIMEOUT_MS,
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
client: {
jasmine: {
random: false,
stopOnSpecFailure: false,
stopSpecOnExpectationFailure: true,
timeoutInterval: 60000,
timeoutInterval: TEST_TIMEOUT_MS,
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ describe('ClickHouse server errors parsing', () => {
})

it('returns "unknown table" error', async () => {
// possible error messages here:
// (since 23.8+) Table foo.unknown_table does not exist.
// (pre-23.8) Table foo.unknown_table doesn't exist.
const errorMessagePattern = `^Table ${getTestDatabaseName()}.unknown_table does(n't| not) exist.*$`
await expectAsync(
client.query({
query: 'SELECT * FROM unknown_table',
})
).toBeRejectedWith(
jasmine.objectContaining({
message: `Table ${getTestDatabaseName()}.unknown_table doesn't exist. `,
message: jasmine.stringMatching(errorMessagePattern),
code: '60',
type: 'UNKNOWN_TABLE',
})
Expand Down
5 changes: 4 additions & 1 deletion packages/client-common/__tests__/integration/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ describe('insert', () => {
})
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringContaining('Unknown setting foobar'),
// Possible error messages:
// Unknown setting foobar
// Setting foobar is neither a builtin setting nor started with the prefix 'SQL_' registered for user-defined settings.
message: jasmine.stringContaining('foobar'),
code: '115',
type: 'UNKNOWN_SETTING',
})
Expand Down
5 changes: 4 additions & 1 deletion packages/client-common/__tests__/integration/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ describe('select', () => {
})
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringContaining('Unknown setting foobar'),
// Possible error messages:
// Unknown setting foobar
// Setting foobar is neither a builtin setting nor started with the prefix 'SQL_' registered for user-defined settings.
message: jasmine.stringContaining('foobar'),
code: '115',
type: 'UNKNOWN_SETTING',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ describe('select with query binding', () => {
})
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringContaining(
'Query parameter `min_limit` was not set'
message: jasmine.stringMatching(
// possible error messages here:
// (since 23.8+) Substitution `min_limit` is not set.
// (pre-23.8) Query parameter `min_limit` was not set
/^.+?`min_limit`.+?not set.*$/
),
code: '456',
type: 'UNKNOWN_QUERY_PARAMETER',
Expand Down
3 changes: 2 additions & 1 deletion packages/client-common/__tests__/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { guid } from './guid'
import { getClickHouseTestEnvironment, TestEnv } from './test_env'
import { TestLogger } from './test_logger'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 120_000

let databaseName: string
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
if (
getClickHouseTestEnvironment() === TestEnv.Cloud &&
databaseName === undefined
Expand Down
2 changes: 2 additions & 0 deletions packages/client-common/src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DataFormat } from './data_formatter'
/**
* @see {@link https://github.com/ClickHouse/ClickHouse/blob/46ed4f6cdf68fbbdc59fbe0f0bfa9a361cc0dec1/src/Core/Settings.h}
* @see {@link https://github.com/ClickHouse/ClickHouse/blob/eae2667a1c29565c801be0ffd465f8bfcffe77ef/src/Storages/MergeTree/MergeTreeSettings.h}
Expand Down Expand Up @@ -1579,6 +1580,7 @@ interface ClickHouseServerSettings {

interface ClickHouseHTTPSettings {
wait_end_of_query: Bool
default_format: DataFormat
}

export type ClickHouseSettings = Partial<ClickHouseServerSettings> &
Expand Down
2 changes: 1 addition & 1 deletion packages/client-common/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.1'
export default '0.2.2'
13 changes: 13 additions & 0 deletions packages/client-node/__tests__/integration/node_exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ClickHouseClient } from '@clickhouse/client-common'
import { createTestClient } from '@test/utils'
import type Stream from 'stream'
import { getAsText } from '../../src/utils'
import { ResultSet } from '../../src'

describe('[Node.js] exec result streaming', () => {
let client: ClickHouseClient<Stream.Readable>
Expand Down Expand Up @@ -44,5 +45,17 @@ describe('[Node.js] exec result streaming', () => {
})
expect(await getAsText(result.stream)).toEqual('0\n')
})

it('should work with default_format', async () => {
const format = 'JSONEachRow'
const { stream, query_id } = await client.exec({
query: 'SELECT number FROM system.numbers LIMIT 1',
clickhouse_settings: {
default_format: format,
},
})
const rs = new ResultSet(stream, format, query_id)
expect(await rs.json()).toEqual([{ number: '0' }])
})
})
})
2 changes: 1 addition & 1 deletion packages/client-node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.1'
export default '0.2.2'
13 changes: 13 additions & 0 deletions packages/client-web/__tests__/integration/web_exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ClickHouseClient } from '@clickhouse/client-common'
import { createTestClient } from '@test/utils'
import { getAsText } from '../../src/utils'
import { ResultSet } from '../../src'

describe('[Web] exec result streaming', () => {
let client: ClickHouseClient<ReadableStream>
Expand Down Expand Up @@ -43,5 +44,17 @@ describe('[Web] exec result streaming', () => {
})
expect(await getAsText(result.stream)).toEqual('0\n')
})

it('should work with default_format', async () => {
const format = 'JSONEachRow'
const { stream, query_id } = await client.exec({
query: 'SELECT number FROM system.numbers LIMIT 1',
clickhouse_settings: {
default_format: format,
},
})
const rs = new ResultSet(stream, format, query_id)
expect(await rs.json()).toEqual([{ number: '0' }])
})
})
})
2 changes: 1 addition & 1 deletion packages/client-web/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.1'
export default '0.2.2'

0 comments on commit 50f790f

Please sign in to comment.