Fix optimizer crash in JDBC by ensuring active_query is set during prepare#551
Closed
thijs-s wants to merge 2 commits intoduckdb:mainfrom
Closed
Fix optimizer crash in JDBC by ensuring active_query is set during prepare#551thijs-s wants to merge 2 commits intoduckdb:mainfrom
thijs-s wants to merge 2 commits intoduckdb:mainfrom
Conversation
Initialize `active_query` in `ClientContext::PrepareInternal` to ensure the optimizer has a valid context during statement preparation. This fixes a null pointer dereference encountered by optimizer extensions when accessing `GetCurrentQuery()` via the JDBC driver. The implementation uses a new `ActiveQueryGuard` RAII helper to ensure strict exception safety and proper resource cleanup. Added regression tests in `TestOptimizerCrash.java`.
Collaborator
|
Hi, thanks for the PR! Please note, that sources in |
Author
|
thanks for the info! Done. duckdb/duckdb#20755 |
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.
This PR fixes a crash/NPE encountered when using optimizer extensions with the JDBC driver.
Issue
When
Connection.prepareStatement()is called,ClientContext::PrepareInternalis executed. Previously,active_querywas not set in theClientContextduring this phase. If an optimizer extension (hooked intoPreparedStatementcreation) attempted to accesscontext.GetCurrentQuery(), it would dereference a null pointer (or hit an assertion in debug builds), causing a crash.Solution
The
ClientContextrequires an initializedactive_querystate for the optimizer to function correctly (e.g., to access the query string). This state was previously missing during the JDBC prepare phase (PrepareInternal), which caused the crash.We now correctly initialize
active_querywithinPrepareInternalusing a new RAII helper structActiveQueryGuard. This ensures the context is fully populated for the duration of the preparation, satisfying the requirements of optimizer extensions.ActiveQueryGuardsetsactive_queryupon initialization.active_queryis reset tonullptrwhen the scope exits, preservingClientContextinvariants.