Data validation#
Functions and types on this page are in parcel_runtime::plan. They use DataFusion’s LogicalPlan, SessionContext, TableProvider, and Arrow types.
session#
pub fn session() -> SessionContext;
Creates a DataFusion session with parcel’s built-in functions registered. Use it as the starting point when registering object stores or other session resources.
validate and validate_in#
pub async fn validate(
c: &Compilation,
validation_plan: LogicalPlan,
data: Arc<dyn TableProvider>,
) -> Result<Verdict>;
pub async fn validate_in(
ctx: &SessionContext,
c: &Compilation,
validation_plan: LogicalPlan,
data: Arc<dyn TableProvider>,
) -> Result<Verdict>;
Parameter |
Meaning |
|---|---|
|
Compilation that produced the validation plan. |
|
Plan to execute, normally |
|
Table provider supplying the contract’s columns. Shared through |
|
Existing session with parcel functions registered; used only by |
Both bind the plan to data, execute it, and read its summary row into a Verdict. validate creates its own session. validate_in uses the supplied session, including its registered object stores.
A failed data quality requirement is returned as a verdict with valid: false. Input or execution failures return RuntimeError.
Example#
This helper accepts an already compiled contract and a DataFusion table provider:
use std::sync::Arc;
use datafusion::datasource::TableProvider;
use parcel_core::Compilation;
use parcel_runtime::plan::{self, Verdict};
async fn check_table(
artifacts: &Compilation,
data: Arc<dyn TableProvider>,
) -> plan::Result<Verdict> {
plan::validate(artifacts, artifacts.validation.plan.clone(), data).await
}
Verdict#
All fields are public. The type implements Clone, Debug, PartialEq, and Serialize.
Field |
Type |
Meaning |
|---|---|---|
|
|
Contract name. |
|
|
Hash of the contract definition. |
|
|
Hash identifying this compilation. |
|
|
Whether the dataset passes requirements that can invalidate it. |
|
|
IDs of breached requirements. |
|
|
Number of rows checked. |
|
|
Assertion ID to failing-row count. |
|
|
Data-only guarantee ID to its Boolean result. |
|
|
Computed statistics keyed by output-column name; exact decimals are strings. |
selectivity#
pub async fn selectivity(
c: &Compilation,
data: Arc<dyn TableProvider>,
caller: &Caller,
) -> Result<usize>;
Counts rows in data that pass the compiled admit rules and assertions with on_fail: drop, using the supplied caller. c provides the rules and enrichment expressions. Returns the count, not the rows. Call refusal first when decide rules must also be tested; selectivity does not evaluate them.
Plan and batch helpers#
Signature |
Parameters and result |
|---|---|
|
Replaces the plan’s placeholder scan with |
|
Adds calculated |
|
Selects and casts columns in |
|
Creates an unqualified column expression preserving the supplied name. |
|
Converts a DataFusion scalar to the JSON representation used for statistics. |
|
Converts decimal text to a scaled integer; |
dataset_value#
pub fn dataset_value(
c: &Compilation,
stats: &BTreeMap<String, serde_json::Value>,
written_at: chrono::DateTime<chrono::Utc>,
contract_hash: &str,
) -> cel::Value;
Builds the dataset namespace for rule evaluation. c defines the required statistics and types, stats supplies their values (normally from Verdict.stats), and the remaining arguments supply the data’s write time and contract hash. Missing or unconvertible statistics are omitted from the resulting CEL value.
RuntimeError and Result#
pub enum RuntimeError {
Invalid(String),
DataFusion(datafusion::error::DataFusionError),
}
pub type Result<T> = std::result::Result<T, RuntimeError>;
Invalid describes invalid inputs or parcel evaluation failures. DataFusion wraps planning and execution failures. Both implement Display and std::error::Error.