Piped SQL from stdin

When stdin is not a TTY, the default invocation reads the entire stdin as a multi-statement SQL script and executes it. Output uses --format.

Category: script-execution

Syntax

delta-forge-cli < <FILE>  |  cat <FILE> | delta-forge-cli

Description

## Overview DeltaForge detects whether stdin is attached to a terminal. When stdin is a TTY (the normal interactive case), the default invocation launches the REPL. When stdin is piped or redirected, the default invocation silently switches to script mode: it reads the entire stream to EOF and executes the SQL it contains. This is the primary integration point for shell pipelines, heredocs, and CI jobs that generate SQL on the fly. ## Behavior - The TTY check uses `std::io::IsTerminal::is_terminal(&std::io::stdin())`. If stdin is not a terminal and no subcommand was specified, the CLI routes to `cmd_run_stdin`. - `cmd_run_stdin` reads stdin fully into a string, then hands the text to the same executor used by `run`: split into statements, authenticate, send each statement to the compute engine in order. - Output uses the format selected by `--format` (default `table`). Result sets are printed with the chosen formatter, DML messages with the elapsed-time wrapper. JSON mode prints one JSON document per statement on stdout. - Piped execution does NOT print `[N/TOTAL] preview ... (elapsed)` progress lines to stderr. Progress reporting is specific to `run <FILE>`; piped mode stays quiet on stderr so the stdout stream is safe to consume directly. - An error from any statement halts execution and returns exit code 1. Earlier statements that succeeded remain committed; there is no script-level transaction. - `-D` variable substitution is not available in piped mode. Callers that need templating should preprocess the text with `envsubst`, `sed`, or a language templating tool and pipe the rendered SQL in. ## Compatibility - Statement splitting, authentication, retry, and per-statement execution are identical between `run <FILE>` and piped stdin. The differences are input source, lack of progress output, and absence of `-D`. - Credentials follow the normal precedence: profile, then env, then CLI flags. Inline credential prompting is TTY-gated, so piped mode never prompts; unset credentials produce an auth failure instead. - Works well with shell heredocs, process substitution, and pipe chains. Any writer that produces UTF-8 on stdout can feed the CLI.

Examples

# Redirect a file into stdin
delta-forge-cli < migrations/v2.sql
# Pipe through a preprocessor
cat fixtures/seed.sql | envsubst | delta-forge-cli --format compact
# Inline one-liner
echo 'SELECT 1' | delta-forge-cli --format json
# Profile selection works the same as with subcommands
delta-forge-cli --profile staging < nightly/etl.sql
# Heredoc, useful in shell scripts
delta-forge-cli --format json <<'SQL'
CREATE TABLE IF NOT EXISTS t (id INT, name STRING);
INSERT INTO t VALUES (1, 'alpha'), (2, 'beta');
SELECT COUNT(*) FROM t;
SQL

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →