Skip to content

TableAlterer

deltalake.table.TableAlterer

TableAlterer(table: DeltaTable)

API for various table alteration commands.

add_columns

add_columns(fields: DeltaField | list[DeltaField], commit_properties: CommitProperties | None = None, post_commithook_properties: PostCommitHookProperties | None = None) -> None

Add new columns and/or update the fields of a stuctcolumn

Parameters:

Name Type Description Default
fields Field | list[Field]

fields to merge into schema

required
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used.

None
post_commithook_properties PostCommitHookProperties | None

properties for the post commit hook. If None, default values are used.

None
Example
from deltalake.schema import Field, PrimitiveType, StructType
dt = DeltaTable("test_table")
new_fields = [
    Field("baz", StructType([Field("bar", PrimitiveType("integer"))])),
    Field("bar", PrimitiveType("integer"))
]
dt.alter.add_columns(
    new_fields
)

add_constraint

add_constraint(constraints: dict[str, str], post_commithook_properties: PostCommitHookProperties | None = None, commit_properties: CommitProperties | None = None) -> None

Add constraints to the table. Limited to single constraint at once.

Parameters:

Name Type Description Default
constraints dict[str, str]

mapping of constraint name to SQL-expression to evaluate on write

required
post_commithook_properties PostCommitHookProperties | None

properties for the post commit hook. If None, default values are used.

None
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used.

None
Example
from deltalake import DeltaTable
dt = DeltaTable("test_table_constraints")
dt.alter.add_constraint({
    "value_gt_5": "value > 5",
})

Check configuration

dt.metadata().configuration
{'delta.constraints.value_gt_5': 'value > 5'}

add_feature

add_feature(feature: TableFeatures | list[TableFeatures], allow_protocol_versions_increase: bool = False, commit_properties: CommitProperties | None = None, post_commithook_properties: PostCommitHookProperties | None = None) -> None

Enable a table feature.

Parameters:

Name Type Description Default
feature TableFeatures | list[TableFeatures]

Table Feature e.g. Deletion Vectors, Change Data Feed

required
allow_protocol_versions_increase bool

Allow the protocol to be implicitly bumped to reader 3 or writer 7

False
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used.

None
post_commithook_properties PostCommitHookProperties | None

properties for the post commit hook. If None, default values are used.

None
Example
from deltalake import DeltaTable
dt = DeltaTable("test_table")
dt.alter.add_feature(TableFeatures.AppendOnly)

Check protocol

dt.protocol()
ProtocolVersions(min_reader_version=1, min_writer_version=7, writer_features=['appendOnly'], reader_features=None)

drop_constraint

drop_constraint(name: str, raise_if_not_exists: bool = True, post_commithook_properties: PostCommitHookProperties | None = None, commit_properties: CommitProperties | None = None) -> None

Drop constraints from a table. Limited to single constraint at once.

Parameters:

Name Type Description Default
name str

constraint name which to drop.

required
raise_if_not_exists bool

set if should raise if not exists.

True
post_commithook_properties PostCommitHookProperties | None

properties for the post commit hook. If None, default values are used.

None
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used.

None
Example
from deltalake import DeltaTable
dt = DeltaTable("test_table_constraints")
dt.metadata().configuration
{'delta.constraints.value_gt_5': 'value > 5'}

Drop the constraint

dt.alter.drop_constraint(name = "value_gt_5")

Configuration after dropping

dt.metadata().configuration
{}

set_column_metadata

set_column_metadata(column: str, metadata: dict[str, str], commit_properties: CommitProperties | None = None, post_commithook_properties: PostCommitHookProperties | None = None) -> None

Update a field's metadata in a schema. If the metadata key does not exist, the entry is inserted.

If the column name doesn't exist in the schema - an error is raised.

:param column: name of the column to update metadata for. :param metadata: the metadata to be added or modified on the column. :param commit_properties: properties of the transaction commit. If None, default values are used. :param post_commithook_properties: properties for the post commit hook. If None, default values are used. :return:

set_table_description

set_table_description(description: str, commit_properties: CommitProperties | None = None) -> None

Set the description of the table.

Parameters:

Name Type Description Default
description str

the description of the table

required
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used. Note: This parameter is not yet implemented and will be ignored.

None
Example
from deltalake import DeltaTable
dt = DeltaTable("test_table")
dt.alter.set_table_description("new_table_description")

set_table_name

set_table_name(name: str, commit_properties: CommitProperties | None = None) -> None

Set the name of the table.

Parameters:

Name Type Description Default
name str

the name of the table

required
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used. Note: This parameter is not yet implemented and will be ignored.

None
Example
from deltalake import DeltaTable
dt = DeltaTable("test_table")
dt.alter.set_table_name("new_table_name")

set_table_properties

set_table_properties(properties: dict[str, str], raise_if_not_exists: bool = True, commit_properties: CommitProperties | None = None) -> None

Set properties from the table.

Parameters:

Name Type Description Default
properties dict[str, str]

properties which set

required
raise_if_not_exists bool

set if should raise if not exists.

True
commit_properties CommitProperties | None

properties of the transaction commit. If None, default values are used.

None
Example
from deltalake import write_deltalake, DeltaTable
import pandas as pd
df = pd.DataFrame(
    {"id": ["1", "2", "3"],
    "deleted": [False, False, False],
    "price": [10., 15., 20.]
    })
write_deltalake("tmp", df)

dt = DeltaTable("tmp")
dt.alter.set_table_properties({"delta.enableChangeDataFeed": "true"})