Repository navigation
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
Open
bjferguson wants to merge 1 commit into
bjferguson wants to merge 1 commit into
Conversation
…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
force-pushed
the
fix/long-data-block-fetch-alloc
branch
from
November 24, 2026 18:48
2206a86 to
2f2a967
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 reportSQL_GD_BLOCK. No error reaches JavaScript. We hit it with the IBM i Access ODBC Driver on Windows:bind_buffers()readsSQL_LONGVARCHAR,SQL_WLONGVARCHARandSQL_LONGVARBINARYwithSQLGetDataonly whenfetch_size == 1or the driver hasSQL_GD_BLOCK. Otherwise it binds them withSQLBindColand a buffer ofColumnSize * MAX_UTF8_BYTES * fetch_size. IBM i Access describesIFS_READ_UTF8'sCLOB(2G)column with aColumnSizeof 2147483647, sofetchSize: 100asks for about 859 GB.new[]throwsstd::bad_allocinsideAsyncWorker::Execute(), and since the addon is built withNAPI_DISABLE_CPP_EXCEPTIONS,std::terminate()aborts the process.#490 changes the
SQL_GD_BLOCKbranch offetch_and_store(); a driver withoutSQL_GD_BLOCKnever takes it.Fix
bind_buffers(): whenfetch_size > 1and the driver lacksSQL_GD_BLOCK, check each column'sSQL_DESC_CONCISE_TYPEfirst. If one is a LONG type, setSQL_ATTR_ROW_ARRAY_SIZEback to 1 and set a newStatementData::single_row_fetch_for_long_dataflag. The existingfetch_size == 1branches then read those columns withSQLGetData. Result sets without a LONG column, and drivers withSQL_GD_BLOCK, are unchanged.fetch()(FetchAsyncWorker): when the flag is set, loop single-rowfetch_and_store()calls untilfetchSizerows are stored, the end is reached or an error occurs, so callers still getfetchSizerows per call andnoDatabehaves as before.new (std::nothrow), and a NULL buffer makesbind_buffers()returnSQL_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.data->columns[i]is stored right after the column is created, anddeleteColumns()skips columns that were never created, so an early return frombind_buffers()no longer leaves a NULL that cleanup dereferences.set_fetch_size()resets the flag, so a re-executed statement starts clean.test/cursor/fetch.test.jsgets an IBM i-only test that reads aCLOB(1G)table through a cursor withfetchSize: 3.Trade-offs
SQL_GD_BLOCK, a result set with any LONG column now costs oneSQLFetchper 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.prepare_for_fetch(). I'm happy to add that if you prefer.Testing
What we did:
npm run build, with no compiler warnings.clang-format-20 --Werror --dry-run src/*passes (clang-format 20.1.8).connection.query(..., { cursor: true, fetchSize: 100 })overQSYS2.IFS_READ_UTF8on a file of more than 250 lines:What we did not do:
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.Statement.execute({ cursor: true })path directly. It goes through the samebind_buffers()andFetchAsyncWorkercode asconnection.query.SQL_GD_BLOCK. That path doesn't run the new code, so its behavior shouldn't change.SQLColAttributenow 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:
ColumnSize == 0workaround marks as long data (SQL_VARCHAR,SQL_WVARCHAR,SQL_VARBINARY) are not detected by the new type check.set_fetch_size()reallocatesrow_status_arrayon every execute without freeing the old one.🤖 Generated with Claude Code