Skip to content

TableAlterer

deltalake.table.TableAlterer

TableAlterer(table: DeltaTable)

API for various table alteration commands.

add_columns

add_columns(fields: Union[DeltaField, List[DeltaField]], custom_metadata: Optional[Dict[str, str]] = None, commit_properties: Optional[CommitProperties] = None, post_commithook_properties: Optional[PostCommitHookProperties] = None) -> None

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

Parameters:

Name Type Description Default
fields Union[Field, List[Field]]

fields to merge into schema

required
commit_properties Optional[CommitProperties]

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

None
post_commithook_properties Optional[PostCommitHookProperties]

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], custom_metadata: Optional[Dict[str, str]] = None, post_commithook_properties: Optional[PostCommitHookProperties] = None, commit_properties: Optional[CommitProperties] = 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
custom_metadata Optional[Dict[str, str]]

Deprecated and will be removed in future versions. Use commit_properties instead.

None
post_commithook_properties Optional[PostCommitHookProperties]

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

None
commit_properties Optional[CommitProperties]

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: Union[TableFeatures, List[TableFeatures]], allow_protocol_versions_increase: bool = False, commit_properties: Optional[CommitProperties] = None, post_commithook_properties: Optional[PostCommitHookProperties] = None) -> None

Enable a table feature.

Parameters:

Name Type Description Default
feature Union[TableFeatures, List[TableFeatures]]

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

required
allow_protocol_versions_increase bool

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

False
commit_properties Optional[CommitProperties]

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

None
post_commithook_properties Optional[PostCommitHookProperties]

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, custom_metadata: Optional[Dict[str, str]] = None, post_commithook_properties: Optional[PostCommitHookProperties] = None, commit_properties: Optional[CommitProperties] = 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
custom_metadata Optional[Dict[str, str]]

Deprecated and will be removed in future versions. Use commit_properties instead.

None
post_commithook_properties Optional[PostCommitHookProperties]

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

None
commit_properties Optional[CommitProperties]

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_table_properties

set_table_properties(properties: Dict[str, str], raise_if_not_exists: bool = True, custom_metadata: Optional[Dict[str, str]] = None, commit_properties: Optional[CommitProperties] = 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
custom_metadata Optional[Dict[str, str]]

Deprecated and will be removed in future versions. Use commit_properties instead.

None
commit_properties Optional[CommitProperties]

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"})