forked from delta-io/delta-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add usage guide for check constraints (delta-io#2079)
# 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
1 parent
a9e714e
commit f0ca6cf
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters