Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose X-ClickHouse-Summary header info #214

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## 0.2.7 (Web only)
## 0.2.7 (Common, Node.js, Web)

### New features

- (Node.js only) `X-ClickHouse-Summary` response header is now parsed when working with `insert`/`exec`/`command` methods.
See the [related test](./packages/client-node/__tests__/integration/node_summary.test.ts) for more details.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, something to discuss for sure. In tests, for non-streaming operations, it works regardless.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Changelog, it should work for non-streaming operations

NB: it is guaranteed to be correct only for non-streaming scenarios.
Web version does not currently support this due to CORS limitations. ([#210](https://github.com/ClickHouse/clickhouse-js/issues/210))

### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion karma.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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)
// base path that will be used to resolve all patterns (e.g. files, exclude)
basePath: '',
frameworks: ['webpack', 'jasmine'],
// list of files / patterns to load in the browser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ describe('data types', () => {
).toEqual('3\n')
})

it('should work with geo', async () => {
// FIXME: somehow broken after, probably, https://github.com/ClickHouse/ClickHouse/pull/56724
slvrtrn marked this conversation as resolved.
Show resolved Hide resolved
// enable once resolved.
xit('should work with geo', async () => {
const values = [
{
p: [42, 144],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('insert', () => {
let tableName: string

beforeEach(async () => {
client = await createTestClient()
client = createTestClient()
tableName = `insert_test_${guid()}`
await createSimpleTable(client, tableName)
})
Expand Down
15 changes: 15 additions & 0 deletions packages/client-common/src/clickhouse_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ export interface InputJSON<T = unknown> {
}

export type InputJSONObjectEachRow<T = unknown> = Record<string, T>

export interface ClickHouseSummary {
read_rows: string
read_bytes: string
written_rows: string
written_bytes: string
total_rows_to_read: string
result_rows: string
result_bytes: string
elapsed_ns: string
}

export interface WithClickHouseSummary {
summary?: ClickHouseSummary
}
15 changes: 7 additions & 8 deletions packages/client-common/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type {
Connection,
ConnectionParams,
ConnInsertResult,
ConnQueryResult,
Logger,
WithClickHouseSummary,
ConnExecResult,
} from '@clickhouse/client-common'
import {
type DataFormat,
Expand Down Expand Up @@ -76,7 +77,7 @@ export interface ClickHouseClientConfigOptions<Stream> {
username?: string
/** The user password. Default: ''. */
password?: string
/** The name of the application using the nodejs client.
/** The name of the application using the JS client.
* Default: empty. */
application?: string
/** Database name to use. Default value: `default`. */
Expand Down Expand Up @@ -124,12 +125,10 @@ export interface ExecParams extends BaseQueryParams {
}

export type CommandParams = ExecParams
export interface CommandResult {
query_id: string
}
export type CommandResult = { query_id: string } & WithClickHouseSummary

export type InsertResult = ConnInsertResult
export type ExecResult<Stream> = ConnQueryResult<Stream>
export type ExecResult<Stream> = ConnExecResult<Stream>
export type PingResult = ConnPingResult

export type InsertValues<Stream, T = unknown> =
Expand Down Expand Up @@ -203,9 +202,9 @@ export class ClickHouseClient<Stream = unknown> {
* If you are interested in the response data, consider using {@link ClickHouseClient.exec}
*/
async command(params: CommandParams): Promise<CommandResult> {
const { stream, query_id } = await this.exec(params)
const { stream, query_id, summary } = await this.exec(params)
await this.closeStream(stream)
return { query_id }
return { query_id, summary }
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/client-common/src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WithClickHouseSummary } from './clickhouse_types'
import type { LogWriter } from './logger'
import type { ClickHouseSettings } from './settings'

Expand Down Expand Up @@ -39,8 +40,9 @@ export interface ConnQueryResult<Stream> extends ConnBaseResult {
query_id: string
}

export type ConnInsertResult = ConnBaseResult
export type ConnExecResult<Stream> = ConnQueryResult<Stream>
export type ConnInsertResult = ConnBaseResult & WithClickHouseSummary
export type ConnExecResult<Stream> = ConnQueryResult<Stream> &
WithClickHouseSummary

export type ConnPingResult =
| {
Expand Down
2 changes: 2 additions & 0 deletions packages/client-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export {
type LogParams,
} from './logger'
export type {
ClickHouseSummary,
WithClickHouseSummary,
ResponseJSON,
InputJSON,
InputJSONObjectEachRow,
Expand Down
70 changes: 70 additions & 0 deletions packages/client-node/__tests__/integration/node_summary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { ClickHouseClient } from '@clickhouse/client-common'
import { createSimpleTable } from '@test/fixtures/simple_table'
import { jsonValues } from '@test/fixtures/test_data'
import { createTestClient, guid } from '@test/utils'
import type Stream from 'stream'

describe('[Node.js] Summary header parsing', () => {
let client: ClickHouseClient<Stream.Readable>
let tableName: string

beforeAll(async () => {
client = createTestClient()
tableName = `summary_test_${guid()}`
await createSimpleTable(client, tableName)
})
afterAll(async () => {
await client.close()
})

it('should provide summary for insert/exec', async () => {
const { summary: insertSummary } = await client.insert({
table: tableName,
values: jsonValues,
format: 'JSONEachRow',
})
expect(insertSummary).toEqual({
read_rows: '5',
read_bytes: jasmine.any(String),
written_rows: '5',
written_bytes: jasmine.any(String),
total_rows_to_read: '0',
result_rows: '5',
result_bytes: jasmine.any(String),
elapsed_ns: jasmine.any(String),
})

const { summary: execSummary } = await client.exec({
query: `INSERT INTO ${tableName} SELECT * FROM ${tableName}`,
})
expect(execSummary).toEqual({
read_rows: '5',
read_bytes: jasmine.any(String),
written_rows: '5',
written_bytes: jasmine.any(String),
total_rows_to_read: '5',
result_rows: '5',
result_bytes: jasmine.any(String),
elapsed_ns: jasmine.any(String),
})
})

it('should provide summary for command', async () => {
const { summary } = await client.command({
query: `INSERT INTO ${tableName} VALUES (144, 'Hello', [2, 4]), (255, 'World', [3, 5])`,
clickhouse_settings: {
wait_end_of_query: 1,
},
})
expect(summary).toEqual({
read_rows: '2',
read_bytes: jasmine.any(String),
written_rows: '2',
written_bytes: jasmine.any(String),
total_rows_to_read: '0',
result_rows: '2',
result_bytes: jasmine.any(String),
elapsed_ns: jasmine.any(String),
})
})
})
Loading
Loading