Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add usage guide for check constraints #2079

Merged
merged 8 commits into from
Jan 16, 2024
Merged
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
Loading