The COPY ... TO statement writes the result of a query (or the contents of a table) to files.

​
Examples

-- Export a query result to a single Parquet file
COPY (
  SELECT
    user_id,
    sum(amount) AS total
  FROM
    "my_bigquery.my_dataset.transactions"
  GROUP BY
    user_id
) TO 's3://my-bucket/exports/user_totals.parquet';


-- Export a table to a directory of CSV files
COPY "my_postgres.public.users" TO 's3://my-bucket/exports/users/' (FORMAT 'csv');


-- Export selected columns only
COPY "my_postgres.public.users" (user_id, email) TO 'gs://my-bucket/users.parquet';


-- Hive-partitioned Parquet output
COPY (
  SELECT
    *
  FROM
    "my_bigquery.my_dataset.transactions"
) TO 's3://my-bucket/exports/transactions/' (FORMAT 'parquet', PARTITION_BY (country, YEAR));


-- Write an Iceberg table with a partition spec
COPY (
  SELECT
    *
  FROM
    "my_bigquery.my_dataset.transactions"
) TO 's3://my-bucket/warehouse/transactions' (
  FORMAT 'iceberg',
  PARTITION_BY (bucket (16, user_id), DAY (created_at))
);

​
Syntax

syntax
COPY { ( <select_statement> ) | <table_name> [ ( <column> [, ...] ) ] }
  TO '<uri>'
  [ [ WITH ] ( <option> [, ...] ) ];

Use a parenthesized query or a table name as the source. A column list after the table name limits the export to those columns.

Chalk SQL selects the storage backend from the destination, which can be a local path or a URI such as s3:// or gs://. A destination ending in .parquet or .csv targets a single file. A trailing / targets a directory where Chalk SQL writes one or more files with generated names.

​
Options

OptionDescription
FORMAT'parquet' (default), 'csv', or 'iceberg'. When omitted, the format is inferred from the destination file extension, falling back to Parquet.
PARTITION_BYPartitions the output. For Parquet and CSV, accepts column names and produces Hive-style partitioned directories. For Iceberg, additionally accepts the partition transforms bucket(N, col), truncate(W, col), year(col), month(col), day(col), and hour(col).
FORMAT_VERSIONIceberg only: the table format version, 2 (default) or 3, for a table this COPY creates. MERGE INTO an Iceberg table needs v3 (its deletes are deletion vectors).
WRITE_BLOOM_FILTERParquet only: write bloom filters for the output files.
BLOOM_FILTER_FALSE_POSITIVE_RATIOParquet only: bloom filter false-positive ratio in (0, 1); defaults to 0.01. Setting this implies WRITE_BLOOM_FILTER true.

​
Iceberg output

With FORMAT 'iceberg', the destination URI names the Iceberg table root (a directory, not a file). Chalk SQL writes the Parquet data files and commits the Iceberg metadata (manifests and a new snapshot) beneath that root, creating the table on first write. PARTITION_BY entries define the table’s partition spec; a bare column name is an identity transform. FORMAT_VERSION 3 creates the table at Iceberg format version 3, which MERGE INTO requires; the default is version 2.

​
Update and insert rows into an Iceberg table

Use MERGE INTO to update rows that match the source and insert rows for unmatched source rows. Use the double-quoted root URI of an existing Iceberg v3 table as the target.

syntax
MERGE INTO "<table_uri>" [ AS <target_alias> ]
  USING <source> [ AS <source_alias> ]
  ON <match_condition>
  WHEN MATCHED THEN UPDATE SET <column> = <expression> [, ...]
  WHEN NOT MATCHED THEN INSERT [ ( <column> [, ...] ) ]
    VALUES ( <expression> [, ...] );

​
Limitations

  • Chalk SQL does not support COPY ... FROM. To append rows to a connected SQL table, use INSERT INTO.
  • FORMAT 'delta' is not supported.
  • Bloom filter options require FORMAT 'parquet' and cannot be combined with PARTITION_BY.