Skip to content

[Feature Request]: [Python/YAML] Add native Expect / data quality PTransform with tagged DLQ outputs #41223

Todo

Description

@jk-shah

Description

In production data pipelines (e.g. Bronze -> Silver -> Gold), validating records against declarative data quality rules is a fundamental requirement.
Currently in Apache Beam, implementing validation requires writing custom multi-output DoFns and branching logic. In Beam YAML, there is no declarative way to specify data quality rules with automated quarantine actions.

Proposed Architecture

Add an Expect Schema-Aware PTransform in apache_beam.transforms.expect (and register type: Expect in Beam YAML):

pipeline:
  transforms:
    - type: ReadFromCsv
      config: {path: 'gs://bucket/raw/*.csv'}
    - type: Expect
      config:
        rules:
          - name: positive_amount
            expression: 'amount > 0.0'
            on_violation: dlq
          - name: non_null_customer
            expression: 'customer_id IS NOT NULL'
            on_violation: fail
          - name: valid_email
            expression: "email LIKE '%@%'"
            on_violation: drop
        dlq_output: InvalidRecords
    - type: WriteToParquet
      input: Expect
      config: {path: 'gs://bucket/silver/'}
    - type: WriteToJson
      input: Expect.InvalidRecords
      config: {path: 'gs://bucket/dlq/'}

Key Capabilities

  1. Tagged Outputs: Emits Tag("main") for passed records and Tag("dlq") for quarantined records.
  2. Standard Beam Metrics: Emits beam:metric:user:expectation_passed:{rule} and expectation_quarantined:{rule} for real-time monitoring on Dataflow/Flink.
  3. Pluggable Violation Actions: dlq, drop, or fail (aborts worker).

Activity

  1. sakshammalik260-cse commented on Nov 30, 2026

    @sakshammalik260-cse
    Contributor

    .take-issue

  2. sakshammalik260-cse commented on Dec 1, 2026

    @sakshammalik260-cse
    Contributor

    Hi @jk-shah,

    I've completed the initial investigation of the Python SDK and YAML infrastructure for this feature. I've also established a local regression baseline of 911 passing tests, 0 failures, and 8 skipped tests.

    I'm proposing a schema-aware Expect PTransform implemented using a single native Python ParDo, reusing Beam's existing tagged outputs, schema infrastructure, and Metrics API. A thin YAML adapter would expose the transform as type: Expect.

    Before proceeding with implementation, I'd appreciate feedback on a few design decisions:

    1. Expression evaluation

    The existing generic YAML expression infrastructure does not directly support all the proposed SQL-style predicates, particularly IS NOT NULL and LIKE.

    Would a restricted, documented native Python SQL-style predicate evaluator be acceptable, or should this feature reuse Beam SQL through its existing cross-language infrastructure?

    The native approach would avoid arbitrary code execution and additional dependencies.

    2. Multiple rule violations

    I propose evaluating all rules and applying deterministic action precedence:

    fail > dlq > drop

    A rejected record routed to the DLQ would contain metadata for all violated rules.

    3. NULL semantics

    I propose SQL three-valued logic, where only TRUE passes a rule and both FALSE and UNKNOWN are treated as violations.

    4. Output contracts

    Valid records would preserve their original schema. The configurable DLQ output would contain the original record and structured violation metadata.

    For YAML, I propose following the existing conventions for default main-output resolution and ensuring that DLQ outputs are consumed or exported.

    Please let me know whether these approaches align with the intended scope, particularly regarding expression evaluation and DLQ semantics.

    No implementation changes have been made yet. I'm happy to adjust the design based on feedback before proceeding.

    Thanks!

  3. sakshammalik260-cse commented on Dec 1, 2026

    @sakshammalik260-cse
    Contributor

    Hi maintainers,

    I've been working on a provisional implementation of the native Python/YAML Expect transform for this issue.

    The current implementation supports:

    • Schema-aware SQL-style Boolean expectations.
    • Per-rule dlq, drop, and fail actions.
    • Configurable tagged dead-letter outputs.
    • Preservation of the valid input schema.
    • Structured violation metadata.
    • Per-rule pass and quarantine counters using Beam's standard Metrics API.
    • Native Beam YAML integration.

    Before opening a PR, I'd appreciate feedback on a few design decisions:

    1. Expression evaluation: Would a restricted, dependency-free Python SQL-style predicate evaluator be acceptable, or would you prefer integration with existing Beam SQL or YAML expression infrastructure?

    2. Multiple violations:The prototype evaluates all rules and applies fail > dlq > drop precedence. Does this match the intended behavior?

    3. DLQ schema: The prototype emits one record containing the original element, structured violation details, a summary message, and an empty stack field for compatibility with existing YAML error conventions.

    4. Metrics: The prototype uses the standard Beam Metrics API with the namespace apache_beam.transforms.expect and counter names expectation_passed:{rule} and expectation_quarantined:{rule}. Additional diagnostic and record-routing counters are currently included.

    5. YAML output handling: The valid output is named good, with a configurable DLQ tag. The names good and output are reserved to avoid conflicts with YAML main-output resolution.

    6. Resource limits: Individual expressions have bounded parsing and evaluation costs. Would maintainers prefer fixed aggregate rule-count and configuration limits, configurable limits, or documented pipeline-author responsibility?

    The prototype has passed targeted regression tests and local execution checks, including embedded, threaded, and subprocess worker modes.

    I'd be happy to adjust or simplify the implementation based on your feedback before submitting it for formal review.

    Thank you!

  4. sakshammalik260-cse commented on Dec 5, 2026

    @sakshammalik260-cse
    Contributor

    .free-issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions