Validation¶
validation
¶
Checks that confirm a table's declarations hold against its data.
Nothing here runs unless the caller asks, through
:meth:tusk.Database.validate or add_table(validate=…).
CHECKS
module-attribute
¶
CHECKS = {
"non_null_primary_key": check_non_null_primary_key,
"unique_primary_key": check_unique_primary_key,
"datetime_row_creation_time": check_datetime_row_creation_time,
}
RELATIONSHIP_CHECKS
module-attribute
¶
RELATIONSHIP_CHECKS = {
"matching_key_dtypes": check_matching_key_dtypes,
"overlapping_keys": check_overlapping_keys,
}
DATABASE_CHECKS
module-attribute
¶
DATABASE_CHECKS = {
"consistent_time_zones": check_consistent_time_zones
}
check_non_null_primary_key
¶
Confirm the declared primary key holds no null.
A table with no primary_key is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
LazyFrame
|
The table's lazy frame. |
required |
schema
|
TableSchema
|
The table's schema, naming the column to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the primary key column holds a null. |
check_unique_primary_key
¶
Confirm the declared primary key holds no repeated value.
A table with no primary_key is skipped. Nulls count as one distinct
value, so repeated nulls fail and a single null passes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
LazyFrame
|
The table's lazy frame. |
required |
schema
|
TableSchema
|
The table's schema, naming the column to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the primary key column holds a repeated value. |
check_datetime_row_creation_time
¶
Confirm the declared row creation time is a Datetime, not a Date.
A table with no row_creation_time is skipped. Reads the schema only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
LazyFrame
|
The table's lazy frame. Unused. |
required |
schema
|
TableSchema
|
The table's schema, naming the column to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the column is not a Datetime. |
check_consistent_time_zones
¶
Confirm every Datetime column in the database agrees on time zone awareness.
Reads the schemas only. Time zone values may differ; only mixing aware with naive fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If some Datetime columns are tz-aware and others naive. |
check_row_creation_time_awareness
¶
Report whether the database's row creation times are tz-aware.
Reads the schemas only. Anything but a Datetime carrying a time zone counts as naive. Time zone values may differ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
awareness |
bool | None
|
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
If some row creation times are tz-aware and others naive. |
check_cutoff_time_zone
¶
Confirm a cutoff matches the database's row creation times in tz awareness.
Reads the schemas only. A timeless database accepts any cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database the cutoff will filter. |
required |
cutoff_time
|
datetime
|
The cutoff. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the cutoff's time zone awareness differs from the row creation times', or if those disagree among themselves. |
check_matching_key_dtypes
¶
Confirm a foreign key has the same dtype as the primary key it points at.
Reads the schemas only. The dtypes must match exactly: pyarrow refuses to join Int64 to Int32, polars refuses Int64 to Float64 and refuses every String/Categorical/Enum crossing, and polars also refuses two Enums whose categories differ. Anything looser passes validation here and then fails the join.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database holding both tables. |
required |
relationship
|
Relationship
|
The link to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the two dtypes differ. |
check_overlapping_keys
¶
Confirm the foreign key matches at least one of the parent's primary keys.
Stops at the first match rather than proving every foreign key resolves. Orphan rows are ordinary in real data; no overlap at all means the link itself is wrong -- the wrong column, or two id spaces that never met.
Null foreign keys are ignored, and a child holding no non-null foreign key is skipped: it has nothing to match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database holding both tables. |
required |
relationship
|
Relationship
|
The link to check. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If no foreign key value appears in the parent. |
validate_table
¶
Run the selected table checks against one table.
Checks run in the order given; the first failure raises
:class:~tusk.exceptions.ValidationError and stops the run. A name that
is not in :data:CHECKS raises :class:ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
LazyFrame
|
The table's lazy frame. |
required |
schema
|
TableSchema
|
The table's schema. |
required |
checks
|
bool | str | Iterable[str]
|
|
True
|
validate_relationship
¶
Run the selected relationship checks against one relationship.
Checks run in the order given; the first failure raises
:class:~tusk.exceptions.ValidationError and stops the run. A name that
is not in :data:RELATIONSHIP_CHECKS raises :class:ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database holding both tables. |
required |
relationship
|
Relationship
|
The link to check. |
required |
checks
|
bool | str | Iterable[str]
|
|
True
|
validate_database
¶
validate_database(
database,
*,
database_checks=True,
table_checks=True,
relationship_checks=True,
)
Run the selected checks against a database.
Table checks run against every table in insertion order, then relationship
checks against every relationship, then database-wide checks once. The
first failure raises :class:~tusk.exceptions.ValidationError and stops
the run. A name outside the registry its selector draws from raises
:class:ValueError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Database
|
The database to check. |
required |
database_checks
|
bool | str | Iterable[str]
|
Selects from :data: |
True
|
table_checks
|
bool | str | Iterable[str]
|
Selects from :data: |
True
|
relationship_checks
|
bool | str | Iterable[str]
|
Selects from :data: |
True
|