Skip to content

Harden ReservoirSamplingOpExec.onFinish to not emit null tuples when input size < k #5592

Closed

Description

@Ma77Ball

Task Summary

ReservoirSamplingOpExec allocates a fixed-size reservoir Array[Tuple] of length count (the per-worker share of k). When a worker receives fewer tuples than count (the input stream is smaller than k, or upstream partitioning is skewed so a worker gets fewer than its share), processTuple fills only slots 0 until n and leaves the trailing count - n slots null. onFinish then returns the whole array verbatim (reservoir.iterator), so the iterator yields n real tuples followed by count - n null entries.

This is not currently user-visible: the worker's data-processor loop has a null-guard (DataProcessor.scala, around line 157: if (outputTuple == null) return) that silently skips any null output tuple before it is serialized or passed downstream, so the end-to-end result is correct and downstream operators never see the nulls. The issue is latent: the operator violates the implicit "do not emit null tuples" contract and relies on a distant global guard to clean up after it. If that guard is ever removed, narrowed, or bypassed (a new output path, a batching change, or a code path that inspects tuples before the guard), the nulls would leak into serialization or downstream operators and cause incorrect results or NullPointerExceptions.

Proposed change: emit only the filled prefix in onFinish:

override def onFinish(port: Int): Iterator[TupleLike] = reservoir.iterator.take(n)

take(n) returns all count elements when n >= count (input >= k, behavior unchanged) and exactly the n filled slots when n < count (input < k, no null padding). This keeps the operator's output contract clean without depending on the engine's null guard.

File: common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/reservoirsampling/ReservoirSamplingOpExec.scala (onFinish). Regression coverage: the input size < k case in ReservoirSamplingOpExecSpec, which fails against reservoir.iterator and passes with reservoir.iterator.take(n).

Task Type

  • Refactor / Cleanup
  • DevOps / Deployment / CI
  • Testing / QA
  • Documentation
  • Performance
  • Other

Activity

  1. added theissue type on Jun 9, 2026
  2. Ma77Ball commented on Jun 9, 2026

    @Ma77Ball
    ContributorAuthor

    reserved as introductory task

  3. suyashj1231 commented on Jun 10, 2026

    @suyashj1231
    Contributor

    /take

  4. added a commit that references this issue on Jun 12, 2026
    b876b47
  5. added a commit that references this issue on Jun 12, 2026
    d5f5e12
  6. added a commit that references this issue on Jun 22, 2026
    35df53d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions