4.1 Data Validation, Schema Drift, and Quality Rules
Key Takeaways
- AWS Glue Data Quality uses Data Quality Definition Language (DQDL) declarative rules to assess datasets against completeness, uniqueness, referential integrity, and custom SQL conditions.
- PyDeequ is an open-source Python wrapper for AWS DeeQu built on Apache Spark, enabling programmatic data unit testing, constraint verification suites, constraint suggestion, and metrics repositories for ML pipelines.
- AWS Glue Data Quality computes an automated quality score (0–100%) and publishes CloudWatch metrics (`glue.data.quality.rules.passed` and `glue.data.quality.rules.failed` under the custom `Glue Data Quality` namespace) to monitor data pipelines over time.
- In production MLOps pipelines, failing data quality rules can trigger conditional branching in AWS Glue or SageMaker Pipelines to halt model training and route defective records to an S3 quarantine prefix.
- Schema drift detection in AWS Glue leverages catalog crawlers and Glue ETL schema evaluation to identify newly added, deleted, or type-altered columns before raw data reaches downstream feature stores or training jobs.
Data Validation, Schema Drift, and Quality Rules
In enterprise machine learning systems, data quality directly dictates model performance. While software engineering pipelines fail loudly when encountering syntax or network errors, machine learning systems often fail silently: corrupted, missing, or drifted data will still flow into training jobs and inference endpoints, producing degraded models and unreliable predictions without throwing runtime exceptions. This failure mode is captured by the foundational ML principle: Garbage In, Garbage Out.
For the AWS Certified Machine Learning Engineer — Associate (MLA-C01) examination, you must master automated data quality validation, declarative constraint authoring using AWS Glue Data Quality and Data Quality Definition Language (DQDL), Spark-based data unit testing with PyDeequ, automated quarantine routing, and metric alerting via Amazon CloudWatch.
+---------------------------------------------------------------------------------------------------+
| AWS DATA QUALITY & VALIDATION ARCHITECTURE |
| |
| [Raw S3 Data] ---> [AWS Glue ETL / PySpark] |
| | |
| +---> [DQDL / PyDeequ Evaluation Engine] |
| | |
| +--------------------+--------------------+ |
| | Score >= Threshold | Score < Threshold |
| v v |
| [Curated S3 Bucket] [Quarantine S3 Bucket] |
| | | |
| v v |
| [SageMaker Training Job] [CloudWatch Metric & Alarm] |
| | |
| v |
| [EventBridge Alert / Halt] |
+---------------------------------------------------------------------------------------------------+
1. The Critical Role of Data Quality in Production ML
Data quality degradation manifests in multiple forms across the ML lifecycle:
- Silent Data Degradation: Upstream source schema updates (e.g., changing a currency format or renaming a column) do not crash ingestion jobs if null values are injected by default, but cause models to make erratic inferences.
- Schema Drift: Unannounced modifications to incoming data contracts, including newly introduced categorical values, altered column types, or dropped attributes.
- Distribution Drift: Statistical properties of features change over time (e.g., mean income shifts due to inflation or geographic expansion), eroding the validity of previously trained parameters.
- Target & Feature Leakage: Inadvertently including post-event indicators or corrupted ground truth labels in feature sets.
To prevent silent corruption, ML engineers implement automated quality gates that evaluate datasets against deterministic constraints prior to feature store ingestion or model training.
2. AWS Glue Data Quality & DQDL
AWS Glue Data Quality is a fully managed, serverless capability that measures and monitors data quality for data stored in Amazon S3, AWS Glue Data Catalog tables, and within active AWS Glue ETL PySpark pipelines.
Glue Data Quality uses Data Quality Definition Language (DQDL), a declarative domain-specific language used to express data quality rules in a human-readable format.
+---------------------------------------------------------------------------------------------------+
| DQDL RULE TYPES & SYNTAX |
| |
| Completeness: IsComplete 'customer_id' |
| Uniqueness: IsUnique 'transaction_uuid' |
| Range / Bounds: ColumnValues 'customer_age' between 18 and 120 |
| String Length: ColumnLength 'country_iso_code' == 2 |
| Set Membership: ColumnValues 'subscription_tier' in ['FREE', 'BASIC', 'PREMIUM'] |
| Cardinality: DistinctValuesCount 'device_type' <= 10 |
| Data Type: ColumnDataType 'order_timestamp' = 'TIMESTAMP' |
| Volume Gate: RowCount > 10000 |
| Custom SQL: CustomSql 'SELECT count(*) FROM primary WHERE transaction_amt < 0' = 0 |
+---------------------------------------------------------------------------------------------------+
Core DQDL Rule Categories
| DQDL Rule | Purpose | Example DQDL Syntax |
|---|---|---|
IsComplete | Validates that a column contains no null, empty, or whitespace-only values | IsComplete 'customer_id' |
IsUnique | Asserts that every value in a primary or foreign key column is distinct | IsUnique 'order_id' |
ColumnValues | Enforces value ranges, comparison operators, or set membership | ColumnValues 'age' between 18 and 120<br/>ColumnValues 'state' in ['CA', 'NY', 'TX'] |
ColumnLength | Restricts string length to fixed or bounded character counts | ColumnLength 'zip_code' == 5 |
DistinctValuesCount | Guards against unexpected cardinality explosion in categorical features | DistinctValuesCount 'payment_method' <= 6 |
RowCount | Validates that the ingested dataset meets minimum/maximum record thresholds | RowCount between 10000 and 5000000 |
CustomSql | Executes arbitrary SQL logic across the dynamic frame for complex cross-column validation | CustomSql 'SELECT count(*) FROM primary WHERE discount_pct > 1.0' = 0 |
ReferentialIntegrity | Verifies foreign key consistency between two distinct datasets | ReferentialIntegrity 'user_id' 'reference_db.users.id' >= 0.99 |
Automatic Recommendation Engine
AWS Glue Data Quality includes a built-in recommendation engine. By analyzing historical dataset profiles, Glue automatically generates a starter DQDL ruleset based on observed distributions, null rates, and column types. ML engineers can review, customize, and version-control these generated DQDL rulesets.
Evaluating DQDL within AWS Glue ETL Jobs
In PySpark Glue jobs, DQDL rules are evaluated dynamically using the EvaluateDataQuality transform:
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
from awsglueml.transforms import EvaluateDataQuality
sc = SparkContext()
glueContext = GlueContext(sc)
job = Job(glueContext)
# 1. Load source dynamic frame
source_dyf = glueContext.create_dynamic_frame.from_catalog(
database='ml_lake',
table_name='raw_sensor_telemetry'
)
# 2. Define DQDL ruleset
dqdl_rules = '''
Rules = [
RowCount > 1000,
IsComplete 'sensor_id',
IsComplete 'timestamp',
ColumnValues 'temperature_celsius' between -50.0 and 150.0,
ColumnValues 'vibration_hz' >= 0.0,
CustomSql 'SELECT count(*) FROM primary WHERE sensor_id IS NULL OR timestamp IS NULL' = 0
]
'''
# 3. Evaluate Data Quality with publishing enabled
eval_results = EvaluateDataQuality.apply(
frame=source_dyf,
ruleset=dqdl_rules,
publishing_options={
'dataQualityEvaluationContext': 'SensorETLJob',
'enableDataQualityCloudWatchMetrics': True,
'enableDataQualityResultsS3Publishing': True,
'dataQualityResultsS3Path': 's3://ml-lake-governance/data-quality-results/'
}
)
job.commit()
[!NOTE] Data Quality Score (0–100%): AWS Glue Data Quality aggregates rule evaluations into an overall quality score. If a ruleset contains 10 rules and 9 pass, the resulting Data Quality Score is 90%. You can enforce threshold gates (e.g., fail the job if Score < 95%).
3. PyDeequ & DeeQU on AWS
Deequ is an open-source library built on top of Apache Spark by AWS to define 'unit tests for data'. PyDeequ provides a native Python API for PySpark environments, making it a standard tool for ML engineers running data validation on Amazon EMR, AWS Glue, or Amazon SageMaker Processing jobs.
+---------------------------------------------------------------------------------------------------+
| PYDEEQU CORE MODULE ARCHITECTURE |
| |
| [PyDeequ Core Modules] |
| 1. Suggestion: Suggests constraints automatically based on statistical profiling. |
| 2. Verification: Runs test suites against Spark DataFrames; returns PASS/FAIL states. |
| 3. Metrics Repo: Persists computed metrics over time in S3 / DynamoDB to detect drift. |
| 4. Anomaly: Applies statistical models to metrics history to flag anomalous shifts. |
+---------------------------------------------------------------------------------------------------+
Core PyDeequ Capabilities
1. Verification Suite
The VerificationSuite executes declarative assertions against Spark DataFrames and returns a structured verification report:
from pydeequ.checks import Check, CheckLevel, ConstrainableDataTypes
from pydeequ.verification import VerificationSuite, VerificationResult
# Construct declarative verification check
check = Check(spark, CheckLevel.Error, 'ML Training Dataset Verification')
check_suite = (
check.hasSize(lambda sz: sz >= 5000)
.isComplete('customer_id')
.isUnique('customer_id')
.isContainedIn('subscription_status', ['active', 'churned', 'suspended'])
.hasMin('credit_score', lambda val: val >= 300)
.hasMax('credit_score', lambda val: val <= 850)
.hasCompleteness('annual_income', lambda c: c >= 0.98) # Allow up to 2% missing
.hasCorrelation('monthly_spend', 'credit_limit', lambda corr: corr > 0.3)
)
# Run suite
verification_result = VerificationSuite(spark) \
.onData(df) \
.addCheck(check_suite) \
.run()
# Inspect verification results
result_df = VerificationResult.checkResultsAsDataFrame(spark, verification_result)
result_df.show(truncate=False)
2. Constraint Suggestion
When onboarding a new dataset without predefined constraints, PyDeequ analyzes the DataFrame and suggests suitable rules:
from pydeequ.suggestions import ConstraintSuggestionRunner, DEFAULT
suggestion_result = ConstraintSuggestionRunner(spark) \
.onData(raw_spark_df) \
.addConstraintRule(DEFAULT()) \
.run()
for suggestion in suggestion_result['constraint_suggestions']:
print(f"{suggestion['column_name']}: {suggestion['description']}")
3. Metrics Repository & Anomaly Detection
PyDeequ can store computed metrics in a Metrics Repository (persisted to an S3 bucket in JSON format). Successive pipeline runs compare current metrics against historical distributions to detect anomalies (e.g., sudden jumps in missing value proportions or feature variance changes):
from pydeequ.repository import FileSystemMetricsRepository, ResultKey
metrics_file = 's3://ml-lake-governance/deequ-metrics-repository.json'
repository = FileSystemMetricsRepository(spark, metrics_file)
result_key = ResultKey(spark, ResultKey.current_milli_time(), {'run_id': 'daily_etl_20260816'})
VerificationSuite(spark) \
.onData(df) \
.addCheck(check_suite) \
.useRepository(repository) \
.saveOrAppendResult(result_key) \
.run()
4. Automated Quality Gates, CI/CD & Quarantine Routing
Production ML architectures require automated mechanisms to handle records that violate quality constraints without crashing the entire business pipeline.
+---------------------------------------------------------------------------------------------------+
| DATA ROUTING & QUARANTINE PATTERN |
| |
| [Raw Input Batch] ---> [Data Quality Validation Rule Split] |
| | |
| +----------------+----------------+ |
| | Passed Records | Failed Records |
| v v |
| [Curated Data (S3)] [Quarantine Buffer (S3)] |
| | | |
| v v |
| [SageMaker Pipeline Step] [Glue Error Table & SNS Alert] |
+---------------------------------------------------------------------------------------------------+
The Quarantine Pattern
- Record-Level Splitting: Incoming dynamic frames are split based on rule outcomes. Records passing all validation checks flow to the Curated S3 Prefix (
s3://lake/curated/year=YYYY/month=MM/). - Quarantine Storage: Records failing any check are routed to an Isolated Quarantine Prefix (
s3://lake/quarantine/year=YYYY/month=MM/) tagged with the failed rule identifier, timestamp, and source job ID. - Conditional Job Execution:
- If the percentage of quarantined records exceeds an operational tolerance (e.g., > 3%), the ETL job raises an exception and triggers an alert.
- Downstream SageMaker Pipelines utilize
ConditionStepto verify the Data Quality status before launching model training.
# PySpark dynamic routing pattern
clean_df = df.filter('customer_id IS NOT NULL AND customer_age >= 18 AND customer_age <= 120')
quarantine_df = df.filter('customer_id IS NULL OR customer_age < 18 OR customer_age > 120')
# Write clean data for ML ingestion
clean_df.write.mode('append').parquet('s3://ml-feature-store-lake/curated/customers/')
# Route corrupted data for inspection
quarantine_df.write.mode('append').json('s3://ml-feature-store-lake/quarantine/customers/')
5. CloudWatch Observability & Metric Alarms
When AWS Glue Data Quality executes with CloudWatch publishing enabled, it emits a pair of metrics under the custom CloudWatch namespace titled Glue Data Quality:
+---------------------------------------------------------------------------------------------------+
| CLOUDWATCH METRIC OBSERVABILITY MATRIX |
| |
| Metric Name Namespace Significance |
| -------------------------------- ----------------- ------------------------------------- |
| glue.data.quality.rules.passed Glue Data Quality Count of rules passed per evaluation |
| glue.data.quality.rules.failed Glue Data Quality Count of rules failed per evaluation |
+---------------------------------------------------------------------------------------------------+
Incident Response Architecture
- CloudWatch Alarm: Configured to trigger when
glue.data.quality.rules.failed > 0over a single evaluation period (any failed rule). The per-run Data Quality Score is available in the evaluation results emitted to S3/CloudWatch for threshold-based dashboards. - Amazon EventBridge: Captures the
Glue Data Quality Rule Evaluation Failedstate change event. - Remediation Actions: EventBridge invokes an AWS Lambda function that disables the downstream SageMaker Training pipeline, sends a high-priority Amazon SNS notification to the MLOps on-call team, and creates an incident ticket.
6. Service Comparison: Data Quality on AWS
| Feature | AWS Glue Data Quality | PyDeequ | SageMaker Model Monitor (Data Quality) |
|---|---|---|---|
| Primary Environment | AWS Glue Catalog, Glue ETL, Glue Studio | Apache Spark (EMR, Glue, SageMaker Processing) | SageMaker Real-Time & Batch Endpoints |
| Rule Specification | DQDL (Declarative text syntax) | Python code (pydeequ.checks.Check) | Auto-generated constraints.json from baseline |
| Lifecycle Stage | Ingestion & Data Preparation (Domain 1) | Distributed Feature Engineering (Domain 1) | Post-Deployment Production Serving (Domain 4) |
| Underlying Engine | Managed AWS DeeQu engine | Open-source DeeQu on Spark | SageMaker Managed Container (DeeQu) |
| Infrastructure | Fully Serverless | Spark cluster (EMR / Glue DPUs) | Managed compute provisioned by SageMaker |
An ML engineer is configuring an AWS Glue ETL job to ingest credit card transaction records from Amazon S3. The downstream fraud detection model requires that the 'transaction_id' column contains no nulls and no duplicates, the 'amount' column must be strictly positive, and the 'currency' column must match valid ISO codes ('USD', 'EUR', 'GBP'). Which Data Quality Definition Language (DQDL) ruleset should be supplied to the EvaluateDataQuality transform?
A data engineering team uses Apache Spark on Amazon EMR to construct complex feature sets for an ML forecasting model. The team needs to implement automated unit tests for incoming Spark DataFrames to ensure that feature correlations remain within expected historical bounds and that computed metrics are tracked across daily pipeline runs in Amazon S3 for longitudinal drift detection. Which tool and component should the team implement?
An enterprise ML pipeline runs daily to ingest healthcare claims data. To comply with clinical governance, any incoming dataset where more than 3% of the rows contain invalid patient IDs or out-of-range diagnosis codes must not be ingested into the SageMaker Feature Store. Passing rows must proceed immediately to feature transformation, while invalid rows must be retained for compliance auditing. How should the ML engineer design this pipeline with the LEAST operational overhead?
An ML operations engineer discovers that silent data corruption in an upstream transactional database caused an automated weekly model retraining pipeline to train on an empty feature table, overwriting the production model with a defective version. What monitoring and alerting strategy should the engineer implement to prevent this failure from recurring?