-
Notifications
You must be signed in to change notification settings - Fork 30
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
Remove split2, refactor Rows for better performance #108
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
650315e
Remove split2, refactor Rows for better performance
slvrtrn d98426f
Fix tests
slvrtrn dbb7be1
Comment the code that still uses split2
slvrtrn e968dda
Update Node.js 18.x test
slvrtrn 6e2cc59
Remove split2 from the IT and the examples
slvrtrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import type Stream from 'stream' | ||
import { type ClickHouseClient, type ResponseJSON, type Row } from '../../src' | ||
import { createTestClient } from '../utils' | ||
|
||
async function rowsValues(stream: Stream.Readable): Promise<any[]> { | ||
async function rowsValues( | ||
stream: AsyncGenerator<Row, unknown> | ||
): Promise<any[]> { | ||
const result: any[] = [] | ||
for await (const chunk of stream) { | ||
result.push((chunk as Row).json()) | ||
} | ||
return result | ||
} | ||
|
||
async function rowsText(stream: Stream.Readable): Promise<string[]> { | ||
async function rowsText( | ||
stream: AsyncGenerator<Row, unknown> | ||
): Promise<string[]> { | ||
const result: string[] = [] | ||
for await (const chunk of stream) { | ||
result.push((chunk as Row).text()) | ||
|
@@ -47,30 +50,28 @@ describe('select', () => { | |
}) | ||
|
||
describe('consume the response only once', () => { | ||
async function assertAlreadyConsumed$<T>(fn: () => Promise<T>) { | ||
async function assertAlreadyConsumed<T>(fn: () => Promise<T>) { | ||
await expect(fn()).rejects.toMatchObject( | ||
expect.objectContaining({ | ||
message: 'Stream has been already consumed', | ||
}) | ||
) | ||
} | ||
function assertAlreadyConsumed<T>(fn: () => T) { | ||
expect(fn).toThrow( | ||
expect.objectContaining({ | ||
message: 'Stream has been already consumed', | ||
}) | ||
) | ||
} | ||
|
||
it('should consume a JSON response only once', async () => { | ||
const rows = await client.query({ | ||
query: 'SELECT * FROM system.numbers LIMIT 1', | ||
format: 'JSONEachRow', | ||
}) | ||
expect(await rows.json()).toEqual([{ number: '0' }]) | ||
// wrap in a func to avoid changing inner "this" | ||
await assertAlreadyConsumed$(() => rows.json()) | ||
await assertAlreadyConsumed$(() => rows.text()) | ||
await assertAlreadyConsumed(() => rows.stream()) | ||
await assertAlreadyConsumed(() => rows.json()) | ||
await assertAlreadyConsumed(() => rows.text()) | ||
await assertAlreadyConsumed(async () => { | ||
for await (const r of rows.stream()) { | ||
r.text() | ||
} | ||
}) | ||
}) | ||
|
||
it('should consume a text response only once', async () => { | ||
|
@@ -80,9 +81,13 @@ describe('select', () => { | |
}) | ||
expect(await rows.text()).toEqual('0\n') | ||
// wrap in a func to avoid changing inner "this" | ||
await assertAlreadyConsumed$(() => rows.json()) | ||
await assertAlreadyConsumed$(() => rows.text()) | ||
await assertAlreadyConsumed(() => rows.stream()) | ||
await assertAlreadyConsumed(() => rows.json()) | ||
await assertAlreadyConsumed(() => rows.text()) | ||
await assertAlreadyConsumed(async () => { | ||
for await (const r of rows.stream()) { | ||
r.text() | ||
} | ||
}) | ||
}) | ||
|
||
it('should consume a stream response only once', async () => { | ||
|
@@ -96,9 +101,13 @@ describe('select', () => { | |
} | ||
expect(result).toEqual('0') | ||
// wrap in a func to avoid changing inner "this" | ||
await assertAlreadyConsumed$(() => rows.json()) | ||
await assertAlreadyConsumed$(() => rows.text()) | ||
await assertAlreadyConsumed(() => rows.stream()) | ||
await assertAlreadyConsumed(() => rows.json()) | ||
await assertAlreadyConsumed(() => rows.text()) | ||
await assertAlreadyConsumed(async () => { | ||
for await (const r of rows.stream()) { | ||
r.text() | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
|
@@ -328,35 +337,14 @@ describe('select', () => { | |
format: 'JSON', | ||
}) | ||
try { | ||
expect(() => result.stream()).toThrowError( | ||
await expect(async () => result.stream().next()).rejects.toThrowError( | ||
'JSON format is not streamable' | ||
) | ||
} finally { | ||
result.close() | ||
} | ||
}) | ||
|
||
it('can pause response stream', async () => { | ||
const result = await client.query({ | ||
query: 'SELECT number FROM system.numbers LIMIT 10000', | ||
format: 'CSV', | ||
}) | ||
|
||
const stream = result.stream() | ||
|
||
let last = null | ||
let i = 0 | ||
for await (const chunk of stream) { | ||
last = chunk.text() | ||
i++ | ||
if (i % 1000 === 0) { | ||
stream.pause() | ||
setTimeout(() => stream.resume(), 100) | ||
} | ||
} | ||
expect(last).toBe('9999') | ||
}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not required anymore with async iterators. It can be paused (not consumed) on the application level, as it is a lazy evaluation. |
||
describe('text()', () => { | ||
it('returns stream of rows in CSV format', async () => { | ||
const result = await client.query({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
import { createClient } from '@clickhouse/client' | ||
import Path from 'path' | ||
import Fs from 'fs' | ||
import split from 'split2' | ||
|
||
void (async () => { | ||
const client = createClient() | ||
const tableName = 'insert_file_stream_ndjson' | ||
await client.exec({ | ||
query: `DROP TABLE IF EXISTS ${tableName}`, | ||
}) | ||
await client.exec({ | ||
query: ` | ||
CREATE TABLE ${tableName} (id UInt64) | ||
ENGINE MergeTree() | ||
ORDER BY (id) | ||
`, | ||
}) | ||
|
||
// contains id as numbers in JSONCompactEachRow format ["0"]\n["0"]\n... | ||
// see also: NDJSON format | ||
const filename = Path.resolve( | ||
process.cwd(), | ||
'./examples/resources/data.ndjson' | ||
) | ||
|
||
await client.insert({ | ||
table: tableName, | ||
values: Fs.createReadStream(filename).pipe( | ||
split((row: string) => JSON.parse(row)) | ||
), | ||
format: 'JSONCompactEachRow', | ||
}) | ||
|
||
const rows = await client.query({ | ||
query: `SELECT * from ${tableName}`, | ||
format: 'JSONEachRow', | ||
}) | ||
|
||
// or just `rows.text()` / `rows.json()` | ||
// to consume the entire response at once | ||
for await (const row of rows.stream()) { | ||
console.log(row.json()) | ||
} | ||
|
||
await client.close() | ||
})() | ||
// import { createClient } from '@clickhouse/client' | ||
// import Path from 'path' | ||
// import Fs from 'fs' | ||
// import split from 'split2' | ||
// | ||
// void (async () => { | ||
// const client = createClient() | ||
// const tableName = 'insert_file_stream_ndjson' | ||
// await client.exec({ | ||
// query: `DROP TABLE IF EXISTS ${tableName}`, | ||
// }) | ||
// await client.exec({ | ||
// query: ` | ||
// CREATE TABLE ${tableName} (id UInt64) | ||
// ENGINE MergeTree() | ||
// ORDER BY (id) | ||
// `, | ||
// }) | ||
// | ||
// // contains id as numbers in JSONCompactEachRow format ["0"]\n["0"]\n... | ||
// // see also: NDJSON format | ||
// const filename = Path.resolve( | ||
// process.cwd(), | ||
// './examples/resources/data.ndjson' | ||
// ) | ||
// | ||
// await client.insert({ | ||
// table: tableName, | ||
// values: Fs.createReadStream(filename).pipe( | ||
// split((row: string) => JSON.parse(row)) | ||
// ), | ||
// format: 'JSONCompactEachRow', | ||
// }) | ||
// | ||
// const rows = await client.query({ | ||
// query: `SELECT * from ${tableName}`, | ||
// format: 'JSONEachRow', | ||
// }) | ||
// | ||
// // or just `rows.text()` / `rows.json()` | ||
// // to consume the entire response at once | ||
// for await (const row of rows.stream()) { | ||
// console.log(row.json()) | ||
// } | ||
// | ||
// await client.close() | ||
// })() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we don't need this anymore. It can be paused (or not consumed) just on the application level.