Skip to content

Fixes #33918: [Snowflake] Bound the stored-procedure history scan and isolate its failures - #33919

Merged
mohittilala merged 3 commits into
mainfrom
fix/snowflake-sp-lineage-timeout
Sep 30, 2026
Merged

mohittilala merged 3 commits into
mainfrom
fix/snowflake-sp-lineage-timeout

Conversation

@mohittilala

@mohittilala mohittilala commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #33918

Snowflake lineage ingestion fails the entire run with 000604 (57014): SQL execution was cancelled by the client due to a timeout when stored-procedure lineage runs over a long queryLogDuration. Three things combine.

The history query is unbounded. The stored-procedure history query reads ACCOUNT_USAGE.QUERY_HISTORY twice, joins the two halves by session and time overlap, and sorts the whole join. Each half was bounded only by a start time, so a 365-day lookback scanned a year of history twice in one statement. Both halves are now bounded at both ends, and a statement that Snowflake cancels is retried over the two halves of its window, recursively, a capped number of times and never below a one-day window. Only a cancel is retried, identified by the server's SQLSTATE 57014, which Snowflake reports both for its own statement timeout (000630) and for a client cancel (000604). A permission or compilation error would fail identically on every half, so it is reported once instead. The child-query half reaches seven days past its window, which is Snowflake's hard ceiling on STATEMENT_TIMEOUT_IN_SECONDS, so a CALL that starts just before the window closes keeps every query it ran after the boundary on any account. The global sort is gone: nothing downstream depends on the order, since the rows are handed to a thread pool.

Our own connect argument cancelled it. #28131 set network_timeout=600 on the Snowflake connection to bound a severed socket, but the driver also arms network_timeout as a client-side cancel timer on every statement it executes, which made it a hard ten-minute cap on every Snowflake statement. The connection no longer sets any client timeout. A severed socket is still bounded, because the driver applies its own 60-second read timeout to every request when socket_timeout is unset. Anyone who wants a statement deadline can still pass network_timeout or socket_timeout in connectionArguments.

One failed statement failed the run. The stored-procedure history read had no error handling, so the exception escaped the lineage producer and the step aborted as Unhandled. Stored-procedure lineage runs before query lineage, so a cancelled history read also cost the run its query lineage. A read that fails and cannot be narrowed is now recorded against the pipeline and skipped, and the rest of the run continues.

Type of change:

  • Bug fix

High-level design:

The shared stored-procedure lineage mixin gains two extension points, following the existing per-database engine hook. One yields the statements to read history with and defaults to the connector's single existing statement. The other yields replacement statements for one that failed and defaults to none. The mixin retries a failed statement over whatever replacements the connector offers and reports the failure once there are none. Every other connector keeps the defaults, so its only change is that a failed history read no longer aborts the run.

The Snowflake source reads the whole window in one statement and, on a cancel, offers the two halves of the failed statement's window.

Splitting only on a cancel, rather than up front, is deliberate. Fixed chunking is what the ACCESS_HISTORY path does (#28329) and was the first thing tried here. Measured against a real account, reading a 365-day window in fixed 2-day chunks returned byte-identical rows but took 1737s across 183 statements against 29s for the single statement, because every ACCOUNT_USAGE statement carries a large fixed cost however little it scans. Splitting on demand leaves accounts that can read their window in one go exactly as fast as they are today.

Tests:

Use cases covered

  • A long lookback is read as one bounded statement, not an unbounded scan
  • A cancelled history statement is retried over the halves of its window, recursively, until it succeeds
  • Splitting stops at its depth cap and at a one-day window, and neither a rejected statement nor a client-side error without a Snowflake SQLSTATE is retried
  • After a split, the child-query bound still covers the longest CALL Snowflake allows
  • A history read that cannot be recovered is reported against the pipeline and skipped, and the lineage producer completes instead of raising, so query lineage still runs
  • Connectors that read their history in one statement keep doing so
  • The Snowflake connection sets no client timeout by default, and user-supplied timeouts still pass through

Unit tests

  • I added unit tests for the new/changed logic, covering the Snowflake statement bounds and splitting, the shared mixin's retry and failure isolation, and the connection's timeout defaults.

Backend integration tests

  • Not applicable (no backend API changes).

Ingestion integration tests

  • Not applicable: there is no Snowflake testcontainer, so the behavior was exercised against a live account instead (below).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

Against a live Snowflake account:

  1. Reproduced the cancel: a 40-second statement fails with the reported 000604 when network_timeout is 10, while a short statement at the same setting and the same statement at 600 both succeed.
  2. Confirmed Snowflake's own statement timeout surfaces as 000630 with SQLSTATE 57014 and is classified as a cancel, and that STATEMENT_TIMEOUT_IN_SECONDS accepts at most 604800 seconds (604801 is rejected as out of range).
  3. Compared a split window against the unbounded query over the same 365-day window: 1860 rows each way, none missing, none extra.
  4. Forced a real cancel over a 60-day window and ran the real history read: the window is split from 60 days to 30 to 15, and the run returns the same 286 rows the uncancelled control returns, with no reported failures and nothing raised.

UI screen recording / screenshots:

Not applicable.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • I have added a test that covers the exact scenario we are fixing.

RetriggerConfidence Score: 4/5

The PR should not merge until the child-query scan preserves lineage for calls allowed to run longer than two days.

Findings

  1. P1 Long calls lose child lineage ▶
Summary

The PR bounds Snowflake stored-procedure history reads, retries cancelled reads over smaller windows, isolates unrecoverable read failures, and replaces the default statement-wide network timer with a socket timeout.

  • The fixed two-day child-query overlap can omit lineage for longer-running calls when statement timeouts are configured above the default.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read bounded CALL and child-query history] --> B{Statement cancelled?}
  B -- No --> C[Process returned lineage]
  B -- Yes --> D{Split limit reached?}
  D -- No --> E[Retry each half]
  E --> A
  D -- Yes --> F[Record failure and continue]
Loading

Reviews (1) · Last reviewed commit: "Bound the Snowflake stored-procedure his..."

…d one failing the run

The stored-procedure history statement scanned the whole lookback window with no upper bound, and our own network_timeout default armed the driver's per-statement cancel timer, so a long queryLogDuration died with 000604 and took the whole run down with it, query lineage included.

The read is now bounded at both ends and split in half only when Snowflake cancels it, the socket guard moves to socket_timeout, and a read that still fails is reported against the pipeline instead of raising.
@mohittilala
mohittilala requested a review from a team as a code owner September 24, 2026 05:41
@mohittilala
mohittilala requested review from stout-yeoman and a lite review from Copilot September 24, 2026 05:41
@mohittilala mohittilala self-assigned this Sep 24, 2026
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@github-actions github-actions Bot added Ingestion safe to test Add this label to run secure Github workflows on PRs labels Sep 24, 2026

Copilot AI left a comment

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.

Copilot review overview

🔵 Needs a closer look

The changes span recursive retries, timeout semantics, SQL bounds, and failure isolation, warranting final human review.

Review effort: Lite
Findings: None

What changed in this PR

This PR hardens Snowflake stored-procedure lineage ingestion against timeouts and failed history scans.

Changes:

  • Bounds and recursively splits history queries.
  • Uses socket_timeout for connection protection.
  • Isolates failures and adds regression tests.
File Description
ingestion/​tests/​unit/​topology/​database/​test_snowflake_stored_procedure_lineage.py Tests Snowflake query bounds and splitting.
ingestion/​tests/​unit/​source/​database/​snowflake/​test_connection.py Tests timeout configuration.
ingestion/​tests/​unit/​lineage/​test_stored_procedure_lineage.py Tests failure isolation and retries.
ingestion/​src/​metadata/​ingestion/​source/​database/​stored_procedures_mixin.py Adds retry hooks and failure handling.
ingestion/​src/​metadata/​ingestion/​source/​database/​snowflake/​queries.py Bounds scans and removes global sorting.
ingestion/​src/​metadata/​ingestion/​source/​database/​snowflake/​lineage.py Implements recursive window splitting.
ingestion/​src/​metadata/​ingestion/​source/​database/​snowflake/​connection.py Switches the default guard to socket_timeout.
ingestion/​.basedpyright/​baseline.json Removes resolved type-check findings.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Playwright Results — workflow succeeded

Validated commit 4693a80b5f2c06adcfbfc18766a8a763fdadb79a in Playwright run 36630234156, attempt 1.

✅ 105 passed · ❌ 0 failed · 🟡 0 flaky · ⏭️ 0 skipped · 🧰 0 lifecycle flaky

Performance

Blocking targets: ✅ met · Optimization targets: 🟡 in progress

Shard-job maxima below are not the full workflow wall time; the linked run includes build, fixture, planning, and reporting.

🕒 Full workflow signal wall (to summary) 52m 58s

⏱️ Max setup 5m 33s · max shard execution 12m 44s · max shard-job elapsed before upload 19m 53s · reporting 3s

🌐 251.43 requests/attempt · 1.82 app boots/UI scenario · 0.00% common-shard skew

Optimization targets still in progress:

  • Browser traffic was 251.43 requests per attempt (convergence target: fewer than 200).
  • Application boot ratio was 1.82 per UI scenario (207 boots / 114 scenarios; convergence target: at most 1).
Shard Passed Failed Flaky Skipped Lifecycle failed Lifecycle flaky
✅ Shard chromium-01 40 0 0 0 0 0
✅ Shard ingestion-01 31 0 0 0 0 0
✅ Shard ingestion-02 34 0 0 0 0 0

📦 Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

…iling

The two-day overlap was Snowflake's default statement timeout, but an account can raise it up to 604800 seconds, so after a split a long CALL near the window boundary could lose the child queries it ran past the overlap. Reaching the hard seven-day ceiling means no CALL on any account can outlive it.
Copilot AI review requested due to automatic review settings September 24, 2026 23:23

Copilot AI left a comment

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.

Copilot review overview

🟡 Changes recommended

A zero server-side timeout can allow calls to exceed the fixed seven-day overlap, causing stored-procedure child lineage to be omitted.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)

…ATE only

- Drop the `socket_timeout` default: the driver already bounds every request read at 60 seconds, and any `network_timeout` default cancels long statements
- Treat an error as a cancel only when the server reports SQLSTATE 57014, so a client error that merely mentions 57014 no longer triggers a split
- Derive the window cache size from the split depth instead of a fixed number
Copilot AI lite review requested due to automatic review settings September 29, 2026 20:59

Copilot AI left a comment

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.

Copilot review overview

🟡 Changes recommended

Address the critical result materialization risk and restore an explicit socket timeout guard.

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
Resolved since last review (1)

@gitar-bot

gitar-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Snowflake query timeout defaults and lineage retry/failure handling change execution behavior

Bounds Snowflake stored-procedure history scans, retries cancelled reads over progressively smaller windows, isolates unrecoverable failures so they no longer abort the run, and removes the client-side statement timeout that was capping queries at ten minutes. No issues found.

Review coverage

📋 Rules No rules evaluated

🧪 Functional validation Not enabled · Set up

🤖 Auto-approval Not enabled · Set up

Options

Display: compact → Counting what did not apply, without listing it.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

Copilot AI left a comment

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.

Copilot review overview

🔵 Needs a closer look

The Snowflake window-splitting guard has a moderate boundary issue that should be corrected before approval.

Review effort: Lite
Findings: None

Resolved since last review (2)

@sonarqubecloud

Copy link
Copy Markdown

@mohittilala
mohittilala added this pull request to the merge queue Sep 30, 2026
Merged via the queue into main with commit d389ad6 Sep 30, 2026
127 checks passed
@mohittilala
mohittilala deleted the fix/snowflake-sp-lineage-timeout branch September 30, 2026 16:14
akashverma0786 added a commit that referenced this pull request Oct 2, 2026
main's #33919 (Snowflake stored-procedure history) rewrote the method this
branch guards, so the two guards collided on one try block.

They guard different failures, and each side has a test asserting the
opposite policy for the other's case:

- an engine we cannot reach (this branch: VIEW SERVER STATE denied on one
  database of an ingest-all run) must be skipped with a warning, not
  recorded, so one denied database does not drag the run's success
  percentage down;
- a statement that fails after connecting (main: Snowflake cancels a scan
  too large to run) must be retried over a narrower window and otherwise
  recorded against the pipeline, so a run that silently lost lineage does
  not report green.

Split the single guard in two to keep both. engine.connect() is guarded
per engine in yield_stored_procedure_queries and warns-and-skips;
_yield_queries_for_statement now takes the live connection and guards
conn.execute() only, keeping main's narrowing and status.failed(). Both
sides' tests pass unchanged - they raise at different points.

Statement building stays outside the connect guard, so a bug in a source's
statement builder still surfaces instead of looking like an unreachable
engine.
mohittilala added a commit that referenced this pull request Nov 12, 2026
… isolate its failures (#33919) (#37614)

* Bound the Snowflake stored-procedure history read and stop a cancelled one failing the run

The stored-procedure history statement scanned the whole lookback window with no upper bound, and our own network_timeout default armed the driver's per-statement cancel timer, so a long queryLogDuration died with 000604 and took the whole run down with it, query lineage included.

The read is now bounded at both ends and split in half only when Snowflake cancels it, the socket guard moves to socket_timeout, and a read that still fails is reported against the pipeline instead of raising.

* Size the stored-procedure overlap to Snowflake's statement timeout ceiling

The two-day overlap was Snowflake's default statement timeout, but an account can raise it up to 604800 seconds, so after a split a long CALL near the window boundary could lose the child queries it ran past the overlap. Reaching the hard seven-day ceiling means no CALL on any account can outlive it.

* Stop setting a Snowflake client timeout and classify cancels by SQLSTATE only

- Drop the `socket_timeout` default: the driver already bounds every request read at 60 seconds, and any `network_timeout` default cancels long statements
- Treat an error as a cancel only when the server reports SQLSTATE 57014, so a client error that merely mentions 57014 no longer triggers a split
- Derive the window cache size from the split depth instead of a fixed number

(cherry picked from commit d389ad6)

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ingestion safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Snowflake lineage ingestion fails with 000604 when stored-procedure lineage runs over a long query history

4 participants