edgarrmondragon opened a new issue, #5031: URL: https://github.com/apache/arrow-adbc/issues/5031
### What happened? ### Describe the bug, including details regarding any error messages, version, and platform. SQLite driver fails to re-execute a prepared statement when SQLite is built with `SQLITE_OMIT_AUTORESET`. Executing the same prepared statement a second time fails if the SQLite driver is linked against a SQLite built with [`SQLITE_OMIT_AUTORESET`](https://www.sqlite.org/compile.html#omit_autoreset). Apple's system SQLite (`/usr/lib/libsqlite3.dylib`) is built that way, and CMake picks it up by default on macOS. On my own macOS: ```console $ echo "pragma compile_options;" | /usr/bin/sqlite3 | grep AUTORESET OMIT_AUTORESET ``` This shows up in the Python DB-API, which skips `set_sql_query()` and `prepare()` when the same SQL is executed twice in a row. As a result, `python/adbc_driver_manager/tests/test_dbapi.py::test_query_fetch_py` fails locally on macOS but passes in CI. ## Cause _Base analysis by Claude Code Opus 5.5, text minimally edited by me_ > For a prepared statement, `SqliteStatement::ExecuteQueryImpl(ArrowArrayStream*)` (`c/driver/sqlite/sqlite.cc`) passes the existing `sqlite3_stmt` to the reader without calling `sqlite3_reset()` first. After the first execution, the statement has already returned `SQLITE_DONE`. A default SQLite build resets it automatically on the next `sqlite3_step()`. With `SQLITE_OMIT_AUTORESET`, that step fails instead. The reader then reports `sqlite3_errmsg()`, which still describes the earlier `SQLITE_DONE` ("no more rows available"), so the error message is misleading too. SQLite's documentation[^1] says to call `sqlite3_reset()` before stepping a statement again after `SQLITE_DONE`. > [SQLITE_DONE](https://www.sqlite.org/rescode.html#done) means that the statement has finished executing successfully. sqlite3_step() should not be called again on this virtual machine without first calling [sqlite3_reset()](https://www.sqlite.org/c3ref/reset.html) to reset the virtual machine back to its initial state. ## Proposed fix _Again, by Claude Code Opus 5.5_ ```diff --- a/c/driver/sqlite/sqlite.cc +++ b/c/driver/sqlite/sqlite.cc @@ -1154,6 +1154,8 @@ class SqliteStatement : public driver::Statement<SqliteStatement> { Result<int64_t> ExecuteQueryImpl(ArrowArrayStream* stream) { struct AdbcError error = ADBC_ERROR_INIT; + // Statement may have run to completion already; required with SQLITE_OMIT_AUTORESET + (void)sqlite3_reset(stmt_); UNWRAP_STATUS(BindImpl(false)); const int64_t expected = sqlite3_bind_parameter_count(stmt_); ``` With this patch and the driver linked against `/usr/lib/libsqlite3.dylib`, the MRE prints the expected output and the `adbc_driver_manager` Python test suite passes (including `test_query_fetch_py`). [^1]: https://www.sqlite.org/c3ref/step.html#:~:text=sqlite3_reset ### Stack Trace ```console $ DYLD_LIBRARY_PATH=build/driver/sqlite python mre.py 0 [(1,)] Traceback (most recent call last): File "/Users/dev/apache/arrow-adbc/mre.py", line 7, in <module> cur.execute("SELECT 1") ~~~~~~~~~~~^^^^^^^^^^^^ File "/Users/dev/apache/arrow-adbc/python/adbc_driver_manager/adbc_driver_manager/dbapi.py", line 943, in execute handle, self._rowcount = _blocking_call( ~~~~~~~~~~~~~~^ self._stmt.execute_query, (), {}, self._stmt.cancel ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "adbc_driver_manager/_lib.pyx", line 1968, in adbc_driver_manager._lib._blocking_call_impl File "adbc_driver_manager/_lib.pyx", line 1961, in adbc_driver_manager._lib._blocking_call_impl File "adbc_driver_manager/_lib.pyx", line 1583, in adbc_driver_manager._lib.AdbcStatement.execute_query File "adbc_driver_manager/_lib.pyx", line 262, in adbc_driver_manager._lib.check_error adbc_driver_manager.OperationalError: IO: Failed to step query: no more rows available ``` ### How can we reproduce the bug? Build the SQLite driver on macOS with the default SQLite: ```shell cmake -S c -B build -G Ninja -DADBC_DRIVER_SQLITE=ON cmake --build build --target adbc_driver_sqlite_shared otool -L build/driver/sqlite/libadbc_driver_sqlite.dylib | grep sqlite3 # /usr/lib/libsqlite3.dylib (compatibility version 9.0.0, current version 371.0.0) ``` ```python # mre.py import adbc_driver_manager.dbapi as dbapi with dbapi.connect(driver="adbc_driver_sqlite") as conn: with conn.cursor() as cur: for i in range(2): cur.execute("SELECT 1") print(i, cur.fetchall()) ``` ```console $ DYLD_LIBRARY_PATH=build/driver/sqlite python mre.py 0 [(1,)] Traceback (most recent call last): ... adbc_driver_manager.OperationalError: IO: Failed to step query: no more rows available ``` With the same driver built against Homebrew's SQLite ```shell cmake --fresh -S c -B build -G Ninja -DADBC_DRIVER_SQLITE=ON -DCMAKE_PREFIX_PATH="$(brew --prefix sqlite)" cmake --build build --target adbc_driver_sqlite_shared otool -L build/driver/sqlite/libadbc_driver_sqlite.dylib | grep sqlite3 # /opt/homebrew/opt/sqlite/lib/libsqlite3.dylib (compatibility version 9.0.0, current version 9.6.0) ``` then you get the expected output: ``` 0 [(1,)] 1 [(1,)] ``` ### Environment/Setup - arrow-adbc [89fe37cf2050202685398cb50502e31a4d293306](https://github.com/apache/arrow-adbc/commit/89fe37cf2050202685398cb50502e31a4d293306) - macOS 15.8.3, SDK 26.0, Apple clang - System SQLite 3.43.2 (`OMIT_AUTORESET`); SDK headers report 3.48.0 - Python 3.15.2 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
