Skip to content

Report exceptions thrown by aggregate and window steps - #16

Merged
marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:report-aggregate-step-errors
Nov 12, 2026
Merged

marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:report-aggregate-step-errors

Conversation

@davidschachterADFA

Copy link
Copy Markdown
Contributor

The step function of an aggregate or window function, and the inverse function of a window function, return nothing, so js_execute_common() drops their result, and with it any exception they throw. The aggregate then goes on as if the row had been added or removed: a step that throws, or runs out of memory, gives a wrong result with no error.

This reports an exception thrown by a step or inverse function as an SQL error. Reported from xStep or xInverse, the error makes SQLite abort the statement; SQLite still calls xFinal, so the aggregate context is released as before.

Test: a new Aggregate errors test checks that an exception in an aggregate step, and one in a window inverse function, each fail the statement with its message, and that the connection stays usable. The final memory check still finds no leak. make test passes on x86_64 Linux, also under ASan and UBSan.

These are six separate pull requests, #11 to #16, so each can be taken or declined on its own; only #15 depends on another (#14). They add tests at the same places in test/main.c, and #11 and #12 both add code just above globaljs_init(), so those merged later will need a small rebase, which we will do.

This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

js_execute_common() turned the result of a function into an SQL result
only when the caller wanted the value. The step function of an aggregate
or window function, and the inverse function of a window function,
return nothing, so their result was dropped, and with it any exception
they threw: the aggregate went on as if the row had been added or
removed. A step that threw, or ran out of memory, gave a wrong result
with no error.

Report an exception thrown by a step or inverse function as an SQL error.
Reported from xStep or xInverse, it makes SQLite abort the statement,
which still calls xFinal, so the aggregate context is released as before.

Add an Aggregate errors test: an exception in an aggregate step and in a
window inverse function each fail the statement with its message, and the
connection stays usable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
@marcobambini

Copy link
Copy Markdown
Member

Merged into main as part of a review of #11–#19 together.

Verified locally on macOS/arm64: universal build with no new warnings, full suite green, clean under ASan + UBSan, and the final memory check still reports no leak — which is the thing I most wanted to confirm, since erroring from xStep means SQLite aborts the statement while still calling xFinal.

The new Aggregate errors test is load-bearing: with the three-line change reverted, SELECT step_fails(x) FROM t; fails the test at once.

Worth noting that this fixes a second, quieter bug as a side effect: before this, an exception from a step or inverse function was never retrieved, so JS_FreeValue on JS_EXCEPTION was a no-op and the exception stayed pending on the context, leaking into the next call. js_error_to_sqlite calls JS_GetException, which clears it.

Nothing needed changing. Three lines, a real bug, a test that proves it — thanks.

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.

2 participants