From e6470bea592aae8915b592dc7a9dd57593cc9a09 Mon Sep 17 00:00:00 2001 From: Shawn Chen Date: Tue, 10 Nov 2026 13:40:32 +1300 Subject: [PATCH 1/3] Fix Arrow table registration with a Session --- index.js | 2 +- src/layer3/execute/arrow-input.ts | 9 +++++--- test/v3/layer3/arrow-input.test.ts | 34 +++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index ef94836..fd7a6a8 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/src/layer3/execute/arrow-input.ts b/src/layer3/execute/arrow-input.ts index 760725b..f2dcc2d 100644 --- a/src/layer3/execute/arrow-input.ts +++ b/src/layer3/execute/arrow-input.ts @@ -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' /** @@ -167,11 +167,14 @@ function encodeColumn(col: ArrowColumnInput): NativeArrowColumn { export function registerArrowTable( name: string, columns: ReadonlyArray, - 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 conn = opts.session !== undefined ? opts.session.connection : null + if (opts.session !== undefined && conn == null) { + throw new ChdbCompileError('registerArrowTable: session has no active connection') + } const rt = runtime() // Shared between close/refresh and the GC finalizer so all three agree on // whether the table is currently registered. diff --git a/test/v3/layer3/arrow-input.test.ts b/test/v3/layer3/arrow-input.test.ts index 3679af4..aadd113 100644 --- a/test/v3/layer3/arrow-input.test.ts +++ b/test/v3/layer3/arrow-input.test.ts @@ -1,5 +1,5 @@ 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('')` and read it through the regular builder. Single-pass @@ -7,6 +7,38 @@ import { registerArrowTable, selectFrom, chTable, ChdbCompileError } from '../.. // 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('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]) }, From 3a779431dc2b62241b7cf960c1ac25e3f0b8c517 Mon Sep 17 00:00:00 2001 From: Shawn Chen Date: Tue, 10 Nov 2026 14:55:59 +1300 Subject: [PATCH 2/3] Pin parity CI to Ubuntu 24.04 for simdjson build --- .github/workflows/connection.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/connection.yml b/.github/workflows/connection.yml index 1cc7258..3eac158 100644 --- a/.github/workflows/connection.yml +++ b/.github/workflows/connection.yml @@ -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: From 9b59ba8f379b56949c80953df8d2c171cb75e806 Mon Sep 17 00:00:00 2001 From: Shawn Chen Date: Tue, 10 Nov 2026 17:27:17 +1300 Subject: [PATCH 3/3] Avoid stale Arrow unregister after Session close --- src/layer3/execute/arrow-input.ts | 22 ++++++++++++++++++---- test/v3/layer3/arrow-input.test.ts | 11 +++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/layer3/execute/arrow-input.ts b/src/layer3/execute/arrow-input.ts index f2dcc2d..84bc703 100644 --- a/src/layer3/execute/arrow-input.ts +++ b/src/layer3/execute/arrow-input.ts @@ -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 { @@ -171,20 +175,30 @@ export function registerArrowTable( ): ArrowTableHandle { validate(name, columns) const encoded = columns.map(encodeColumn) - const conn = opts.session !== undefined ? opts.session.connection : null - if (opts.session !== undefined && conn == 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 } @@ -203,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 } diff --git a/test/v3/layer3/arrow-input.test.ts b/test/v3/layer3/arrow-input.test.ts index aadd113..28dc8d1 100644 --- a/test/v3/layer3/arrow-input.test.ts +++ b/test/v3/layer3/arrow-input.test.ts @@ -39,6 +39,17 @@ describe('registerArrowTable — round-trips JS data through the engine', () => ], { 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]) },