Skip to content

Commit

Permalink
docs: add usage guide for check constraints (delta-io#2079)
Browse files Browse the repository at this point in the history
# Description
Added a usage guide for adding check constraints to tables

# Related Issue(s)
Closes delta-io#2063 

# Documentation

I just wrote it
  • Loading branch information
hntd187 authored and RobinLin666 committed Feb 2, 2024
1 parent a9e714e commit f0ca6cf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/src/python/check_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def add_constraint():
# --8<-- [start:add_constraint]
from deltalake import DeltaTable

dt = DeltaTable("../rust/tests/data/simple_table")

# Check the schema before hand
print(dt.schema())
# Add the constraint to the table.
dt.alter.add_constraint({"id_gt_0": "id > 0"})
# --8<-- [end:add_constraint]


def add_data():
# --8<-- [start:add_data]
from deltalake import write_deltalake
import pandas as pd

df = pd.DataFrame({"id": [-1]})
write_deltalake(dt, df, mode="append", engine="rust")
# _internal.DeltaProtocolError: Invariant violations: ["Check or Invariant (id > 0) violated by value in row: [-1]"]
# --8<-- [end:add_data]
23 changes: 23 additions & 0 deletions docs/src/rust/check_constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

// --8<-- [start:add_constraint]
let table = deltalake::open_table("../rust/tests/data/simple_table").await?;
let ops = DeltaOps(table);
ops.with_constraint("id_gt_0", "id > 0").await?;
// --8<-- [end:add_constraint]

// --8<-- [start:add_data]
let table = deltalake::open_table("../rust/tests/data/simple_table").await?;
let schema = table.get_state().arrow_schema()?;
let invalid_values: Vec<Arc<dyn Array>> = vec![
Arc::new(Int32Array::from(vec![-10]))
];
let batch = RecordBatch::try_new(schema, invalid_values)?;
table.write(vec![batch]).await?;
// --8<-- [end:add_data]

Ok(())
}
17 changes: 17 additions & 0 deletions docs/usage/constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Adding a Constraint to a table

Check constraints are a way to enforce that only data that meets the constraint is allowed to be added to the table.

## Add the Constraint

{{ code_example('check_constraints', 'add_constraint', ['DeltaTable']) }}

After you have added the constraint to the table attempting to append data to the table that violates the constraint
will instead throw an error.

## Verify the constraint by trying to add some data

{{ code_example('check_constraints', 'add_data', []) }}

Note: ensure you use the `engine='rust'` parameter when writing to the table as this feature is not supported in the
default pyarrow writer.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ nav:
- Creating a table: usage/create-delta-lake-table.md
- Loading a table: usage/loading-table.md
- Append/overwrite tables: usage/appending-overwriting-delta-lake-table.md
- Adding a constraint: usage/constraints.md
- Examining a table: usage/examining-table.md
- Querying a table: usage/querying-delta-tables.md
- Managing a table: usage/managing-tables.md
Expand Down

0 comments on commit f0ca6cf

Please sign in to comment.