Skip to content

perf(statement): use INLINE results for repeated small queries on Real-Time sessions - #460

Closed
vuanhphung wants to merge 1 commit into
perf/rd-h2-errorsfrom
perf/rd-h2-errors-2
Closed

vuanhphung wants to merge 1 commit into
perf/rd-h2-errorsfrom
perf/rd-h2-errors-2

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

What did you change, and why?

Behavior change (Lakehouse Real-Time sessions only): a read-only statement whose SQL text recently returned a small result is now executed with disposition: INLINE instead of INLINE_OR_EXTERNAL_LINKS. The client conf realtime_inline_small_results=false turns this off. It is also accepted through session_conf, so for the Python connector session_configuration={"realtime_inline_small_results": "false"} works.

Problem. For the hybrid INLINE_OR_EXTERNAL_LINKS disposition, a Real-Time warehouse persists every result to cloud storage, small inline results included. It keeps its internal Reyder-to-Reyden HTTP/2 stream open for the whole upload. With a few hundred concurrent statements, this saturates the server's streams:

  • throughput falls to near zero for about a second every 6-10 s;
  • 1-4% of statements fail with h2 protocol error: error reading a body from connection, h2 protocol error: http2 error, or operation was canceled;
  • the server then replays the execution under the same operation id, which fails as execution already exists;
  • some statements hang for up to a minute.

Server tickets: ES-2200519 (the server-side fix is merged, but its rollout is paused), SC-249818, SC-248618, ES-2349637. The same requests sent with INLINE show none of this. INLINE can't be the default, though: results over 25 MiB fail with "Inline byte limit exceeded", or come back silently truncated on servers without the ES-2127499 fix.

When INLINE is used. All of these must hold:

  • the session is a Real-Time session (CreateSession returned x-databricks-rt-session: true);
  • CloudFetch is enabled;
  • the call is a synchronous execute (async submit and metadata are unchanged);
  • the statement's leading verb, after comments and opening parentheses, is SELECT, VALUES, TABLE, SHOW, DESCRIBE, or EXPLAIN, or it is WITH / FROM and no INSERT, MERGE, UPDATE or DELETE keyword appears anywhere in the text;
  • the same SQL text returned at most 4 MiB, without external links, in this session within the last 10 minutes.

The first execution of any SQL text keeps INLINE_OR_EXTERNAL_LINKS. The session remembers hashes of SQL texts only, never parameter values, with at most 1,024 entries.

When a statement is re-run. An INLINE attempt is discarded, and the statement runs exactly once more with INLINE_OR_EXTERNAL_LINKS, in two cases:

  • the server fails it with BAD_REQUEST "Inline byte limit exceeded";
  • the server returns a manifest with truncated: true that the caller's row_limit does not explain.

That SQL text then leaves the small set. A truncated result is never returned. The cost: a remembered-small query whose result suddenly exceeds 25 MiB executes twice, once wasted. Live, a parameterized query that grew from 1k to 1M rows took 1.5 s instead of 0.8 s for that one execution. Standard (non-Real-Time) sessions keep INLINE_OR_EXTERNAL_LINKS. One more behavior change: a session that already uses INLINE because CloudFetch is disabled (the #456 fallback) now gets a DataLoss error for a truncated manifest, instead of silently receiving partial rows. It has no links to re-run with.

Stacked on #456.

How do you know it works?

  • Wiremock tests:
    • a repeated small SELECT switches to INLINE, and DML never does;
    • byte-limit overflow re-runs with links;
    • a truncated manifest re-runs with links;
    • the kill switch keeps the hybrid disposition and is stripped from the server session conf;
    • standard sessions never use INLINE;
    • WITH ... INSERT and Hive multi-insert never use INLINE;
    • a truncated INLINE result on a CloudFetch-disabled session fails instead of returning partial rows.
  • A unit test covers the verb check. fmt and clippy -D warnings pass. cargo test --no-fail-fast passes.
  • Live, against a Medium Real-Time warehouse. Python connector main with use_kernel=True, TPC-H Q6 back to back. Runs were isolated (query history showed no other sessions) and interleaved, client on 8 cores:
runs avg QPS steady QPS peak 5 s QPS query p50 / p99 failed
800 sessions (4 x 200), 120 s, 1.4.0 2 624, 546 630, 541 873, 991 709-942 / 3622-3800 ms 2,068, 3,564
800 sessions, this change 2 922, 961 936, 977 1,079, 1,053 739-790 / 2018 ms 0, 0
200 sessions, 60 s, 1.4.0 2 572, 587 578, 589 754, 762 237-245 / 2384-2784 ms 470, 420
200 sessions, this change 3 713, 679, 658 722, 693, 671 785, 758, 724 259-275 / 616-742 ms 0, 0, 0

With this change no 1-second bucket fell below 480 QPS; on 1.4.0 the series repeatedly dropped to zero. Large results still come back complete: a 600k-row (~100 MB) result twice, and the growing parameterized query above.

This pull request and its description were written by Isaac.

…eal-Time sessions

With the INLINE_OR_EXTERNAL_LINKS disposition, Lakehouse Real-Time persists every
result to cloud storage, even small inline ones, and keeps its internal result stream
open while it does. At a few hundred concurrent statements this stalls the warehouse
and fails statements with "h2 protocol error" / "execution already exists".

On Real-Time sessions, a read-only statement now requests INLINE once the same SQL
text has returned a small result (<= 4 MiB) in the last 10 minutes. If the result
outgrows the inline limit (BAD_REQUEST or a truncated manifest), the statement runs
again once with INLINE_OR_EXTERNAL_LINKS. The client conf
realtime_inline_small_results=false turns this off.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: 2 Medium · 1 Nit

The adaptive-INLINE design is sound and well-tested. I found two medium issues: the read-only verb check lets DML that starts with WITH/FROM through, and the new truncated-manifest check also changes behavior for non-Real-Time sessions that use the CloudFetch-disabled INLINE fallback. There is also one low note: the re-run gets a fresh full timeout instead of the remaining budget.

Other findings

  • ⚪ Nit — The re-run after an INLINE overflow is given the same full timeout as the first attempt. The first attempt has already used up part of the budget, so a sync execute with a client query timeout can now take up to about 2× the configured timeout before it returns. Consider passing the remaining budget (deadline minus elapsed) to the re-run so the caller's timeout stays an upper bound.

Comment thread src/client/sea.rs
.to_ascii_uppercase();
matches!(
verb.as_str(),
"SELECT" | "WITH" | "VALUES" | "TABLE" | "FROM" | "SHOW" | "DESCRIBE" | "DESC" | "EXPLAIN"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium — WITH and FROM don't always mean a read-only statement in Spark/Databricks SQL. WITH cte AS (...) INSERT INTO t SELECT * FROM cte is valid, and so is the Hive-style multi-insert FROM src INSERT INTO t1 SELECT ... INSERT INTO t2 SELECT .... Both pass is_read_only_query, so the doc comment's claim that re-running is side-effect free doesn't hold for them.

Today a DML result (affected-row counts) is tiny, so the overflow/truncation re-run is unlikely to fire. But the overflow re-run path in execute_statement depends on this check to be safe, and any server-side truncation or byte-limit error on such a statement would execute the insert twice. Two fixes would work: look past a leading WITH/FROM for an INSERT/MERGE/UPDATE/DELETE keyword, or drop FROM (which is rare as a leading verb for reads) and document the WITH caveat. Also add a negative case like WITH t AS (SELECT 1) INSERT INTO x SELECT * FROM t to read_only_query_detection_skips_comments_and_parentheses.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 023b734. WITH and FROM now count as read-only only when no INSERT, MERGE, UPDATE or DELETE token appears anywhere in the text. A false negative only skips the optimization. The unit test has new negative cases: WITH t AS (SELECT 1) INSERT INTO x ..., WITH ... MERGE ... UPDATE SET *, and the Hive multi-insert FROM src INSERT INTO t1 ... INSERT INTO t2 .... It also has a positive case where a column is named updated_at.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I couldn't find the change described for 023b734 in the current head. is_read_only_query (line 115) still ends in the same matches! on the leading verb, with "WITH" and "FROM" accepted unconditionally. Grepping src/client/sea.rs finds no INSERT/MERGE/UPDATE/DELETE token scan inside that function. The test has no WITH t AS (SELECT 1) INSERT INTO x ... case, no Hive multi-insert case and no updated_at case either; the only WITH case is the read-only WITH t AS (SELECT 1) SELECT * FROM t. The commit may not have been pushed to this branch, or it may have landed somewhere else. Once it's on the PR head, I'll re-check it. The approach you described (treat WITH/FROM as read-only only when no DML keyword appears, and accept the occasional false negative) addresses the concern.

Comment thread src/client/sea.rs
return Err(error);
}
};
if inline

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium — The new truncated-manifest check runs whenever inline is true, and inline also covers the existing inline_disposition fallback (CloudFetch disabled, warehouse rejected can_cloud_download). That fallback applies to standard sessions too, and to metadata calls that go through execute_and_read.

Before this PR, those sessions returned the inline result as-is when manifest.truncated was set. Now they get a hard data_loss error and the statement is closed. No retry happens for them, because only execute_statement retries and only when prefers_inline was true. This contradicts the PR description's "Standard (non-Real-Time) sessions are unchanged", and no test covers it. If the change is intended (refusing silently truncated data is arguably better), call it out in the description and add a wiremock test for the CloudFetch-disabled path. If not, apply the check only to the adaptive-inline attempt (pass the caller's inline flag separately from the effective disposition).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Kept on purpose: with CloudFetch disabled there are no links to re-run with, and returning a server-truncated result as complete is the data-loss case ES-2127499 describes. In 023b734 the PR description calls this out as a behavior change for the #456 fallback, and the new wiremock test cloudfetch_disabled_inline_fallback_rejects_truncated_result pins it. A row_limit that explains the truncation still passes through unchanged.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Rejecting server-truncated inline results when CloudFetch is disabled seems like a reasonable call, and I'm fine with keeping the behavior change. What I can't confirm is the follow-through. In the checked-out head, nothing matches cloudfetch_disabled_inline_fallback_rejects_truncated_result, and the only references to inline_result_truncated are its definition (line 154) and the call at line 1147 in src/client/sea.rs. No test exercises the CloudFetch-disabled path. Commit 023b734 may not be in this checkout yet, or the test may not have been pushed. Once the test and the description note are on the branch, I'm happy to close this out.

@vuanhphung

Copy link
Copy Markdown
Collaborator Author

Closing: the INLINE_OR_EXTERNAL_LINKS high-concurrency gap is being fixed server-side (ES-2200519 / SC-249818), so the kernel keeps using INLINE_OR_EXTERNAL_LINKS for Real-Time sessions.

@vuanhphung vuanhphung closed this Nov 13, 2026
@vuanhphung

Copy link
Copy Markdown
Collaborator Author

On the Nit about the re-run timeout: the re-run shares the caller's budget. ExecutionTimeout::Deadline holds an absolute Instant ("Apply one absolute client deadline", src/client/transport.rs:45), and execute_and_read_with waits with tokio::time::timeout_at(deadline, ...), so a re-run started late gets only the remaining time. The client query timeout stays an upper bound. Default / Unlimited carry no caller deadline: Default is the transport's own 600 s polling ceiling per attempt, unchanged from before.

This branch was previously deployed

1 inactive deployment
azure-prod — 45d15aba Deployed Nov 13, 2026 by oss-reviewer-bot[bot] via followup #2766
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant