Skip to content

fix: stage the qualify merge in a session-scoped temporary table - #36

Open
pquadri wants to merge 1 commit into
mainfrom
fix/temporary-merge-temp-table
Open

pquadri wants to merge 1 commit into
mainfrom
fix/temporary-merge-temp-table

Conversation

@pquadri

@pquadri pquadri commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #35 (review comment by @giamo): if the pod is killed while copying into <table>_temp_<id>, nobody cleans the table up and it is untagged.

_merge now runs the whole load in one Snowflake session and the staging table is TEMPORARY: only that session sees it and Snowflake discards it with the session. The manual drop-on-failure path is gone.

Changes

  • Table.temporary adds TEMPORARY to the create-table and qualify statements (non-temporary SQL is unchanged).
  • _merge opens one connection and publishes it through a ContextVar; the existing connect() sites in table.py reuse it, so copy_into, copy_custom, merge and merge_custom need no signature changes.

Review notes

  • A killed process does not end its Snowflake session immediately: the temp table is invisible to everyone else and is dropped when that session expires (idle timeout, hours by default), not on the spot.
  • The whole load now holds one connection for its duration (previously several short ones).
  • Tests: 77 mocked tests pass. Also run against real Snowflake in a throwaway sandbox schema: first load, incremental load (0 duplicate reads, no temp table visible to a reader), forced COPY failure, a hard-killed process (nothing visible afterwards, live table unchanged), merge() path and schema evolution.

https://claude.ai/code/session_011PFLPuv77fp5M3JXdZvW1v


Note

Medium Risk
Changes core load/merge connection lifecycle and staging table semantics; failures no longer explicitly drop temp tables but rely on Snowflake session expiry, and loads hold one connection for longer.

Overview
Qualified copy_into / merge staging no longer leaves orphaned <table>_temp_<id> tables if a worker dies mid-load. _merge now holds one Snowflake session for copy → qualify → merge, and the staging table is created as a session-scoped TEMPORARY table via a new Table.temporary flag (DDL and qualify emit TEMPORARY when set).

A ContextVar plus _connection() lets bulk_insert, _copy, and related paths reuse that session without API changes. The old manual DROP on failure path is removed; cleanup relies on session end. README documents the single-session temporary staging behavior. Tests assert temporary DDL, single connect(), and that failures close the session without merging.

Reviewed by Cursor Bugbot for commit 756e413. Bugbot is set up for automated code reviews on this repo. Configure here.

The temp table behind copy_into(qualify=True)/merge() was a regular table
created over several connections, so a killed process left it behind in
the schema, untagged. _merge now runs the whole load in one session and
the staging table is TEMPORARY: only that session sees it and Snowflake
discards it with the session.

- Table.temporary adds TEMPORARY to create/qualify statements
- a ContextVar shares the _merge session with the existing connect() sites
- removes the manual drop-on-failure path

Claude-Session: https://claude.ai/code/session_011PFLPuv77fp5M3JXdZvW1v
@pquadri
pquadri requested a review from giamo October 1, 2026 14:36
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.

2 participants