Skip to content

Stop AI functions mid-block on KILL QUERY and max_execution_time - #125698

Merged
george-larionov merged 1 commit into
masterfrom
ai-functions/stop-mid-block-on-cancel
Oct 14, 2026
Merged

george-larionov merged 1 commit into
masterfrom
ai-functions/stop-mid-block-on-cancel

Conversation

@george-larionov

@george-larionov george-larionov commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Related: #122484

An AI function sends its provider requests row by row within a block, and nothing checked the query status between requests. KILL QUERY and max_execution_time only took effect at the next block boundary, after every row in the block had its request and retries finish.

AIQuotaTracker now checks the query status in checkQuotas and recordApiCall, i.e. before each request and each retry. Under timeout_overflow_mode = 'break' it throws TIMEOUT_EXCEEDED as well, because a function cannot return fewer rows than it received. The pipeline is already cancelled by the time limit at that point, so the exception is dropped and the query returns a partial result, as usual for break.

Split out of #122484.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

KILL QUERY and max_execution_time (including timeout_overflow_mode = 'break') now stop AI functions in the middle of a block, instead of only after all requests for the block have finished.


Workflow [PR]
Sync PR [sync-upstream/pr/125698]

Version info

  • Merged into: 26.10.1.2414-master (included in 26.10 and later)
  • Backported to: 26.9.26.25, 26.8.35.22

`AIQuotaTracker` checks the query status before each request and retry.
Split out of #122484.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [582e356]

Summary: ✅


AI Review

Summary

The PR checks query cancellation and time limits through the shared AIQuotaTracker before provider requests and retries, allowing AI functions to stop within a block. Request changes: the new break regression test expects success despite the implementation deliberately throwing TIMEOUT_EXCEEDED.

Missing context / blind spots
  • ⚠️ Current-head integration results are pending, and the tests were not run locally. A passing run of the three added cancellation/timeout tests would close this gap.
Findings

⚠️ Majors

  • tests/integration/test_ai_functions/test.py:2481: test_max_execution_time_break_stops_issuing_requests expects a successful query, but when the limit expires mid-block, AIQuotaTracker::throwIfCancelled converts the false return from QueryStatus::checkTimeLimit into TIMEOUT_EXCEEDED. The executor propagates it, so instance.query raises before the request-count assertion. Use instance.query_and_get_error, assert TIMEOUT_EXCEEDED, update the docstring, and retain the request-count bound.
Final Verdict

Status: ⚠️ Request changes. Correct the break test expectation and run the three added cancellation/timeout tests.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 13, 2026
@rschu1ze rschu1ze self-assigned this Oct 13, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Oct 13, 2026
"""With `timeout_overflow_mode = 'break'` the query returns without an error once the time limit is
reached, and no further requests are sent."""
_reset_concurrency()
instance.query(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Expect TIMEOUT_EXCEEDED in the break regression test

Once the two-second limit expires inside this 400-row block, QueryStatus::checkTimeLimit returns false and the new AIQuotaTracker::throwIfCancelled explicitly throws TIMEOUT_EXCEEDED. That check runs outside the provider retry handler, and PipelineExecutor propagates the exception; instance.query therefore raises before the request-count assertion. This test cannot pass on the mid-block timeout path it is meant to cover.

Use instance.query_and_get_error, assert that the error contains TIMEOUT_EXCEEDED, and update the docstring to match the deliberate scalar-function behavior described in the PR. Keep the request-count assertion so the test still proves that no further provider calls are issued.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is correct; it passes in CI and locally. The TIMEOUT_EXCEEDED thrown from AIQuotaTracker::throwIfCancelled is caught in ISimpleTransform::work and pushed to the output port. By then PullingAsyncPipelineExecutor has already cancelled the pipeline with CancelledByTimeout, so the exception is never pulled downstream, and the query finishes as a regular break result. Locally: QueryFinish, exception_code 0, AIAPICalls = 4 of 400.

@george-larionov
george-larionov added this pull request to the merge queue Oct 14, 2026
Merged via the queue into master with commit 79dd612 Oct 14, 2026
176 checks passed
@george-larionov
george-larionov deleted the ai-functions/stop-mid-block-on-cancel branch October 14, 2026 21:42
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 27, 2026
george-larionov added a commit that referenced this pull request Oct 27, 2026
Keep only the `ai_slow` collection from #125698; drop `ai_mock_no_structured_output`, which comes from a master-only change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
george-larionov added a commit that referenced this pull request Oct 27, 2026
Keep only the `ai_slow` collection from #125698; drop `ai_mock_no_structured_output`, which comes from a master-only change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-ch-test-poll2 added a commit that referenced this pull request Oct 27, 2026
Cherry pick #125698 to 26.8: Stop AI functions mid-block on `KILL QUERY` and `max_execution_time`
robot-clickhouse added a commit that referenced this pull request Oct 27, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 27, 2026
Cherry pick #125698 to 26.9: Stop AI functions mid-block on `KILL QUERY` and `max_execution_time`
robot-clickhouse added a commit that referenced this pull request Oct 27, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 27, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 27, 2026
Backport #125698 to 26.9: Stop AI functions mid-block on `KILL QUERY` and `max_execution_time`
clickhouse-gh Bot added a commit that referenced this pull request Oct 27, 2026
Backport #125698 to 26.8: Stop AI functions mid-block on `KILL QUERY` and `max_execution_time`
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
@clickhouse-gh clickhouse-gh Bot added comp-ai-functions AI functions: SQL functions that call a large language model or an embedding model (aiGenerate, a... and removed comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-ai-functions AI functions: SQL functions that call a large language model or an embedding model (aiGenerate, a... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants