Repository navigation
Cherry pick #114885 to 26.7: AI functions: Make quotas per query not per block - #115864
Merged
robot-ch-test-poll4 merged 13 commits intoAug 24, 2026
Merged
Conversation
`AIQuotaTracker` was a stack local in `executeImpl`, so every block and every pipeline stream got a fresh allowance and the `ai_function_max_*_per_query` limits scaled with the data instead of bounding the query. Own one tracker on the query `Context` (shared by every AI function call in the query) and make its counters atomic for concurrent pipeline threads. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Move the AI call-count tests into the mock suite and add multi-part (multi-block) tests that a per-block tracker cannot pass: `test_api_call_quota_is_per_query`, `test_api_call_quota_throws_per_query`, `test_input_token_quota_is_per_query`. Merges are stopped so the parts survive to the scan. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Reserve each request slot with an atomic bounded increment (`tryReserveApiCall`) so concurrent pipeline threads cannot overshoot `ai_function_max_api_calls_per_query`. Token limits stay best-effort: usage is known only after the response. Adds a multi-threaded test and updates the setting docs. Co-Authored-By: Claude Opus 4.8 <[email protected]>
- Fix the garbled `ai_function_max_api_calls_per_query` doc string. - Print the observed call count (not the max twice) in the limit-reached exception. - Make `test_api_call_quota_holds_under_concurrency` prove concurrency: assert `peak_threads_usage` exceeds 1 (from `system.query_log`) over a table with enough marks to split the scan across threads. The mock becomes a threaded server with a larger backlog so it serves the concurrent AI calls. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…uery The tracker lives on the per-query-execution `Context`, and distributed execution creates one context per shard/fragment (`makeQueryContext`), so each enforces the limit independently. Narrow the setting docs and the `AIQuotaTracker` comment to say so, instead of claiming a global cap. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Drop the `AIQuotaTracker & quota = *quota_tracker` reference bindings and call through the `shared_ptr` instead. No behavior change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Per review: "reserve an API call" reads oddly; the method counts a call against the request quota. Behavior unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Per review: the counters aren't performance-critical (each is touched once per provider request), so a plain mutex with TSA annotations is simpler than atomics and drops the memory-order noise. Behavior unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The quota settings are read from the query context, so a sub-query SETTINGS override of them is ignored. Note this in the four settings' docs and add a regression test asserting a sub-query cap does not change the query budget (the top-level value governs). Co-Authored-By: Claude Opus 4.8 <[email protected]>
recordApiCall now re-checks the token/sticky quotas under the same lock as the API-call reservation (shared helper quotasExceededLocked), so a call is never started after another thread's response pushed the token budget over - closing the check-then-reserve gap for the token limits. The now-redundant per-attempt checkQuotas before recordApiCall is removed; the top-of-row and top-of-batch checks stay. Co-Authored-By: Claude Opus 4.8 <[email protected]>
AI functions: Make quotas per query not per block
…ypick/26.7/114885 # Conflicts: # src/Core/Settings.cpp # src/Functions/FunctionBaseAI.cpp # src/Functions/aiEmbed.cpp # src/Functions/aiSimilarity.cpp # tests/integration/test_ai_functions/test.py
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.
Original pull-request #114885
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job