Skip to main content
Version: 1.33.0 (latest) View Markdown

Data quality lifecycle

warning

This feature is in public preview

Data can be validated at several stages of a pipeline run. The stage determines three things that are hard to change later:

  • the input data that a check can see: a single record, the rows of the current load, or the full dataset with its history
  • the compute resources that are used: the machine that runs the pipeline or the destination
  • the actions that are possible after a failed check: reject or repair a record before it is stored, keep a whole load out of the dataset, or report on data that is already there

dlt distinguishes two kinds of data. Source data is the data as it arrives from the source, before it is normalized. Destination data is the normalized data, before or after it is loaded. Source data is validated record by record while the pipeline extracts and normalizes it. Destination data is validated with checks and metrics, which describe whole tables.

The table lists the stages in the order in which a pipeline passes through them:

StageData that is validatedMechanismStatus
Source data validationRecords, during extract and normalizeSchema contracts, Pydantic models, schema hintsAvailable in dlt
Pre-load (in-memory)The load packages of the current loadChecks and metricsWork in progress
Pre-load (staging)The current load in a staging datasetChecks and metricsWork in progress
Post-loadThe full dataset on the destinationChecks and metrics with dq.run()Available

Source data validation​

The first place to catch bad data is the pipeline itself. While dlt extracts and normalizes source data, it controls the shape of the data and validates each record, before anything is written to the destination:

  • Schema contracts control how the schema is allowed to evolve. For tables, columns and data types you decide whether a change is accepted (evolve), raises an error (freeze), or whether the offending row or value is dropped (discard_row, discard_value).
  • Pydantic models validate the content of each record: required fields, formats, value ranges and any rule that you can write as a validator. A model also defines the schema of the table that the resource produces.
  • Schema hints on a resource, such as data types and nullable=False, make dlt coerce values to the declared types and keep nulls out of columns that must not have them.

This validation is stateless and looks at one record at a time. It fits semi-structured data from APIs well, and it is the only stage that can reject or repair a single record before it is stored. By design it cannot answer questions about a table as a whole: whether a key is unique, whether each order has a customer, whether the data is fresh. These questions need aggregates over the destination data and are the domain of checks and metrics in the stages below.

The data quality lifecycle page of the dlt documentation gives an overview of these features, grouped by the dimension of data quality that they address.

Pre-load (in-memory)​

warning

Work in progress. Currently unavailable.

The pre-load execution in memory runs checks with duckdb against the load packages, that is the temporary files stored on the machine that runs dlt. Checks can then trigger actions before the data is loaded into the destination.

note

This is equivalent to using a staging destination that is the local filesystem. This section highlights the trade-offs of this choice.

Properties:

  • Failed records can be dropped or quarantined before load. This works with all write dispositions.
  • Checks only have access to the current load. Checks against the full dataset require communication between the machine that runs the pipeline and the destination.
  • Checks are computed on the machine that runs the pipeline, so its resources must match the size of the load.
  • Data, check results and outcomes can be lost if the runtime is ephemeral (for example, an AWS Lambda timeout). In this case, the pipeline must process the data again.

Pre-load (staging)​

warning

Work in progress. Currently unavailable.

The pre-load execution with a staging dataset runs checks on the destination and triggers actions before the data is loaded into the final dataset. In effect, these are post-load checks on a staging dataset, followed by a second load phase.

info

dlt uses staging datasets for other features such as the merge and replace write dispositions.

Properties:

  • Failed records can be dropped or quarantined before load. This works with all write dispositions.
  • The destination must support staging datasets.
  • Checks have access to the current load. If the staging dataset is on the same destination as the final dataset, checks can also access the full dataset. If it is on a different destination, they cannot without moving data between the two.
  • Checks are computed on the staging destination. This scales well with the size of the data and the complexity of the checks.
  • Data, check results and outcomes can be kept safely in the staging dataset until they are reviewed. This helps human-in-the-loop workflows, because the pipeline does not have to process the data again.

Post-load​

The post-load execution is the simplest option, and the one that dq.run() implements today. The pipeline goes through Extract -> Normalize -> Load as usual. Then the checks are executed on the destination.

Properties:

  • Failed records can't be dropped or quarantined before load. All records are written first, then checked, then handled. Removing bad records afterwards is practical only with write_disposition="append" or on destinations that support snapshots (for example iceberg, ducklake).
  • Checks have access to the full dataset. This includes current and past loads and the internal dlt tables.
  • Checks are computed directly on the destination. This scales well with the size of the data and the complexity of the checks.
  • Results and outcomes are stored in the same dataset. No data movement is required.

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.