Skip to content
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/connection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ jobs:
- run: npm run test:v3 -- test/v3/connection/

parity:
runs-on: ubuntu-latest
# The pinned clickhouse-js client-1.23.0 test dependency simdjson@0.9.2
# fails to build with GCC 15 on ubuntu-26.04 (ubuntu-latest as of Nov 2026).
# Keep parity on the last known-good image until upstream updates it.
runs-on: ubuntu-24.04
env:
TZ: UTC
steps:
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ function version() {

// Arrow C Data Interface — low-level binding. The high-level helper lives in
// Layer 3 (`src/layer3/execute/arrow-input.ts`); this module just routes the
// call to the native addon. The connection arg is a `Session._handle` (the
// call to the native addon. The connection arg is `Session.connection` (the
// External returned by CreateConnection) or null for the process-wide default.
function _arrowRegisterColumns(connection, tableName, columns) {
return chdbNode.ArrowRegisterColumns(connection ?? null, tableName, columns);
Expand Down
27 changes: 22 additions & 5 deletions src/layer3/execute/arrow-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* handle for each query.
*/

import { runtime, type Runtime, type RuntimeSession } from '../runtime'
import { runtime, type Runtime } from '../runtime'
import { ChdbCompileError } from '../../errors'

/**
Expand All @@ -38,11 +38,15 @@ import { ChdbCompileError } from '../../errors'
const arrowFinalizers = new FinalizationRegistry<{
rt: Runtime
conn: unknown
session?: { readonly connection: unknown }
name: string
state: { live: boolean }
}>(({ rt, conn, name, state }) => {
}>(({ rt, conn, session, name, state }) => {
if (!state.live) return
state.live = false
// Session.close() releases the native connection and its Arrow registrations.
// The External captured at registration is no longer safe to pass to native.
if (session !== undefined && session.connection !== conn) return
try {
rt._arrowUnregister(conn, name)
} catch {
Expand Down Expand Up @@ -167,21 +171,34 @@ function encodeColumn(col: ArrowColumnInput): NativeArrowColumn {
export function registerArrowTable(
name: string,
columns: ReadonlyArray<ArrowColumnInput>,
opts: { session?: RuntimeSession } = {},
opts: { session?: { readonly connection: unknown } } = {},
): ArrowTableHandle {
validate(name, columns)
const encoded = columns.map(encodeColumn)
const conn = opts.session !== undefined ? (opts.session as { _handle?: unknown })._handle : null
const session = opts.session
const conn = session !== undefined ? session.connection : null
if (session !== undefined && conn == null) {
throw new ChdbCompileError('registerArrowTable: session has no active connection')
}
const rt = runtime()
const sessionStillOwnsConnection = (): boolean => session === undefined || session.connection === conn
// Shared between close/refresh and the GC finalizer so all three agree on
// whether the table is currently registered.
const state = { live: false }
const register = (): void => {
if (!sessionStillOwnsConnection()) {
throw new ChdbCompileError('registerArrowTable: session has no active connection')
}
rt._arrowRegisterColumns(conn, name, encoded)
state.live = true
}
const unregister = (): void => {
if (!state.live) return
if (!sessionStillOwnsConnection()) {
// Closing the Session has already dropped its native Arrow registrations.
state.live = false
return
}
rt._arrowUnregister(conn, name)
state.live = false
}
Expand All @@ -200,6 +217,6 @@ export function registerArrowTable(
}
// Register the GC fallback. The handle is also the unregister token so
// `close()` can cancel it.
arrowFinalizers.register(handle, { rt, conn, name, state }, handle)
arrowFinalizers.register(handle, { rt, conn, session, name, state }, handle)
return handle
}
45 changes: 44 additions & 1 deletion test/v3/layer3/arrow-input.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import { describe, it, expect } from 'vitest'
import { registerArrowTable, selectFrom, chTable, ChdbCompileError } from '../../../index.js'
import { registerArrowTable, selectFrom, chTable, ChdbCompileError, Session } from '../../../index.js'

// Arrow C Data Interface input — register a JS columnar dataset as
// `arrowstream('<name>')` and read it through the regular builder. Single-pass
// semantics: each query consumes the stream, so `refresh()` must run between
// repeated reads against the same table.

describe('registerArrowTable — round-trips JS data through the engine', () => {
it('registers and unregisters on the supplied Session connection', async () => {
const session = new Session()
try {
const t = registerArrowTable('arr_session', [
{ name: 'x', type: 'Int32', data: new Int32Array([7, 8]) },
], { session })
try {
const result = await session.queryAsync(
"SELECT x FROM arrowstream('arr_session') ORDER BY x",
{ format: 'JSONEachRow' },
)
expect(result.text().trim().split('\n').map((line) => JSON.parse(line))).toEqual([{ x: 7 }, { x: 8 }])
} finally {
t.close()
}
await expect(session.queryAsync(
"SELECT x FROM arrowstream('arr_session')",
{ format: 'JSONEachRow' },
)).rejects.toThrow(/arr_session|not found/i)
} finally {
session.close()
}
})

it('rejects a closed Session instead of registering on the default connection', () => {
const session = new Session()
session.close()
expect(() => registerArrowTable('arr_closed_session', [
{ name: 'x', type: 'Int32', data: new Int32Array([1]) },
], { session })).toThrow(/session has no active connection/)
})

it('does not unregister through a Session connection after the Session closes', () => {
const session = new Session()
const t = registerArrowTable('arr_session_closed_first', [
{ name: 'x', type: 'Int32', data: new Int32Array([1]) },
], { session })
session.close()
expect(() => t.refresh()).toThrow(/session has no active connection/)
expect(() => t.close()).not.toThrow()
expect(() => t.close()).not.toThrow()
})

it('reads Int32, Float64, and Utf8 columns in row order', async () => {
const t = registerArrowTable('arr_basics', [
{ name: 'id', type: 'Int32', data: new Int32Array([10, 20, 30]) },
Expand Down
Loading