Freshness Check¶
Check name: freshness-check · Type: aggregate · Config: FreshnessCheckConfig
Validates that the most recent timestamp in a column is within a configured recency window. Use it to detect stale datasets — feeds that stopped updating or arrived late.
Parameters¶
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
check_id |
str |
yes | — | Unique identifier for this check within the CheckSet. |
column |
str |
yes | — | The timestamp column to evaluate. |
interval |
int |
yes | — | Size of the recency window; must be positive. |
period |
str |
yes | — | Time unit (singular): year, month, week, day, hour, minute, second. |
severity |
Severity |
no | CRITICAL |
CRITICAL fails the whole batch; WARNING only reports. |
Usage¶
Behavior¶
- Compares against
current_timestamp(). The check passes whenmax(column) >= now - interval period. It is evaluated at run time, so the verdict depends on when the pipeline runs. periodis singular. Useday, notdays— an invalid unit raises a validation error at config time.intervalmust be positive, or the config raisesInvalidCheckConfigurationError.- A critical failure fails the batch. A failing
CRITICALaggregate marks every row_dq_passed = False. AWARNINGfailure is reported only. - Result and metrics. Available via
result.aggregate_results; themetricsdict reports the observedmax_timestampand thefreshness_threshold.
Example¶
Requiring data no older than 1 day, on a dataset whose latest timestamp is from 2020, the check fails.
import datetime
from pyspark.sql import SparkSession
from sparkdq.checks import FreshnessCheckConfig
from sparkdq.engine import BatchDQEngine
from sparkdq.management import CheckSet
spark = SparkSession.builder.getOrCreate()
df = spark.createDataFrame([
{"id": 1, "event_ts": datetime.datetime(2020, 1, 1, 0, 0, 0)},
])
check_set = CheckSet().add_check(
FreshnessCheckConfig(check_id="data-fresh", column="event_ts", interval=1, period="day")
)
result = BatchDQEngine(check_set).run_batch(df)
for r in result.aggregate_results:
print(r.check_id, r.passed, r.metrics)
import datetime
import yaml
from pyspark.sql import SparkSession
from sparkdq.engine import BatchDQEngine
from sparkdq.management import CheckSet
spark = SparkSession.builder.getOrCreate()
df = spark.createDataFrame([
{"id": 1, "event_ts": datetime.datetime(2020, 1, 1, 0, 0, 0)},
])
with open("checks.yml") as f:
config = yaml.safe_load(f)
check_set = CheckSet()
check_set.add_checks_from_dicts(config)
result = BatchDQEngine(check_set).run_batch(df)
for r in result.aggregate_results:
print(r.check_id, r.passed, r.metrics)
The aggregate result reports the latest timestamp and the threshold:
Typical use cases¶
- Detect feeds that stopped updating or arrived late.
- Gate downstream jobs on data recency (e.g. "must be < 1 hour old").
- Monitor SLA compliance for periodic loads.
Related checks¶
- Timestamp Max Check — bound timestamps against a fixed instant rather than "now".