Table Operations#
Function |
Description |
|---|---|
Change the columns, properties or partitioning of a table in an attached catalog. |
|
Run a maintenance procedure on a table in an attached catalog. |
|
Write the result of a query, or a table, to a file, or add the rows of a file to a table. |
|
Create a named expression or query with parameters. |
|
Create a namespace in an attached catalog. |
|
Create a new table and its columns from a SQL query executed against an existing table. |
|
Create a named query that is run each time it is read. |
|
Remove specific rows of data from a table using an (optional) constraint. |
|
Returns the column names and types of a table, view, file or query. |
|
Deletes the specified macro. |
|
Drop an empty namespace of an attached catalog. |
|
Deletes the specified table, unregistering it. |
|
Deletes the specified view. |
|
Returns the Polars execution plan for a given SQL query. |
|
Append rows to a table, or replace its rows. |
|
Update, delete and insert rows of a table in an attached catalog from a source. |
|
Returns the names of the SQL functions, optionally those that match a pattern. |
|
Returns a list of all tables and views registered in the given context. |
|
A table with a row per column of each table and view registered in the given context. |
|
Create a table with one column of values from start to stop, stop included. |
|
Create a table with one column of values from start to stop, stop not included. |
|
Unnest one or more arrays as columns in a new table object. |
|
Remove all data from a table without actually deleting it. |
|
Change rows of a table. |
ALTER TABLE#
Change a table of a catalog attached with SQLContext.register_catalog(), with the
name resolved as in a query. Each statement makes one change, in one commit:
ADD [COLUMN] [IF NOT EXISTS] <name> <type>adds a nullable column (NOT NULLis not supported, as the existing rows have no value for it);DROP [COLUMN] [IF EXISTS] <name>drops a column (not one that a partition spec of the table partitions by, also an older one, nor one that the table’s sort order sorts by);RENAME COLUMN <name> TO <new_name>renames a column, keeping its data;ALTER COLUMN <name> [SET DATA] TYPE <type>changes a column’s type, as the catalog allows (such asINTtoBIGINT,FLOATtoDOUBLE, or a widerDECIMAL);ALTER COLUMN <name> DROP NOT NULLmakes a column nullable;SET TBLPROPERTIES ('key' = value, ...)sets table properties;SET PARTITIONED BY (<field>, ...)sets how new data is partitioned, with the fields ofCREATE TABLE ... PARTITION BY, in the order written;RESET PARTITIONED BYstops partitioning new data;SET SORTED BY (<field> [ASC | DESC] [NULLS FIRST | LAST], ...)sets how new data is sorted, with fields as inPARTITION BY, in the order written (truncatealso takes decimals);RESET SORTED BYstops sorting new data;CREATE TAG [IF NOT EXISTS] <name> [AS OF VERSION <snapshot_id>] [RETAIN <n> <unit>]names a snapshot, by default the current one;CREATE BRANCH [IF NOT EXISTS] <name> [AS OF VERSION <snapshot_id>] [RETAIN <n> <unit>] [WITH SNAPSHOT RETENTION <retention>]starts a branch at a snapshot, by default the current one;[CREATE OR] REPLACE TAG | BRANCH <name> ..., with the options ofCREATE, points a tag or branch at a snapshot, by default also the current one, and can change its retention;DROP TAG | BRANCH [IF EXISTS] <name>drops a tag or branch.
Changing the partitioning only changes the table’s metadata. Existing data files keep the
partitioning they were written with, and later writes use the new one. A field that the
table had before keeps its name, and an older spec with the same fields is used again. A
statement adds at most one time transform (year, month, day or hour) per
column. Time fields the table has can stay.
Iceberg v1 tables are not supported: upgrade them to v2 first.
Changing the sort order also only changes the table’s metadata. Later SQL writes (also
DELETE, UPDATE and MERGE, which rewrite data files) and sink_iceberg sort
their data by the new order. A field without NULLS FIRST or NULLS LAST takes the null order of
ORDER BY: nulls last for ASC, first for DESC. Engines differ in this default, so
state the null order in statements that other engines also run. An older sort order with the
same fields is used again.
A tag or branch is read with FOR VERSION AS OF '<name>' (see time travel), and the $refs table lists them with their snapshots. Writes go to the
main branch: writing to another branch is not supported yet. Names are identifiers and
keep their case. A name that starts with a digit or has characters other than letters,
digits and _ needs double quotes. CREATE fails if the name is taken by a tag or
branch, unless with IF NOT EXISTS. REPLACE and DROP fail if the name is a ref of
the other kind. REPLACE keeps the retention the statement does not set. To change only
the retention, give the ref’s own snapshot with AS OF VERSION. The main branch
cannot be replaced or dropped, and no tag can take its name.
RETAIN sets how long the ref is kept, in DAYS, HOURS or MINUTES.
WITH SNAPSHOT RETENTION sets how many snapshots a branch keeps (<n> SNAPSHOTS), how
long it keeps them (<n> <unit>), or both, in that order. Both only store the setting:
snapshots are expired by the engine that does it. A table without snapshots has none to
tag or branch.
Column names are case-sensitive and name top-level columns. SET PARTITIONED BY and
SET SORTED BY also take fields of struct columns (s.f), as
CREATE TABLE ... PARTITION BY does.
ALTER TABLE IF EXISTS does nothing if the table does not exist.
Example:
ALTER TABLE lake.sales.orders ADD COLUMN discount DOUBLE
ALTER TABLE lake.sales.orders RENAME COLUMN qty TO quantity
ALTER TABLE lake.sales.orders ALTER COLUMN quantity SET DATA TYPE BIGINT
ALTER TABLE lake.sales.orders SET TBLPROPERTIES ('owner' = 'finance')
ALTER TABLE lake.sales.orders SET PARTITIONED BY (month(ordered_at), bucket(16, id))
ALTER TABLE lake.sales.orders RESET PARTITIONED BY
ALTER TABLE lake.sales.orders SET SORTED BY (customer_id, ordered_at DESC NULLS LAST)
ALTER TABLE lake.sales.orders RESET SORTED BY
ALTER TABLE lake.sales.orders CREATE TAG "end-of-quarter" RETAIN 365 DAYS
ALTER TABLE lake.sales.orders CREATE BRANCH audit WITH SNAPSHOT RETENTION 3 SNAPSHOTS
ALTER TABLE lake.sales.orders DROP TAG IF EXISTS "end-of-quarter"
CALL#
Run a procedure on a table of a catalog attached with
SQLContext.register_catalog(). A CALL returns a result row.
CALL [<catalog>.]system.<procedure>(<arguments>)
Without
<catalog>, the default catalog is used.Arguments are given in order, or by name as
name => value.Argument values are literals: strings, integers, booleans,
NULL,map('k', 'v')orarray('a', 'b').The
tableargument is a string with the table’s name in that catalog:'ns.t', or't'in the catalog’s default namespace. It may start with the catalog’s name.
add_files
Adds existing Parquet files to a table in one commit, as an append snapshot. The files
are not copied: the table refers to them where they are, and from then on owns them.
Snapshot expiry, DROP TABLE ... PURGE and rewrites that replace them can delete them.
Only the files’ footers are read. To copy the rows instead, use
INSERT INTO <table> SELECT * FROM read_parquet('<files>').
Argument |
Description |
|---|---|
|
The table. Required. |
|
|
|
Not supported yet. |
|
|
|
Accepted. Has no effect. |
A file’s partition value is never guessed:
Files directly in the folder: the file’s column statistics must prove one value per partition field, or that the column is NULL in every row. A file whose statistics are missing, that mixes NULL with values, or whose partition column is a floating-point column, is refused.
Files in
col=valuefolders, as written for Hive tables: the folders give the value, for a table partitioned by those columns as they are (identity). Values are strings, integers, booleans, dates (2024-06-01), timestamps (2024-06-01 10:00:00, with an offset such as+01:00for a timestamp with time zone) or decimals, written exactly.__HIVE_DEFAULT_PARTITION__is NULL. If a file also holds the column, its statistics must agree with the folder. A file may leave the column out only if it is an optional top-level column.
Files must not have Iceberg field ids, and their columns must fit the table’s schema. The
table’s name mapping is set if it has none, as the files are read by column name. A name
mapping the table has must map the files’ columns as its schema does. Catalogs that do not
allow that table property refuse the CALL. It
returns added_files_count and changed_partition_count, the latter from the snapshot
summary, NULL when the summary has none. A folder without files is refused, as a mistyped
path on object storage lists as empty. Files are listed and read with the catalog’s
storage configuration for the table. They may be outside the table’s location, but the
catalog’s credentials then may not reach them. Iceberg v3 tables are not supported yet.
Example:
-- Add the Parquet files in a landing folder to table `orders` in namespace `sales`
-- of catalog `lake`, without copying them.
CALL lake.system.add_files('sales.orders', 'parquet.`s3://bucket/landing/2024-06`')
-- A Hive-style folder: `.../region=eu/part-0.parquet`, for a table partitioned by
-- `region`.
CALL lake.system.add_files(
table => 'sales.orders',
source_table => 'parquet.`s3://bucket/hive/orders`'
)
rewrite_data_files
Compacts the data files of a table in one commit, as a replace snapshot. Small files
are combined, and large ones split, into files of about the target size. Sizes are
estimated from the files read: files that another writer compressed differently can take
a second CALL to reach the target size. The rows stay the
same: rows that delete files removed are not written again. The new files are written
under the table’s current partition spec and sorted by its sort order. Delete files stay.
Files are selected as Iceberg’s binpack strategy does: per partition, a group of files is
rewritten when it has enough small or large files, or files with many deleted rows.
Argument |
Description |
|---|---|
|
The table. Required. |
|
|
|
Not supported yet. |
|
A
|
|
A condition as a string. Only the files that may hold a matching row are
considered, and they are rewritten whole. |
It returns rewritten_data_files_count, added_data_files_count,
rewritten_bytes_count, failed_data_files_count and removed_delete_files_count.
The last two are always 0. If no group needs a rewrite, nothing is committed and the counts
are 0. Concurrent appends do not conflict with a compaction. A concurrent change to a
rewritten file, such as a delete, makes it fail. Iceberg v3 tables, tables with equality
delete files, and position delete files that are not of one data file are not supported
yet.
Example:
-- Compact every data file of table `orders` in namespace `sales` of catalog `lake`,
-- partition by partition, into files of about 128 MB.
CALL lake.system.rewrite_data_files(
table => 'sales.orders',
options => map('rewrite-all', 'true', 'target-file-size-bytes', '134217728')
)
-- Only the files that may hold rows of 2024, by position: the strategy, no sort
-- order, no options, then the condition.
CALL lake.system.rewrite_data_files(
'sales.orders', 'binpack', NULL, NULL, 'ordered_at >= ''2024-01-01'''
)
rewrite_manifests
Rewrites the data manifests of a table in one commit, as a replace snapshot. The rows
and data files stay the same. The new manifests are sorted by partition and about
commit.manifest.target-size-bytes in size (default 8 MB), so queries skip more of them.
Delete manifests are not changed.
Argument |
Description |
|---|---|
|
The table. Required. |
|
Accepted. Has no effect. |
|
The partition spec whose manifests are rewritten. Default: the current spec. |
|
Not supported yet. |
It returns rewritten_manifests_count and added_manifests_count. If there is nothing
to rewrite (no data manifests, or one small enough), nothing is committed and both counts
are 0. Iceberg v3 tables are not supported yet.
Example:
-- Table `orders` in namespace `sales` of catalog `lake`: rewrite the data manifests of
-- its current partition spec into fewer manifests, sorted by partition. Rows and data
-- files stay the same.
CALL lake.system.rewrite_manifests(table => 'sales.orders')
-- The same table, given by position. Only the manifests of partition spec 0 are
-- rewritten, e.g. those written before the table's partitioning changed. Other specs'
-- manifests stay as they are.
CALL lake.system.rewrite_manifests('sales.orders', spec_id => 0)
COPY#
Write the result of a query, or a table, to a file. SQLContext.execute() returns a
LazyFrame that writes the file once it is collected; collecting it gives an empty frame.
COPY <table> FROM '<file>' adds the rows of a file to a table instead, see below.
The format is the one that FORMAT names, or else the one of the file extension: csv;
parquet; ipc, arrow or feather; ndjson or jsonl. A CSV file takes the
options HEADER (written unless set to false), DELIMITER, NULL and QUOTE.
The path can be a cloud URL, and a leading ~ is the home directory.
The directory of the file must exist, and an existing file is replaced. If the query fails while it runs, a partial file can be left behind, also in the place of a file that existed.
Writing files can be turned off with SQLContext(allow_file_writes=False).
COPY <table> [(<column>, ...)] FROM '<file>' adds the rows of the file to a registered
table, as INSERT does: the file’s columns go by position into the listed
columns, or into all of them, and the other columns get NULL. As in Postgres, the file is read
at the COPY, so a missing file or a value that does not fit fails there and leaves the
table as it was. The format is chosen as for COPY ... TO. The path names one file: a
directory is refused, and a CSV path is not a glob.
A CSV file is read into the types of the columns, by position.
HEADERisfalseunless given, as in Postgres;HEADER trueskips the first line, whatever its names.DELIMITERandQUOTEset the separator and the quote character.In a CSV file, an unquoted empty field is NULL and a quoted
""is an empty string. WithNULL '<marker>', the marker is NULL, and an empty field of a text column is an empty string.The CSV file is parsed as
read_csvparses it, which accepts more than Postgres does: a quoted marker is NULL too, an empty field of a column of another type is NULL, a row with too few fields is filled with NULLs, a number too large for aREALorDOUBLEcolumn is infinite, and booleans aretrueandfalseonly.A file of another format keeps its own column types, which convert as in
INSERT.COPY ... FROM STDINis not supported. Reading files can be turned off withSQLContext(allow_file_reads=False).
Example:
COPY (SELECT region, SUM(amount) AS total FROM sales GROUP BY region) TO 'totals.parquet'
COPY sales (region, amount) TO 'sales.csv'
COPY sales TO 'sales.txt' (FORMAT csv, DELIMITER '|', HEADER false)
COPY sales FROM 'sales.csv' (HEADER true)
COPY contacts (id, name, phone) FROM 'contacts.csv' (FORMAT csv, HEADER true, NULL '\N')
CREATE MACRO#
Create a macro: a named expression with parameters (a scalar macro), or a named query with
parameters (a table macro, with AS TABLE). A call is replaced by the body of the macro,
with the parameters replaced by the arguments. CREATE FUNCTION is the same statement.
A parameter can have a default (
b := 5); a call can give arguments by name (b := 1).A macro can have several definitions, for different numbers of arguments.
A scalar macro is called like a function; a table macro is called in
FROM.Outside subqueries, the body of a scalar macro can only use its parameters.
An argument that reads columns cannot be used in a subquery of the macro body.
OR REPLACEreplaces an existing macro.TEMP/TEMPORARYis accepted; every macro lives in its context. A view uses the macros as they are when it is read.
Example:
CREATE MACRO add_tax(amount, rate := 0.21) AS amount * (1 + rate)
CREATE MACRO clamp(a) AS greatest(a, 0),
(a, hi) AS least(greatest(a, 0), hi)
CREATE MACRO orders_since(d) AS TABLE SELECT * FROM orders WHERE order_date >= d
SELECT add_tax(amount), add_tax(amount, rate := 0.09) FROM orders
SELECT * FROM orders_since('2024-01-01')
CREATE SCHEMA#
Create a namespace in a catalog attached with SQLContext.register_catalog(). A name
that starts with an attached catalog is a namespace of that catalog; any other name is one
of the default catalog, not under its default_namespace. An existing namespace raises an
error unless IF NOT EXISTS is given, which leaves it as it is. WITH (...) sets the
namespace’s properties, such as 'location', where its tables go by default; the catalog
decides which properties it keeps and uses.
Example:
CREATE SCHEMA lake.sales
CREATE SCHEMA IF NOT EXISTS lake.sales WITH ('location' = 's3://bucket/sales')
CREATE TABLE#
Create a new table from a sequence of column definitions, a SQL query executed against an existing table, or create an empty table from the schema of an existing table.
Example:
CREATE TABLE tbl(colx VARCHAR, coly DATE, colz ARRAY<DOUBLE>, cols STRUCT<a INT, b VARCHAR>)
CREATE TABLE new_table AS
SELECT * FROM existing_table WHERE value > 42
CREATE TABLE new_table LIKE existing_table
Temporary tables: CREATE TEMP TABLE (or TEMPORARY) always registers the
table in the context, also when a default catalog is attached. Unlike CREATE TABLE,
it raises an error if the table already exists, unless IF NOT EXISTS (leave it as it
is) or OR REPLACE (replace it) is given.
CREATE OR REPLACE TEMP TABLE staging AS
SELECT * FROM existing_table WHERE value > 42
Catalog tables: with an attached Iceberg catalog (see
SQLContext.register_catalog()), a name that resolves to the catalog, as in a query,
creates the table in the catalog.
CREATE TABLE lake.sales.orders (
id BIGINT NOT NULL, region VARCHAR, ordered_at TIMESTAMP, amount DECIMAL(10, 2)
)
WITH ('format-version' = 2)
CREATE TABLE IF NOT EXISTS lake.sales.big_orders
LOCATION 's3://bucket/big_orders'
PARTITION BY (region, day(ordered_at), bucket(16, id))
AS SELECT * FROM lake.sales.orders WHERE amount > 1000
NOT NULLmakes a column a required field; other constraints are not supported.PARTITION BY (...)sets the partition spec: a column (identity),year,month,dayorhourof a column,bucket(N, column)ortruncate(W, column). A column can also be a field of a struct column:s.for"s"."f"is fieldfof structs, never a table-qualified column, and"s.f"is a column nameds.f.DELETEandUPDATEconditions can use such a field only when its struct column isNOT NULL, so not one level deeper, and not in a table created withAS SELECT.PARTITIONED BY (column, ...)takes top-level column names only.LOCATION(or the'location'option) sets the table location; the otherWITHoptions become table properties.AS SELECTcommits the rows together with the create, so a failed query creates no table. A catalog that cannot stage a create gets an empty table first, dropped again if filling it fails, unless another writer changed it. The columns are never required. The rows are written as byLazyFrame.sink_iceberg(): write properties such aswrite.parquet.compression-codecare stored, but not applied to them.IF NOT EXISTSleaves an existing table as it is; its query is checked, but not run.LIKEandOR REPLACEare not supported for catalog tables.
CREATE VIEW#
Create a view: a named query that is planned again each time a statement reads it, so it
reads the tables as they are then. Use CREATE TABLE ... AS to keep the result of a
query as it is now.
OR REPLACEreplaces an existing view;IF NOT EXISTSleaves it as it is.A list of column names after the view name renames the columns of the query.
TEMP/TEMPORARYis accepted; every view lives in its context.Tables and views share names, a view cannot read itself, and a view cannot read the tables of an attached catalog.
Example:
CREATE VIEW recent_orders AS SELECT * FROM orders WHERE order_date >= '2024-01-01'
CREATE OR REPLACE VIEW order_totals (customer, total) AS
SELECT customer_id, SUM(amount) FROM orders GROUP BY customer_id
DELETE#
Remove specific rows from a table using an (optional) constraint.
Omitting the constraint deletes all rows, equivalent to TRUNCATE.
On a registered table, it returns the remaining rows, and later statements in the same context see the table without the deleted rows. Source files and the original frame are not changed.
The table keeps the condition, not its result. Each read of the table runs the condition
again on the table’s source. A subquery of the condition keeps its tables as registered at
the time of the DELETE. A condition whose subquery reads the table itself is computed at
the DELETE, as in Postgres.
A condition that names a missing column fails at the
DELETEand leaves the table unchanged. Other errors in the condition, such as a string compared with an integer or a cast of a string that is not a number, show when the table is read, on each read until the table is registered again.Collecting the result of a
DELETEcomputes the remaining rows. Leave the result lazy when only the change is needed.Each
DELETEadds its condition to the table. After many, register the collected rows again:ctx.register("t", ctx.execute("SELECT * FROM t", eager=True)).
On a table of an attached catalog, it deletes the matching rows in one commit, as one snapshot. It returns the statement, not the remaining rows. Rows where the condition is NULL are kept.
The condition compares columns with constants (=, <>, <, <=, >, >=,
IN, BETWEEN, IS [NOT] NULL, LIKE 'prefix%'), combined with AND, OR
and NOT. On floating-point columns, >, >= and <> are not supported. String
constants may only contain letters, digits, spaces, - and _ for now. Decimal
columns cannot be compared with constants yet.
Data files whose rows all match are dropped. If other files match only partly, the files that may match are read, and the partly matching ones are rewritten without the deleted rows (copy-on-write), also on a merge-on-read table. Not supported yet:
Iceberg v3 tables.
Reading a data file that is not Parquet.
Reading a data file whose delete files cannot be applied, such as equality deletes or position deletes that cover several data files.
If another writer commits first:
DELETEretries when that writer’s rows cannot match the condition.Otherwise it fails, unless the table’s
write.delete.isolation-levelissnapshot. The other writer’s matching rows are then kept.A
DELETEthat reads files fails at any isolation level if that writer deleted or replaced rows of those files, or changed the table’s schema or partition spec.
Example:
DELETE FROM some_table WHERE value < 0
DELETE FROM lake.sales.orders WHERE order_date < DATE '2020-01-01'
DESCRIBE#
Returns the column names and data types of a table, a view, a file or the result of a query,
as the columns column_name and column_type. DESC is the same. A query is not run.
A file is given by its path in single quotes, as in FROM; only what its columns
are known from is read, such as the metadata of a Parquet file or the first rows of a CSV file.
A type is given by the SQL name that CAST reads back as that type, with a nested
type in full, such as STRUCT<lat DOUBLE PRECISION, lon DOUBLE PRECISION>. A type that has
no SQL name, such as a fixed-size array, is given by its Polars name in lower case, also inside
a nested type; such a name cannot be used in CAST.
Example:
DESCRIBE some_table
DESCRIBE 'data/events.parquet'
DESCRIBE SELECT a, b * 2 AS c FROM some_table
DROP MACRO#
Deletes the specified scalar macro, or table macro with DROP MACRO TABLE. DROP FUNCTION
deletes a scalar macro. Dropping a macro that does not exist raises an error unless the
optional IF EXISTS clause is given.
Example:
DROP MACRO add_tax
DROP MACRO TABLE IF EXISTS orders_since
DROP SCHEMA#
Drop a namespace of an attached catalog, with the name resolved as in
CREATE SCHEMA. Only an empty namespace is dropped: the namespace is
checked for tables and child namespaces before the drop, and raises an error if it has any
(CASCADE is not supported). Dropping a namespace
that does not exist raises an error unless IF EXISTS is given.
Example:
DROP SCHEMA lake.sales_old
DROP SCHEMA IF EXISTS lake.scratch
DROP TABLES#
Deletes the specified table, unregistering it. Dropping a table that does not
exist raises an error unless the optional IF EXISTS clause is given, in
which case the statement is a no-op.
Example:
DROP TABLE actual_table
DROP TABLE IF EXISTS possible_table
Catalog tables: a name that resolves to an attached catalog, and to no registered
table, drops the table from the catalog, permanently. PURGE also deletes its data
files. Some catalogs drop tables only with PURGE.
DROP TABLE lake.sales.orders_old PURGE
DROP VIEW#
Deletes the specified views. Dropping a view that does not exist raises an error unless
the optional IF EXISTS clause is given.
Example:
DROP VIEW recent_orders
DROP VIEW IF EXISTS order_totals
EXPLAIN#
Returns the Polars execution plan for a given SQL query.
Example:
EXPLAIN SELECT * FROM some_table
INSERT#
Write rows to a table. INSERT INTO appends the rows of a VALUES list or a query.
INSERT OVERWRITE replaces all rows of the table, in every partition. A WHERE in its
query selects the new rows, not the rows replaced.
Values fill the columns in order, or the listed columns. Other columns get NULL. A
NOT NULL column of a catalog table needs a value. Numbers convert to any numeric type.
A fraction going into an integer column rounds: a decimal half away from zero, a float half
to even. CAST truncates instead. Dates and timestamps convert among themselves. Other
conversions need a CAST. A value that does not fit raises an error.
On a registered table, it returns the table with the new rows, and later statements in the
same context see them, as after DELETE. Source files and the
original frame are not changed. The rows of a VALUES list without subqueries are computed
at the INSERT. The rows of a query are not: the table keeps the query, and each read of
the table runs it again, on the tables as they were at the time of the INSERT. A value of
the query that does not fit shows when the table is read, on each read until the table is
registered again. A query that reads the table itself, as in an incremental load
(INSERT INTO t SELECT ... WHERE id NOT IN (SELECT id FROM t)), is computed at the
INSERT, as in Postgres.
On a table of a catalog attached with SQLContext.register_catalog(), it writes the
rows in one commit. If another writer commits first, INSERT INTO retries.
INSERT OVERWRITE fails if that writer added rows, unless the table’s
write.delete.isolation-level is snapshot.
Example:
INSERT INTO orders VALUES (1, 10), (2, 20)
INSERT INTO lake.sales.orders (quantity, id) SELECT qty, order_id FROM staged
INSERT OVERWRITE lake.sales.orders SELECT * FROM staged
MERGE#
Update, delete and insert rows of a table of a catalog attached with
SQLContext.register_catalog() from a source, in one commit. ON pairs target rows
with source rows. Each target row with a matching source row takes the first
WHEN MATCHED clause whose condition holds: UPDATE SET, DELETE or
DO NOTHING. Each source row that matches no target row takes the first
WHEN NOT MATCHED clause whose condition holds: INSERT or DO NOTHING. Those
clauses see the source columns only. A row with no applicable clause is left as it is. It
returns the statement.
The source can be a catalog table, the target itself, a registered frame, a subquery
(with an alias), VALUES with column names or a table function. It is read once, so
every clause sees the same source rows. The source columns the statement uses are held in
memory while it runs.
ON must contain a key: target_column = source_expression, alone or joined with
AND. Other conditions of ON and the WHEN conditions may use both sides. A
NULL key matches nothing. A target row may be updated or deleted by one source row at
most, or the statement fails.
Values convert to the column types as in INSERT, and only the rows taking
an action must fit. INSERT without a column list takes a value for every column.
NOT MATCHED BY SOURCE, UPDATE SET *, INSERT *, RETURNING, DEFAULT,
subqueries in conditions or values, WITH before MERGE and ON conditions without
a key are not supported yet. Column names are case-sensitive. Registered tables cannot be
merged into.
The data files holding a target row to update or delete are read in full and replaced, and the inserted rows are added, in one snapshot (copy-on-write). The source’s keys select the files that are read. A merge that changes nothing commits nothing. Iceberg v3 tables, and files whose delete files cannot be read with them (such as equality deletes), are not supported yet.
If another writer commits first, MERGE retries when that writer’s rows cannot match
the source’s keys. Otherwise it fails, unless the table’s write.update.isolation-level
is snapshot. It also fails if that writer deleted or replaced rows of the files being
rewritten, or changed the table’s schema or partition spec.
Example:
MERGE INTO lake.sales.orders AS t
USING updates AS s
ON t.id = s.id
WHEN MATCHED AND s.cancelled THEN DELETE
WHEN MATCHED THEN UPDATE SET status = s.status
WHEN NOT MATCHED THEN INSERT (id, status) VALUES (s.id, s.status)
SHOW FUNCTIONS#
Returns the names of the SQL functions, table functions such as read_csv included, in a
column name. LIKE (or ILIKE) keeps those that match a pattern; function names are
case-insensitive, and so is the pattern.
Example:
SHOW FUNCTIONS LIKE 'regexp%'
SHOW TABLES#
Returns a list of all tables and views registered in the given context.
Example:
SHOW TABLES
INFORMATION_SCHEMA.COLUMNS#
A table with a row per column of each table and view registered in the given context, in the
order of their names. Its columns are table_name, column_name, ordinal_position
(starting at 1), data_type (the Polars data type, as in DESCRIBE) and
is_nullable (always 'YES'). Read in a view, it does not list the views that read it.
Example:
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'some_table'
GENERATE_SERIES#
Create a table with one column of values from start to stop, stop included. Takes the
same arguments as the GENERATE_SERIES function; the arguments
must be constants. The column is named generate_series unless renamed by an alias.
Example:
SELECT * FROM
GENERATE_SERIES(DATE '2026-01-01', DATE '2026-12-31', INTERVAL '1 day') AS cal (d)
RANGE#
Like GENERATE_SERIES, but stop is not included. The
column is named range unless renamed by an alias.
Example:
SELECT * FROM RANGE(0, 10, 2)
UNNEST#
Unnest one or more arrays as columns in a new table object.
Example:
SELECT * FROM
UNNEST(
[1, 2, 3, 4],
['ww','xx','yy','zz'],
[23.0, 24.5, 28.0, 27.5]
) AS tbl (x,y,z)
TRUNCATE#
Remove all data from a table without deleting the table itself. Truncating a
table that does not exist raises an error unless the optional IF EXISTS
clause is given, in which case the statement is a no-op.
On a catalog table, it commits once and writes no data files. Like INSERT OVERWRITE,
it fails if another writer added rows.
Example:
TRUNCATE TABLE actual_table
TRUNCATE TABLE IF EXISTS possible_table
UPDATE#
Change rows of a table. SET gives each column a new value, computed from the row as it
was before the update. SET (a, b) = (x, y) sets several columns at once. Without
WHERE, every row is updated. Rows where the condition is NULL are not updated.
Values convert to the column types as in INSERT, and only the values of
updated rows must fit. A value comes from its own row, so aggregate, window and
set-returning functions are not supported. Subqueries, FROM, RETURNING and
DEFAULT are not supported either. Column names are case-sensitive.
On a registered table, it returns the table with the new values, and later statements in
the same context see them, as after DELETE. Source files and the
original frame are not changed. As with DELETE, the table keeps the change, not its
result: a value that does not fit shows when the table is read, on each read until the table
is registered again.
On a table of a catalog attached with SQLContext.register_catalog(), it updates the
rows in one commit, and returns the statement. The condition has the same limits as in
DELETE. The data files holding a matching row are read in full
and replaced in one snapshot (copy-on-write). An update that matches no row commits
nothing. Iceberg v3 tables, and files whose delete files cannot be read with them (such as
equality deletes), are not supported yet.
If another writer commits first, UPDATE retries when that writer’s rows cannot match the
condition. Otherwise it fails, unless the table’s write.update.isolation-level is
snapshot. The other writer’s matching rows then keep their values. It also fails if that
writer deleted or replaced rows of the files being rewritten, or changed the table’s schema
or partition spec.
Example:
UPDATE orders SET status = 'shipped' WHERE id = 42
UPDATE lake.sales.orders SET (qty, amount) = (qty + 1, amount * 2) WHERE region = 'EU'