Skip to content

fix: fetch LONG columns one row at a time when the driver lacks SQL_GD_BLOCK - #506

Open
bjferguson wants to merge 1 commit into
IBM:mainfrom
bjferguson:fix/long-data-block-fetch-alloc
Open

bjferguson wants to merge 1 commit into
IBM:mainfrom
bjferguson:fix/long-data-block-fetch-alloc

Conversation

@bjferguson

Copy link
Copy Markdown

Problem

A cursor with fetchSize > 1 over a result set that has a LOB column (CLOB, DBCLOB, BLOB) terminates the Node.js process when the driver doesn't report SQL_GD_BLOCK. No error reaches JavaScript. We hit it with the IBM i Access ODBC Driver on Windows:

const cursor = await connection.query(
  "SELECT * FROM TABLE(QSYS2.IFS_READ_UTF8(PATH_NAME => '/some/file.log'))",
  { cursor: true, fetchSize: 100 }
);
await cursor.fetch(); // the process exits with no JS error

bind_buffers() reads SQL_LONGVARCHAR, SQL_WLONGVARCHAR and SQL_LONGVARBINARY with SQLGetData only when fetch_size == 1 or the driver has SQL_GD_BLOCK. Otherwise it binds them with SQLBindCol and a buffer of ColumnSize * MAX_UTF8_BYTES * fetch_size. IBM i Access describes IFS_READ_UTF8's CLOB(2G) column with a ColumnSize of 2147483647, so fetchSize: 100 asks for about 859 GB. new[] throws std::bad_alloc inside AsyncWorker::Execute(), and since the addon is built with NAPI_DISABLE_CPP_EXCEPTIONS, std::terminate() aborts the process.

#490 changes the SQL_GD_BLOCK branch of fetch_and_store(); a driver without SQL_GD_BLOCK never takes it.

Fix

  • bind_buffers(): when fetch_size > 1 and the driver lacks SQL_GD_BLOCK, check each column's SQL_DESC_CONCISE_TYPE first. If one is a LONG type, set SQL_ATTR_ROW_ARRAY_SIZE back to 1 and set a new StatementData::single_row_fetch_for_long_data flag. The existing fetch_size == 1 branches then read those columns with SQLGetData. Result sets without a LONG column, and drivers with SQL_GD_BLOCK, are unchanged.
  • Cursor fetch() (FetchAsyncWorker): when the flag is set, loop single-row fetch_and_store() calls until fetchSize rows are stored, the end is reached or an error occurs, so callers still get fetchSize rows per call and noData behaves as before.
  • Allocations: the bound column buffers are allocated with new (std::nothrow), and a NULL buffer makes bind_buffers() return SQL_ERROR, so an allocation that is still too large (a non-LONG column described with a huge size) rejects the query instead of aborting the process.
  • Cleanup: data->columns[i] is stored right after the column is created, and deleteColumns() skips columns that were never created, so an early return from bind_buffers() no longer leaves a NULL that cleanup dereferences.
  • set_fetch_size() resets the flag, so a re-executed statement starts clean.
  • Test: test/cursor/fetch.test.js gets an IBM i-only test that reads a CLOB(1G) table through a cursor with fetchSize: 3.

Trade-offs

  • On a driver without SQL_GD_BLOCK, a result set with any LONG column now costs one SQLFetch per row instead of one per rowset, even when the LONG column is small enough that block binding used to work. The results are the same. The driver's own block fetching from the server is unaffected.
  • A buffer allocation failure reports the generic "Error preparing for fetch" with no ODBC diagnostics. A specific message would mean changing each caller of prepare_for_fetch(). I'm happy to add that if you prefer.

Testing

What we did:

  • Built on Windows 11 with Node 22 and MSVC 14.44 (VS 2022 Build Tools) via npm run build, with no compiler warnings.
  • clang-format-20 --Werror --dry-run src/* passes (clang-format 20.1.8).
  • Against IBM i, through the IBM i Access ODBC Driver, using connection.query(..., { cursor: true, fetchSize: 100 }) over QSYS2.IFS_READ_UTF8 on a file of more than 250 lines:
    • 2.6.0 from npm: the process exits during the fetch with no error.
    • This branch: the fetches return 100, 100 and 50 rows with the expected content.

What we did not do:

  • We did not run the mocha suite (npm test), including the new test. We didn't want to run the full suite's DDL against our production system. The new test has been syntax-checked but not executed.
  • We did not build on Linux or macOS (left to CI).
  • We did not test the Statement.execute({ cursor: true }) path directly. It goes through the same bind_buffers() and FetchAsyncWorker code as connection.query.
  • We did not check a driver that reports SQL_GD_BLOCK. That path doesn't run the new code, so its behavior shouldn't change.
  • The live run used the patch before two small follow-ups. A failed SQLColAttribute now leaves the column to the old binding code instead of failing the query, and the code was reformatted with clang-format. The behavior on IBM i is the same.

Not addressed here

These were already present and are left alone:

  • Columns that the ColumnSize == 0 workaround marks as long data (SQL_VARCHAR, SQL_WVARCHAR, SQL_VARBINARY) are not detected by the new type check.
  • set_fetch_size() reallocates row_status_array on every execute without freeing the old one.

🤖 Generated with Claude Code

…D_BLOCK

A cursor with fetchSize > 1 over a CLOB/DBCLOB/BLOB column, on a driver
that doesn't report SQL_GD_BLOCK (such as IBM i Access ODBC), bound the
column with a buffer of ColumnSize * fetchSize. A CLOB(2G) made that an
allocation of hundreds of GB, and the std::bad_alloc aborted the process,
since the addon is built without C++ exceptions.

bind_buffers() now lowers the row array size to 1 when such a column is
present, so the column is read with SQLGetData, and the cursor fetch
loops single-row fetches to still return fetchSize rows. Column buffers
are allocated with nothrow, so an allocation that fails rejects the
query instead of terminating the process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Benjamin Ferguson <ben.ferguson@duncanaviation.com>
@bjferguson
bjferguson force-pushed the fix/long-data-block-fetch-alloc branch from 2206a86 to 2f2a967 Compare November 24, 2026 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant