Skip to content

Guides

Loading through your own connection

Embed the sqlload Rust library and load into a warehouse through a connection your program already holds, with SQLLoad planning and rendering every statement.

A program that embeds the sqlload crate can load into a warehouse through a connection it already holds, instead of one from [connections]. SQLLoad still plans the load and renders every statement; your program runs them with its own credentials. It attaches the connection by name when it parses the project, and targets use that name:

use std::collections::BTreeMap;
use sqlload::adapters::session::models::SessionConnection;
use sqlload::column_types::models::Warehouse;
use sqlload::project::main::parse_python_project::parse_python_project;
let sessions = BTreeMap::from([(
"warehouse".to_owned(),
SessionConnection {
warehouse: Warehouse::Snowflake,
motherduck: false,
database: Some("ANALYTICS".to_owned()),
caller: my_connection, // an Arc<dyn CallerConnection>
},
)]);
let project = parse_python_project(&text, &lookup, (&[], sessions))?;
[targets.dev]
connection = "warehouse" # attached by the program
loader_schema = "RAW"

connect_targets, plan_project, apply_project and load_tables then work as for any target. Run them on threads with 8 MiB of stack: build the Tokio runtime with thread_stack_size(8 * 1024 * 1024), and give the thread that blocks on the load the same. A debug build’s load overflows Tokio’s 2 MiB default.

CallerConnection::open returns a session for SQLLoad alone: SQLLoad opens one per table and drops it when the table is done. The session holds that table’s temporary tables and transactions from one statement to the next, so it can’t be a connection a pool swaps out. A session must:

  • run in autocommit; SQLLoad sends BEGIN, COMMIT and ROLLBACK itself, including ROLLBACK after a failed COMMIT. A Snowflake session with autocommit off is refused when it opens;
  • on DuckDB, have the database tables load into as its current database;
  • let SQLLoad set session parameters (a Snowflake session runs in UTC), so don’t use it for other work at the same time.

Rows reach the warehouse as:

Warehouse Rows go in as
DuckDB, MotherDuck Parquet files on this machine, read with read_parquet
Snowflake Parquet files PUT on the user stage (@~/sqlload/...), then COPY INTO, then REMOVE
Postgres A binary COPY ... FROM STDIN, through CallerSession::copy_in
BigQuery, ClickHouse, Databricks Parquet files your session loads with its own client, through CallerSession::load_parquet

A session reports a failure as a SessionError: transient when a rerun may pass, not_found when what a query names doesn’t exist, such as a BigQuery dataset before its first load.

Write strategies, schema changes, checks and cursors work as on SQLLoad’s own connection, except:

  • The warehouse never reads a files source’s files itself; SQLLoad reads them and sends the rows.
  • Rows are not streamed into Snowflake without files.
  • A DuckDB target gets no memory budget from SQLLoad; your connection’s settings apply.