Skip to contents

Changes a column's type in place with ALTER TABLE ... ALTER COLUMN ... SET TYPE. DuckLake permits widening promotions only: integers to a wider integer, HUGEINT, FLOAT, or DOUBLE (the floating-point promotions are accepted even where precision can be lost), FLOAT to DOUBLE, TIMESTAMP to TIMESTAMP WITH TIME ZONE, and the "NULL" type to any scalar type. No data files need rewriting.

Usage

set_column_type(table_name, column_name, type)

Arguments

table_name

The table to change.

column_name

Name of the column.

type

The new (wider) SQL type.

Value

Invisibly returns NULL.

Details

A column that was all NA when a table was written from a query gets the "NULL" type, which holds nothing but NULL until promoted; this is the call that gives it its real type once the first values are known.

To narrow a type, which DuckLake refuses, take the explicit route: add_table_column() with the smaller type, copy the values over (after checking they fit), drop_table_column() the original, and rename_table_column() the new column into place.

Examples

lake_dir <- tempfile("coltype_lake_")
dir.create(lake_dir)
attach_ducklake("coltype_lake", lake_path = lake_dir)
create_table(mtcars, "cars")

# Widening promotions only: INTEGER -> BIGINT is fine
add_table_column("cars", "order_id", "INTEGER")
#> Added column "order_id" (INTEGER) to "cars".
#> ℹ Metadata-only change; no data files were rewritten.
set_column_type("cars", "order_id", "BIGINT")
#> Column "order_id" in "cars" is now BIGINT.

detach_ducklake("coltype_lake", shutdown = TRUE)
unlink(lake_dir, recursive = TRUE)