Insert Overwrite
Replace all rows of a table, or only the rows of the named partitions, with the rows of a query. Run it in a SQL paragraph, or in a Magpie paragraph as sql(INSERT OVERWRITE …). See Changing Table Data with SQL for where DML runs, which tables accept it, and worked examples.
Syntax
INSERT OVERWRITE [TABLE] <table> [PARTITION (<partition column> = <value> [, ...])] [(<column> [, ...])] <source>
INSERT OVERWRITE [TABLE] <table> [PARTITION (<partition column> = <value> [, ...])] [(<column> [, ...])]
FROM <relation> SELECT <columns> [WHERE ...] [GROUP BY ...] [ORDER BY ...] [LIMIT <n>]
<source> is a query, VALUES (…), or TABLE <source table>, as for Insert Into.
Parameters
The parameters are the same as for Insert Into: the table, an optional static PARTITION, an optional column list that must repeat the declared order, and a positional source.
PARTITION
Optional. Without it, every row of the table is replaced. With it, only the named partitions are replaced and all other partitions are untouched:
- Delta tables: the named partitions are replaced in one transaction.
- Other file formats: Magpie clears the partition directory and then appends the new rows. This is not atomic: if the append fails, the error names the cleared partition and the statement must be run again. The clause must name the table's leading partition columns.
An empty source still clears the named partitions.
Supported tables
Delta tables and tables on other file formats (Parquet, ORC, CSV, JSON, Avro, text). Connector-backed tables are refused, because overwriting one would drop or truncate it in the source system.
Examples
-- rebuild a dimension table from scratch
INSERT OVERWRITE analytics.dim_products
SELECT product_id, name, category FROM staging.products
-- rebuild only the region = 'us' partition; all other partitions are untouched
INSERT OVERWRITE sales.orders PARTITION (region = 'us')
SELECT order_id, customer_id, amount FROM staging.us_orders_corrected
Errors
Besides the common DML errors and the Insert Into errors:
| Message | Cause |
|---|---|
INSERT OVERWRITE is not supported for table "<table>" (connector: <type>). Overwriting a connector-backed table would drop or truncate it at the system of record; only file-backed targets support INSERT OVERWRITE. | The table is connector-backed. |
INSERT OVERWRITE ... PARTITION on table "<table>" must specify the leading partition columns (<columns>); got (<columns>). | On a non-Delta file table, PARTITION doesn't name the leading partition columns. |
REPLACE WHERE cannot be combined with INSERT OVERWRITE | Use Insert Replace Where with INSERT INTO instead. |
INSERT OVERWRITE DIRECTORY is not supported. Use EXPORT RESULT or data-source COPY/EXPORT commands instead. | Writing to a directory isn't DML. Use Export Result, Copy Data Source File, or Export Data Source File. |
Security
Requires Write permission on the table and Read permission on every table the source reads.
Lineage
An overwrite without PARTITION replaces the table's contents, so Describe stops showing the sources of earlier inserts and merges. How the table was created stays in the lineage.