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

Handle errors in a middle of a stream #384

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/client-common/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ interface ClickHouseServerSettings {

/** @see https://clickhouse.com/docs/en/interfaces/http */
interface ClickHouseHTTPSettings {
http_write_exception_in_output_format: Bool
/** Ensures that the entire response is buffered.
* In this case, the data that is not stored in memory will be buffered in a temporary server file.
* This could help prevent errors that might occur during the streaming of SELECT queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
ClickHouseError,
type ClickHouseSettings,
} from '@clickhouse/client-common'
import { createTestClient } from '@test/utils'
import type { StreamableDataFormat } from '../../src'
import type { NodeClickHouseClient } from '../../src/client'

describe('[Node.js] Errors during streaming', () => {
let client: NodeClickHouseClient
beforeAll(() => {
client = createTestClient() as NodeClickHouseClient
})
afterAll(async () => {
await client.close()
})

it('should work with CSV', async () => {
let err: unknown
let rowsCount = 0
try {
const rs = await runQuery('CSV')
const stream = rs.stream()
for await (const rows of stream) {
rowsCount += rows.length
}
} catch (e) {
err = e
}
assertStreamFailure(rowsCount, err)
})

it('should work with JSONEachRow', async () => {
let err: unknown
let rowsCount = 0
try {
const rs = await runQuery('JSONEachRow', {
http_write_exception_in_output_format: 0,
})
const stream = rs.stream()
for await (const rows of stream) {
for (const row of rows) {
row.json() // should not produce SyntaxError
rowsCount++
}
}
} catch (e) {
err = e
}
assertStreamFailure(rowsCount, err)
})

function runQuery<F extends StreamableDataFormat>(
format: F,
settings?: ClickHouseSettings,
) {
return client.query({
query: `
SELECT
number,
if(number > 20, throwIf(1, 'Boom'), number) AS value
FROM numbers(1000)
`,
format,
clickhouse_settings: {
// Forces CH to send more chunks of data (1 row each),
// so that we get 200 OK + headers first instead of 500 ISE
max_block_size: '1',
...(settings ?? {}),
},
})
}

function assertStreamFailure(rowsCount: number, err: unknown) {
// rows count may vary, but the last row will always contain the error message
expect(rowsCount).toBeGreaterThanOrEqual(10)
expect(err).toBeInstanceOf(ClickHouseError)
expect((err as ClickHouseError).code).toBe('395')
expect((err as ClickHouseError).type).toBe(
'FUNCTION_THROW_IF_VALUE_IS_NON_ZERO',
)
}
})
34 changes: 33 additions & 1 deletion packages/client-node/src/result_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import type {
Row,
} from '@clickhouse/client-common'
import {
ClickHouseError,
isNotStreamableJSONFamily,
isStreamableJSONFamily,
parseError,
validateStreamFormat,
} from '@clickhouse/client-common'
import { Buffer } from 'buffer'
Expand Down Expand Up @@ -111,6 +113,7 @@ export class ResultSet<Format extends DataFormat | unknown>
validateStreamFormat(this.format)

let incompleteChunks: Buffer[] = []
let errorRowText: string | undefined
const logError = this.log_error
const toRows = new Transform({
transform(
Expand Down Expand Up @@ -156,7 +159,22 @@ export class ResultSet<Format extends DataFormat | unknown>
} else {
// to be processed during the first pass for the next chunk
incompleteChunks.push(chunk.subarray(lastIdx))
this.push(rows)
// error reporting goes like this:
// __exception__\r\n // - the row before the last one
// Code: X. DB::Exception: ...\n // - the very last row
// we are not going to push these rows downstream
if (
rows.length > 1 &&
rows[rows.length - 2].text === errorHeaderMessage
) {
errorRowText = rows[rows.length - 1].text
// push the remaining rows before the error
if (rows.length > 2) {
this.push(rows.slice(0, -2))
}
} else {
this.push(rows)
}
}
lastIdx = idx + 1 // skipping newline character
} while (idx !== -1)
Expand All @@ -165,6 +183,19 @@ export class ResultSet<Format extends DataFormat | unknown>
}
callback()
},
// will be triggered if ClickHouse terminates the connection with an error while streaming
Copy link
Member

Choose a reason for hiding this comment

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

Triggered by whom?

destroy(err: Error | null, callback: (error?: Error | null) => void) {
if (errorRowText !== undefined) {
const maybeLastRowErr = parseError(errorRowText)
if (maybeLastRowErr instanceof ClickHouseError) {
callback(maybeLastRowErr)
}
} else if (err !== null) {
callback(err)
} else {
callback()
}
},
autoDestroy: true,
objectMode: true,
})
Expand Down Expand Up @@ -203,3 +234,4 @@ export class ResultSet<Format extends DataFormat | unknown>

const streamAlreadyConsumedMessage = 'Stream has been already consumed'
const resultSetClosedMessage = 'ResultSet has been closed'
const errorHeaderMessage = `__exception__\r`
Loading