Skip to content

[GLUTEN-10215][VL] Check Delta constraints on Velox batches without converting rows - #4

Open
sezruby wants to merge 1 commit into
mainfrom
feat/delta-native-constraints
Open

sezruby wants to merge 1 commit into
mainfrom
feat/delta-native-constraints

Conversation

@sezruby

@sezruby sezruby commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

What changes are proposed in this pull request?

Part of apache#10215 ("Constraint expression needs to offload"). apache#12647 keeps top-level NOT NULL checks native; this PR does the same for every other constraint.

A native Delta write checks its constraints on Velox batches only when every constraint is a top-level NOT NULL. Any other constraint makes the whole write fall back to Delta's row-based DeltaInvariantCheckerExec, which converts every row of every column from columnar to row and back. Delta has two kinds of constraints, NOT NULL and CHECK. CHECK constraints, legacy column invariants (delta.invariants), the CHECK Delta adds for replaceWhere, and the CHECK it adds when a write provides a generated column are all CHECKs. NOT NULL fields of a struct fall back too.

Changes

  • GlutenDeltaReferencedColumnsChecker checks these constraints with Delta's own CheckDeltaInvariant expressions, built and time-resolved as DeltaInvariantCheckerExec builds them. It exports only the columns the checks reference from each Velox batch to Arrow and evaluates the checks on those rows, so a violation raises the same error as Delta. When a CHECK prints a struct, array or map value in its message, the checks run on unsafe copies of the rows, as in Delta, so the value prints the same.
  • GlutenDeltaEmpty2Null: when a write has constraints, Delta puts a ProjectExec of Empty2Null on the string partition columns (TransactionalWrite.convertEmptyToNullIfNeeded). Delta adds it after Gluten's rules have run, so it stays row-based and converts every row too. It now runs in a ProjectExecTransformer; Delta's ProjectExec is kept when the transformer does not validate.
  • Both Delta versions: src-delta33 binds the checks with withBoundReferences (extractors are a map there); src-delta4 uses BindReferences.
  • VeloxDelta.md: the CHECK / NOT NULL row.

A violation is Delta's own exception. With apache#13561, it keeps its type when native code pulls the checked batches, such as the writer's sort on a partitioned write or an optimized write's shuffle, so this change needs no handling of its own for that.

Coverage. Each case ran natively on Spark 3.5.5 / Delta 3.3.2 (Linux x86-64), with native write on and the native libraries of a 2026-11-27 nightly bundle (which includes apache#13561), with and without this change. "Checker" is the checker in the Velox writer's plan.

Write Constraints Checker without this change Checker with it
save(path) append top-level NOT NULL native native
save(path) append CHECK; nested NOT NULL (incl. a NULL parent); provided generated column; legacy column invariant; CHECK on decimal, date, timestamp, TIMESTAMP_NTZ, current_date(), array, map, struct field, boolean, binary, double row checker native, no row conversion
save(path) append, partitioned CHECK, incl. an empty partition string written as NULL row checker and row-based Empty2Null native
save(path) overwrite, replaceWhere CHECK, replaceWhere predicate row checker native
UPDATE, DELETE (rewrite), with and without change data feed CHECK, generated column row checker native
OPTIMIZE CHECK row checker native
optimized write append CHECK row checker native

In every case, a violating write raises the same exception and message as with native write off, with and without this change. SQL INSERT, MERGE and streaming writes are written by Delta's writer even with native write on (see VeloxDelta.md), so Delta checks their constraints, as before. Column-mapped tables on Spark 3.5 fall back to Delta's writer (apache#13383).

Not covered. Once any constraint is not a top-level NOT NULL, all constraints are evaluated on the JVM, including top-level NOT NULLs, and a NOT NULL on a struct field exports the whole top-level struct. Checking NOT NULLs natively and exporting only the columns CHECKs reference would cut this further.

Measured with an earlier revision of this PR, on an 8-executor (4 cores each) Linux cluster, Spark 3.5, 24 GB off-heap per executor. The table has 64 files × 500K rows, a nested schema with ~600 leaf columns, a generated date partition, NOT NULL struct fields and change data feed on. A DELETE rewrites every file:

Rewrite task time Wall time
Vanilla Spark 4,804 s 189 s
Gluten native write, without this change 3,360 s 122 s
Gluten native write, with this change 2,434 s 98 s

How was this patch tested?

  • DeltaNativeWriteSuite (Delta 3.3 and Delta 4) new tests: CHECK constraints unpartitioned and partitioned; nested NOT NULL (with a NULL parent) and generated columns; an empty partition string; DELETE with change data feed; an optimized write; replaceWhere, UPDATE and OPTIMIZE; legacy column invariants; a violation in a later batch of a task; CHECKs on decimal, TIMESTAMP_NTZ, current_date(), array and map columns. Each asserts the checker in the writer's plan and no row conversion of batches, and compares a violation's message with native write off.
  • Run natively on Linux x86-64 for Spark 3.5, with this branch's classes and the native libraries of the 2026-11-27 nightly bundle: DeltaNativeWriteSuite 20/20, GlutenDeltaInvariantCheckerSuite 2/2 and GlutenDeltaNativeConstraintSuite 6/6 pass.
  • With an earlier revision of this PR, run the same way:
    • Negative control: without GlutenDeltaReferencedColumnsChecker, all 9 new constraint tests fail.
    • Gluten's copies of Delta Lake suites DeleteSQLSuite, UpdateSQLSuite, DeltaInsertIntoDataFrameByPathSuite, DeltaColumnDefaultsInsertSuite, OptimizedWritesSuite and DeltaSuite pass (279 tests).
    • Delta Lake 3.3.2's own CheckConstraintsSuite, InvariantEnforcementSuite, GeneratedColumnSuite, TypeWideningConstraintsSuite, TypeWideningGeneratedColumnsSuite, DeltaColumnRenameSuite, DeltaDropColumnSuite and OptimizeGeneratedColumnSuite, with Gluten loaded in every test session and native write on: nothing newly fails compared with the same bundle without the change. Their 51 common failures, all in OptimizeGeneratedColumnSuite, look for Delta's partition filters in a vanilla scan node. Of their native writes with constraints, 46 used the native checkers and 2 used Delta's row checker: writes that omit a NOT NULL column, which Delta rejects with its own error.
  • Main and test code compile, with scalastyle and spotless, for Spark 3.5 / Delta 3.3 and Spark 4.0 / Delta 4. The Delta 4 tests were not run.

🤖 Generated with Claude Code

@sezruby
sezruby force-pushed the feat/delta-native-constraints branch from c8fcf18 to d5ac107 Compare November 26, 2026 18:03
@github-actions github-actions Bot added the VELOX label Nov 26, 2026
…onverting rows

A native Delta write checked its constraints on Velox batches only when every constraint was a
top-level NOT NULL. Any other constraint made the whole write fall back to Delta's row-based
DeltaInvariantCheckerExec, which converts every row of every column from columnar to row and
back. Such constraints include a CHECK constraint, a legacy column invariant, the CHECK that
Delta adds for replaceWhere or when a write provides a generated column, and a NOT NULL field of
a struct.

GlutenDeltaReferencedColumnsChecker now checks these constraints with Delta's own
CheckDeltaInvariant expressions, built and time-resolved as DeltaInvariantCheckerExec builds
them. It exports only the columns the checks reference from each Velox batch to Arrow, and
evaluates the checks on those rows, so a violation raises the same error as Delta. When a CHECK
prints a struct, array or map value in its message, the checks run on unsafe copies of the rows,
as in Delta, so that the value prints the same.

When a write has constraints, Delta also puts a ProjectExec of Empty2Null on its string
partition columns (TransactionalWrite.convertEmptyToNullIfNeeded). Delta adds it after Gluten's
rules have run, so it stays row-based and converted every row as well. GlutenDeltaEmpty2Null
runs that projection in a ProjectExecTransformer, since Velox runs Empty2Null, and keeps Delta's
ProjectExec when the transformer does not validate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sezruby
sezruby force-pushed the feat/delta-native-constraints branch from d5ac107 to b988d72 Compare November 27, 2026 02:54
@github-actions github-actions Bot added the DOCS label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant