Repository navigation
Rename CurrentThread::getQueryContext to tryGetQueryContext - #99325
Conversation
|
Workflow [PR], commit [340848d] Summary: ❌
AI Review1) SummaryThis PR primarily renames 2) Missing context (if any)
3) Findings (by severity)Blockers
Majors
No additional inline comments were posted because there are no new non-duplicate findings to report. 4) Tests & Evidence
5) ClickHouse Compliance Checklist (Yes/No + short note)
6) Performance & Safety Notes
7) User-Lens Review
8) Final Verdict
|
b54a16e to
767eb93
Compare
The `try` prefix makes it explicit that the function may return nullptr (e.g. in background threads like S3Queue), following the existing ClickHouse convention for nullable return values. This makes unchecked dereferences like `CurrentThread::tryGetQueryContext()->foo()` immediately suspicious in code review. Also renames `ThreadStatus::getQueryContext` to `tryGetQueryContext` for consistency, since it returns `query_context.lock()` which may be null. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ead` `BuildRuntimeFilterTransform` and `FunctionApplyFilter` were fetching the query context from the thread-local `CurrentThread` at execution time, which is unsafe in executor threads that may lack a query context. Follow the standard ClickHouse pattern: receive the context at construction time and store it as a member. The context is captured during pipeline building (which always runs in a query thread) and passed to the transform constructor. Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Remove `CurrentThread.h` from high-fan-out headers: `ProcessList.h`, `IQueryPlanStep.h`, `DDLWorker.h`, `TCPHandler.h`, `AggregatingTransform.h`, `ParallelParsingInputFormat.h`, `StorageTableFunction.h`, `parseSyslogLevel.h`, `EmbeddedRocksDBBulkSink.h`, `BlockIO.h`, `LocalConnection.h`, `StorageMaterializedView.h`, `HTTPHandler.h` - Extract `QueryScope` from `CurrentThread` into standalone `Common/QueryScope.h` with its own `.cpp`, replacing the nested `CurrentThread::QueryScope` class - Extract `getCurrentThreadGroup` free function into `Common/ThreadGroupSwitcher.h`, replacing `CurrentThread::getGroup()` in template headers: `threadPoolCallbackRunner.h`, `ParallelFormattingOutputFormat.h`, `HashedDictionaryParallelLoader.h`, `UniqExactSet.h`, `HashedDictionary.h`, `StatusRequestsPool.h` - De-inline `MemoryTrackerSwitcher` ctor/dtor into `MemoryTrackerSwitcher.cpp` - De-inline `ResourceGuard::Request::wait` into `ResourceGuard.cpp` - De-inline `ManyAggregatedData` destructor into `AggregatingTransform.cpp` - De-inline `QueryStatus::updateProgressIn/Out` into `ProcessList.cpp` - Add explicit `CurrentThread.h` includes to `.cpp` files that relied on transitive includes through the trimmed headers Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ntext `joinRuntimeFilter.cpp` creates `__applyFilter` with `nullptr` context at query plan optimization time. Fall back to `CurrentThread::tryGetQueryContext` at execution time when the stored context is null. Co-Authored-By: Claude Opus 4.6 <[email protected]>
767eb93 to
90cb994
Compare
- Move `CurrentMetrics` extern declarations from `AggregatingTransform.h` to `AggregatingTransform.cpp` where the destructor now lives - Move `ErrorCodes::RESOURCE_ACCESS_DENIED` from `ResourceGuard.h` to `ResourceGuard.cpp` where `wait` now lives - Revert `FunctionApplyFilter` to not store `ContextPtr` member (style rule: avoid holding `ContextPtr` copies in Functions). Use `CurrentThread::tryGetQueryContext` at execution time instead. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ish` `tryGetQueryContext` can return nullptr. Guard the dereference with an explicit check before calling `getRuntimeFilterLookup`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…lized The move-assignment operator was overwriting `initialized` without running cleanup (logging peak memory, detaching from thread group) if the target was already active. Now mirrors the destructor logic before taking ownership of the source state. Co-Authored-By: Claude Opus 4.6 <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 92.30% (635/688) |
| /// Also some race condition is possible, because collector_thread runs in parallel with | ||
| /// the destruction of the objects already created in this scope. | ||
| collector_thread = ThreadFromGlobalPool([thread_group = CurrentThread::getGroup(), this] | ||
| collector_thread = ThreadFromGlobalPool([thread_group = getCurrentThreadGroup(), this] |
There was a problem hiding this comment.
BTW it will be nice to have some style check for lack of these
There was a problem hiding this comment.
I'll think of something in general as I keep doing more of this work. Now it's part of AI review to keep includes into account, but we should find a better way
39e0335
Adapt feature files to three API changes from the upstream refactor: - Rename `getQueryContext` to `tryGetQueryContext` (on both `CurrentThread` and `ThreadStatus`) - Replace `CurrentThread::QueryScope` with standalone `QueryScope` class and add `#include <Common/QueryScope.h>` - Add missing `#include <Common/CurrentThread.h>` where transitive includes were removed Co-Authored-By: Claude Opus 4.6 <[email protected]>

Follow-up for #99231
Changelog category (leave one):
Description
Renames
CurrentThread::getQueryContexttotryGetQueryContextandThreadStatus::getQueryContexttotryGetQueryContext.The
tryprefix makes it explicit that the function may return nullptr (e.g. in background threads likeS3Queue), following the existing ClickHouse convention for nullable return values. This makes unchecked dereferences likeCurrentThread::tryGetQueryContext()->foo()immediately suspicious in code review.🤖 Generated with Claude Code
Note
Medium Risk
Wide mechanical refactor across server/client/threading code paths; low algorithmic risk but touches query/thread context plumbing and adds hard failures when query context is unexpectedly missing (e.g., runtime filters), so regressions may surface in edge/background-thread scenarios.
Overview
Renames
CurrentThread::getQueryContext()/ThreadStatus::getQueryContext()totryGetQueryContext()and updates call sites to make nullable query-context access explicit.Extracts
CurrentThread::QueryScopeinto a standaloneCommon/QueryScopeclass and switches client/server request handlers,BlockIO, async insert flush, and various internal query execution paths to use it.Reduces header coupling by introducing
ThreadGroupSwitcher.h(getCurrentThreadGroup()), moving some inline implementations to.cpp(MemoryTrackerSwitcher,ResourceGuard::Request::wait,QueryStatus::updateProgress*), and tightening runtime-filter code to require/preserve a query context (passing it intoBuildRuntimeFilterTransform, and throwingLOGICAL_ERRORif absent).Written by Cursor Bugbot for commit 340848d. This will update automatically on new commits. Configure here.
Version info
26.3.1.629