Implementation:173TECH Sayn Database ddl tests
| Knowledge Sources | |
|---|---|
| Domains | Data_Quality, DDL_Validation |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for validating column DDL definitions and constructing standard column test queries (unique, not_null, allowed_values) provided by the SAYN framework.
Description
The Database._validate_ddl and Database._construct_tests methods work together to validate DDL and generate test SQL:
_validate_ddl(columns, table_properties, post_hook):
- Validates columns via the
DDLpydantic model - Converts string column names to
{"name": ...}dicts - Checks for duplicate column names
- Transforms test strings into structured test objects
- Returns a formatted DDL dict with
columns,properties,post_hook
_construct_tests(columns, table, schema):
- Renders Jinja templates from
standard_tests.sqlfor each column/test - Builds a UNION query of all test subqueries with
LIMIT 5 - Returns
[test_query, breakdown]— the SQL query and metadata
_format_properties(properties):
- Adds
unique,not_null,allowed_valuesflags based on test types
Usage
Called internally by SqlTask.config(), CopyTask.config(), and load_data() to validate DDL. Called by SqlTask.test() and CopyTask.test() to execute standard column tests.
Code Reference
Source Location
- Repository: SAYN
- File:
sayn/database/__init__.py - Lines: L16-17 (
Hook), L20-37 (Columns,Tests), L40-99 (BaseDDL,DDL), L290-297 (_construct_tests), L239-288 (_construct_tests_template), L299-341 (_validate_ddl,_format_properties), L343-363 (test_problematic_values)
Signature
class DDL(BaseDDL):
columns: List[Union[str, Columns]] = list()
post_hook: List[Hook] = list()
class Columns(BaseModel):
name: str
type: Optional[str]
dst_name: Optional[str]
description: Optional[str]
class Tests(BaseModel):
name: Optional[str]
allowed_values: Optional[List[Union[int, float, bool, str]]]
execute: bool = True
tests: List[Union[str, Tests]] = list()
class Database:
def _validate_ddl(self, columns: list, table_properties: dict, post_hook: list) -> Result[Dict]
def _format_properties(self, properties: dict) -> Result[Dict]
def _construct_tests(self, columns: list, table: str, schema: str = None) -> Result[List]
def _construct_tests_template(self, columns: list, table: str, test_file_name: str, schema: str) -> tuple
def test_problematic_values(self, failed: list, table: str, schema: str) -> str
Import
from sayn.database import Database, DDL, Columns
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| columns | List[Union[str, Mapping]] | No Column definitions (strings or dicts with name, type, tests) | |
| table_properties | Mapping | No Table-level properties | |
| post_hook | List[Mapping] | No Post-execution SQL hooks |
Outputs
| Name | Type | Description |
|---|---|---|
| _validate_ddl returns | Result[Dict] Formatted DDL with columns (name, type, dst_name, unique, not_null, allowed_values, tests), properties, post_hook | |
| _construct_tests returns | None, breakdown: list] |
Usage Examples
Validating DDL
# Called internally by SqlTask.config()
columns = [
{"name": "user_id", "type": "INTEGER", "tests": ["unique", "not_null"]},
{"name": "status", "type": "VARCHAR", "tests": [{"name": "allowed_values", "allowed_values": ["active", "inactive"]}]},
]
result = db._validate_ddl(columns, table_properties={}, post_hook=[])
if result.is_ok:
ddl = result.value
# ddl["columns"] has structured column info with test flags
for col in ddl["columns"]:
print(f"{col['name']}: unique={col['unique']}, not_null={col['not_null']}")
Constructing Standard Tests
# Called internally by SqlTask.test()
result = db._construct_tests(ddl["columns"], table="users", schema="analytics")
if result.is_ok:
test_query, breakdown = result.value
if test_query is not None:
print("Test SQL:", test_query)
print("Breakdown:", breakdown)
else:
print("No tests to execute")