|
| 1 | +--- |
| 2 | +title: 'Constraints' |
| 3 | +sidebar_label: 'Constraints' |
| 4 | +sidebar_position: 2 |
| 5 | +description: 'Learn how to add/configure constraints on local acceleration tables in Spice.' |
| 6 | +--- |
| 7 | + |
| 8 | +Constraints are rules that enforce data integrity in a database. Spice supports constraints on locally accelerated tables to ensure data quality, as well as configuring the behavior for inserting data updates that violate constraints. |
| 9 | + |
| 10 | +Constraints are specified in the Spicepod via the `primary_key` field in the acceleration configuration. Additional unique constraints are specified via the [`indexes`](./indexes.md) field with the value `unique`. |
| 11 | + |
| 12 | +The behavior of inserting data that violates the constraint can be configured via the `on_conflict` field to either `drop` the data that violates the constraint or `upsert` that data into the accelerated table (i.e. update all values other than the columns that are part of the constraint to match the incoming data). |
| 13 | + |
| 14 | +If there are multiple rows in the incoming data that violate any constraint, the entire incoming batch of data will be dropped. |
| 15 | + |
| 16 | +Example Spicepod: |
| 17 | + |
| 18 | +```yaml |
| 19 | +datasets: |
| 20 | + - from: spice.ai/eth.recent_blocks |
| 21 | + name: eth.recent_blocks |
| 22 | + acceleration: |
| 23 | + enabled: true |
| 24 | + engine: sqlite |
| 25 | + primary_key: hash # Define a primary key on the `hash` column |
| 26 | + indexes: |
| 27 | + "(number, timestamp)": unique # Add a unique index with a multicolumn key comprised of the `number` and `timestamp` columns |
| 28 | + on_conflict: |
| 29 | + # Upsert the incoming data when the primary key constraint on "hash" is violated, |
| 30 | + # alternatively "drop" can be used instead of "upsert" to drop the data update. |
| 31 | + hash: upsert |
| 32 | +``` |
| 33 | +
|
| 34 | +## Column References |
| 35 | +
|
| 36 | +Column references can be used to specify which columns are part of the constraint. The column reference can be a single column name or a multicolumn key. The column reference must be enclosed in parentheses if it is a multicolumn key. |
| 37 | +
|
| 38 | +Examples |
| 39 | +
|
| 40 | +- `number`: Reference a constraint on the `number` column |
| 41 | +- `(hash, timestamp)`: Reference a constraint on the `hash` and `timestamp` columns |
| 42 | + |
| 43 | +## Limitations |
| 44 | + |
| 45 | +- **Not supported for in-memory Arrow:** The default in-memory Arrow acceleration engine does not support constraints. Use [DuckDB](../../data-accelerators/duckdb.md), [SQLite](../../data-accelerators/sqlite.md), or [PostgreSQL](../../data-accelerators/postgres/index.md) as the acceleration engine to enable constraint checking. |
| 46 | +- **Single on_conflict target supported**: Only a single `on_conflict` target can be specified, unless all `on_conflict` targets are specified with drop. |
| 47 | + |
| 48 | + - <details> |
| 49 | + <summary>Examples for valid/invalid `on_conflict` targets</summary> |
| 50 | + <div> |
| 51 | + The following Spicepod is invalid because it specifies multiple `on_conflict` targets with `upsert`: |
| 52 | + |
| 53 | + :::danger[Invalid] |
| 54 | + ```yaml |
| 55 | + datasets: |
| 56 | + - from: spice.ai/eth.recent_blocks |
| 57 | + name: eth.recent_blocks |
| 58 | + acceleration: |
| 59 | + enabled: true |
| 60 | + engine: sqlite |
| 61 | + primary_key: hash |
| 62 | + indexes: |
| 63 | + "(number, timestamp)": unique |
| 64 | + on_conflict: |
| 65 | + hash: upsert |
| 66 | + "(number, timestamp)": upsert |
| 67 | + ``` |
| 68 | + ::: |
| 69 | + |
| 70 | + The following Spicepod is valid because it specifies multiple `on_conflict` targets with `drop`, which is allowed: |
| 71 | + |
| 72 | + :::tip[Valid] |
| 73 | + ```yaml |
| 74 | + datasets: |
| 75 | + - from: spice.ai/eth.recent_blocks |
| 76 | + name: eth.recent_blocks |
| 77 | + acceleration: |
| 78 | + enabled: true |
| 79 | + engine: sqlite |
| 80 | + primary_key: hash |
| 81 | + indexes: |
| 82 | + "(number, timestamp)": unique |
| 83 | + on_conflict: |
| 84 | + hash: drop |
| 85 | + "(number, timestamp)": drop |
| 86 | + ``` |
| 87 | + ::: |
| 88 | + |
| 89 | + |
| 90 | + The following Spicepod is invalid because it specifies multiple `on_conflict` targets with `upsert` and `drop`: |
| 91 | + |
| 92 | + :::danger[Invalid] |
| 93 | + ```yaml |
| 94 | + datasets: |
| 95 | + - from: spice.ai/eth.recent_blocks |
| 96 | + name: eth.recent_blocks |
| 97 | + acceleration: |
| 98 | + enabled: true |
| 99 | + engine: sqlite |
| 100 | + primary_key: hash |
| 101 | + indexes: |
| 102 | + "(number, timestamp)": unique |
| 103 | + on_conflict: |
| 104 | + hash: upsert |
| 105 | + "(number, timestamp)": drop |
| 106 | + ``` |
| 107 | + ::: |
| 108 | + |
| 109 | + </div> |
| 110 | + </details> |
| 111 | + |
| 112 | +- **DuckDB Limitations:** |
| 113 | + - DuckDB does not support `upsert` for datasets with List or Map types. |
| 114 | + - Standard indexes unexpectedly act like unique indexes and block updates when `upsert` is configured. |
| 115 | + - <details> |
| 116 | + <summary>Standard indexes blocking updates</summary> |
| 117 | + <div> |
| 118 | + The following Spicepod specifies a standard index on the `number` column, which blocks updates when `upsert` is configured for the `hash` column: |
| 119 | + |
| 120 | + ```yaml |
| 121 | + datasets: |
| 122 | + - from: spice.ai/eth.recent_blocks |
| 123 | + name: eth.recent_blocks |
| 124 | + acceleration: |
| 125 | + enabled: true |
| 126 | + engine: duckdb |
| 127 | + primary_key: hash |
| 128 | + indexes: |
| 129 | + number: enabled |
| 130 | + on_conflict: |
| 131 | + hash: upsert |
| 132 | + ``` |
| 133 | + |
| 134 | + The following error is returned when attempting to upsert data into the `eth.recent_blocks` table: |
| 135 | + |
| 136 | + ```bash |
| 137 | + ERROR runtime::accelerated_table::refresh: Error adding data for eth.recent_blocks: External error: |
| 138 | + Unable to insert into duckdb table: Binder Error: Can not assign to column 'number' because |
| 139 | + it has a UNIQUE/PRIMARY KEY constraint |
| 140 | + ``` |
| 141 | + |
| 142 | + This is a limitation in DuckDB. |
| 143 | + </div> |
| 144 | + </details> |
0 commit comments