Skip to content

[GLUTEN-11912][VL] Translate Velox assert_not_null errors to Spark's NOT_NULL_ASSERT_VIOLATION exception - #13780

Merged
zhztheplayer merged 1 commit into
apache:mainfrom
pedrumj2:gluten-11912-not-null-assert-violation
Nov 26, 2026
Merged

zhztheplayer merged 1 commit into
apache:mainfrom
pedrumj2:gluten-11912-not-null-assert-violation

Conversation

@pedrumj2

@pedrumj2 pedrumj2 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

This PR translates a gluten exception to Spark exception:

  1. Maintain consistency with how exceptions are surfaced to users.
  2. We can avoid the .exclude("some function") in tests
Inserted value Spark 4.2.0 without Gluten Gluten before this change Gluten after this change
1 into INT NOT NULL Inserted Inserted Inserted
NULL into INT NOT NULL NOT_NULL_ASSERT_VIOLATION GlutenException NOT_NULL_ASSERT_VIOLATION

Please see the following discussions #13647, #13685 and facebookincubator/velox#20483

Conclusion can be found here

How was this patch tested?

Reproduction

  1. Clone the repo and check out the base commit:
git clone https://github.com/apache/gluten.git
cd gluten
git checkout 85c89803ed2d050db4052f1813d76711ebe29fb8
  1. Build the native libraries and the Spark 4.2 bundle jar:
docker run --rm -v "$PWD":/work/gluten:z apache/gluten:centos-9-jdk8 bash -c '
  source /opt/rh/gcc-toolset-12/enable
  export VELOX_BUILD_SHARED=ON
  cd /work/gluten
  ./dev/builddeps-veloxbe.sh --run_setup_script=OFF --build_arrow=OFF
  export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
  ./build/mvn clean install -Pbackends-velox -Pspark-4.2 -Pscala-2.13 -Pjava-17 -DskipTests
'
  1. Fetch Spark 4.2.0:
curl -sSL https://archive.apache.org/dist/spark/spark-4.2.0/spark-4.2.0-bin-hadoop3.tgz | tar -xz
  1. Write two queries that insert from a Parquet table into a NOT NULL column. Spark 4.2 checks NOT NULL on file source inserts only with spark.sql.fileSource.insert.enforceNotNull, and the queries turn off ANSI mode, which Gluten falls back to Spark for:
cat > fits.sql <<'EOF'
SET spark.sql.ansi.enabled=false;
SET spark.sql.fileSource.insert.enforceNotNull=true;
DROP TABLE IF EXISTS src;
DROP TABLE IF EXISTS t;
CREATE TABLE src USING parquet AS SELECT 1 AS i;
CREATE TABLE t (i INT NOT NULL) USING parquet;
INSERT INTO t SELECT i FROM src;
SELECT i FROM t;
EOF
cat > null.sql <<'EOF'
SET spark.sql.ansi.enabled=false;
SET spark.sql.fileSource.insert.enforceNotNull=true;
DROP TABLE IF EXISTS src;
DROP TABLE IF EXISTS t;
CREATE TABLE src USING parquet AS SELECT CAST(NULL AS INT) AS i;
CREATE TABLE t (i INT NOT NULL) USING parquet;
INSERT INTO t SELECT i FROM src;
EOF
  1. Run each query with Gluten, and print the exception it fails with and Velox's reason, if any:
docker run --rm -v "$PWD":/work/gluten:z apache/gluten:centos-9-jdk8 bash -c '
  export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark4.2_2.13-linux_amd64-*.jar)
  for q in fits.sql null.sql; do
    spark-4.2.0-bin-hadoop3/bin/spark-sql --master "local[2]" \
      --jars "$BUNDLE" --driver-class-path "$BUNDLE" \
      --conf spark.plugins=org.apache.gluten.GlutenPlugin \
      --conf spark.memory.offHeap.enabled=true \
      --conf spark.memory.offHeap.size=4g \
      --conf spark.shuffle.manager=org.apache.spark.shuffle.sort.ColumnarShuffleManager \
      -f "$q" 2>err.log
    grep -m 1 -E "^org\.apache\.[a-z.]+\.[A-Za-z]+Exception: " err.log
    grep -m 1 "^Reason:" err.log
  done
'

On the base commit, the null fails with a GlutenException that wraps Velox's error:

spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: Null value appeared in non-nullable field

On this pull request's branch, rerunning steps 2 and 5 gives Spark's exception, with Velox's error kept as its cause:

spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 
Reason: Null value appeared in non-nullable field

Spark without Gluten, which is step 5's spark-sql command keeping only --master, fails with the same exception:

spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 

Spark's message continues with the field, i, where Gluten's message has none:

grep -m 1 -A 2 "^org.apache.spark.SparkRuntimeException" err.log
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 
i
If the schema is inferred from a Scala tuple/case class, or a Java bean, please try to use scala.Option[_] or other nullable types (such as java.lang.Integer instead of int/scala.Int). SQLSTATE: 42000

Automated tests

Inside the apache/gluten:centos-9-jdk8 image, with JDK 17 and SPARK_ANSI_SQL_MODE=false, which Gluten's CI sets, these suites pass on Spark 4.0:

SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.0 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark40 -am -DargLine="-Dspark.test.home=/opt/shims/spark40/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.GlutenRuntimeNullChecksV2Writes,org.apache.spark.sql.connector.GlutenDataSourceV2SQLSuiteV1Filter,org.apache.spark.sql.sources.GlutenInsertSuite,org.apache.spark.sql.connector.GlutenGroupBasedUpdateTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedMergeIntoTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedMergeIntoTableUpdateAsDeleteAndInsertSuite,org.apache.spark.sql.connector.GlutenGroupBasedMergeIntoTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedUpdateAsDeleteAndInsertTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedUpdateTableSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
GlutenRuntimeNullChecksV2Writes:
…
- NOT NULL checks for atomic top-level fields (byName)
…
GlutenDataSourceV2SQLSuiteV1Filter:
…
- CreateTableAsSelect: nullable schema
…
GlutenInsertSuite:
…
- SPARK-24583 Wrong schema type in InsertIntoDataSourceCommand
…
GlutenDeltaBasedMergeIntoTableSuite:
…
- merge with NOT NULL checks
…
Tests: succeeded 567, failed 0, canceled 0, ignored 38, pending 0
All tests passed.

Spark 4.1 runs the same command with -Pspark-4.1, gluten-ut/spark41 and /opt/shims/spark41/spark_home/:

Tests: succeeded 860, failed 0, canceled 0, ignored 38, pending 0
All tests passed.

Spark 4.2 runs it with -Pspark-4.2, gluten-ut/spark42 and /opt/shims/spark42/spark_home/:

GlutenInsertSuite:
…
- SPARK-55716: V1 INSERT rejects null into NOT NULL column for file sources
…
GlutenGroupBasedMergeIntoTableSuite:
…
- merge with NOT NULL checks
…
GlutenDeltaBasedUpdateTableSuite:
…
- Gluten - update with constraint violation and transactional checks
…
Tests: succeeded 813, failed 0, canceled 0, ignored 41, pending 0
All tests passed.

On Spark 3.5, GlutenRuntimeNullChecksV2Writes passes with JDK 8:

./build/mvn -ntp -B clean test -Pspark-3.5 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark35 -am -DargLine="-Dspark.test.home=/opt/shims/spark35/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.GlutenRuntimeNullChecksV2Writes -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
Tests: succeeded 8, failed 0, canceled 0, ignored 14, pending 0
All tests passed.

The change also compiles against Spark 3.4:

./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-3.4 -pl gluten-ut/spark34 -am
[INFO] BUILD SUCCESS

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Co-authored with Claude Code (claude-opus-5-5)

Related issue: #11912

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions github-actions Bot added CORE works for Gluten Core VELOX labels Nov 24, 2026
@pedrumj2
pedrumj2 marked this pull request as ready for review November 25, 2026 01:36
…NOT_NULL_ASSERT_VIOLATION exception

## What changes are proposed in this pull request?

This PR translates a gluten exception to Spark exception:
1. Maintain consistency with how exceptions are surfaced to users.
2. We can avoid the ` .exclude("some function")` in tests

| Inserted value | Spark 4.2.0 without Gluten | Gluten before this change | Gluten after this change |
|---|---|---|---|
| `1` into `INT NOT NULL` | Inserted | Inserted | Inserted |
| `NULL` into `INT NOT NULL` | `NOT_NULL_ASSERT_VIOLATION` | `GlutenException` | `NOT_NULL_ASSERT_VIOLATION` |

Please see the following discussions apache#13647, apache#13685 and facebookincubator/velox#20483

Conclusion can be found [here](apache#13639 (comment))

## How was this patch tested?

### Reproduction

1. Clone the repo and check out the base commit:

```bash
git clone https://github.com/apache/gluten.git
cd gluten
git checkout 85c8980
```

2. Build the native libraries and the Spark 4.2 bundle jar:

```bash
docker run --rm -v "$PWD":/work/gluten:z apache/gluten:centos-9-jdk8 bash -c '
  source /opt/rh/gcc-toolset-12/enable
  export VELOX_BUILD_SHARED=ON
  cd /work/gluten
  ./dev/builddeps-veloxbe.sh --run_setup_script=OFF --build_arrow=OFF
  export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
  ./build/mvn clean install -Pbackends-velox -Pspark-4.2 -Pscala-2.13 -Pjava-17 -DskipTests
'
```

3. Fetch Spark 4.2.0:

```bash
curl -sSL https://archive.apache.org/dist/spark/spark-4.2.0/spark-4.2.0-bin-hadoop3.tgz | tar -xz
```

4. Write two queries that insert from a Parquet table into a NOT NULL column. Spark 4.2 checks NOT NULL on file source inserts only with [spark.sql.fileSource.insert.enforceNotNull](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala#L5170-L5176), and the queries turn off ANSI mode, which Gluten falls back to Spark for:

```bash
cat > fits.sql <<'EOF'
SET spark.sql.ansi.enabled=false;
SET spark.sql.fileSource.insert.enforceNotNull=true;
DROP TABLE IF EXISTS src;
DROP TABLE IF EXISTS t;
CREATE TABLE src USING parquet AS SELECT 1 AS i;
CREATE TABLE t (i INT NOT NULL) USING parquet;
INSERT INTO t SELECT i FROM src;
SELECT i FROM t;
EOF
cat > null.sql <<'EOF'
SET spark.sql.ansi.enabled=false;
SET spark.sql.fileSource.insert.enforceNotNull=true;
DROP TABLE IF EXISTS src;
DROP TABLE IF EXISTS t;
CREATE TABLE src USING parquet AS SELECT CAST(NULL AS INT) AS i;
CREATE TABLE t (i INT NOT NULL) USING parquet;
INSERT INTO t SELECT i FROM src;
EOF
```

5. Run each query with Gluten, and print the exception it fails with and Velox's reason, if any:

```bash
docker run --rm -v "$PWD":/work/gluten:z apache/gluten:centos-9-jdk8 bash -c '
  export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark4.2_2.13-linux_amd64-*.jar)
  for q in fits.sql null.sql; do
    spark-4.2.0-bin-hadoop3/bin/spark-sql --master "local[2]" \
      --jars "$BUNDLE" --driver-class-path "$BUNDLE" \
      --conf spark.plugins=org.apache.gluten.GlutenPlugin \
      --conf spark.memory.offHeap.enabled=true \
      --conf spark.memory.offHeap.size=4g \
      --conf spark.shuffle.manager=org.apache.spark.shuffle.sort.ColumnarShuffleManager \
      -f "$q" 2>err.log
    grep -m 1 -E "^org\.apache\.[a-z.]+\.[A-Za-z]+Exception: " err.log
    grep -m 1 "^Reason:" err.log
  done
'
```

On the base commit, the null fails with a [GlutenException](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/gluten-core/src/main/java/org/apache/gluten/exception/GlutenException.java#L19) that wraps Velox's error:

```
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: Null value appeared in non-nullable field
```

On this pull request's branch, rerunning steps 2 and 5 gives Spark's exception, with Velox's error kept as its cause:

```
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 
Reason: Null value appeared in non-nullable field
```

Spark without Gluten, which is step 5's `spark-sql` command keeping only `--master`, fails with the same exception:

```
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
1
spark.sql.ansi.enabled	false
spark.sql.fileSource.insert.enforceNotNull	true
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 
```

Spark's message continues with the field, `i`, where Gluten's message has none:

```bash
grep -m 1 -A 2 "^org.apache.spark.SparkRuntimeException" err.log
```

```
org.apache.spark.SparkRuntimeException: [NOT_NULL_ASSERT_VIOLATION] NULL value appeared in non-nullable field: 
i
If the schema is inferred from a Scala tuple/case class, or a Java bean, please try to use scala.Option[_] or other nullable types (such as java.lang.Integer instead of int/scala.Int). SQLSTATE: 42000
```

### Automated tests

Inside the `apache/gluten:centos-9-jdk8` image, with JDK 17 and `SPARK_ANSI_SQL_MODE=false`, which [Gluten's CI sets](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/.github/workflows/velox_backend_x86.yml#L72-L73), these suites pass on Spark 4.0:

```bash
SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.0 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark40 -am -DargLine="-Dspark.test.home=/opt/shims/spark40/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.GlutenRuntimeNullChecksV2Writes,org.apache.spark.sql.connector.GlutenDataSourceV2SQLSuiteV1Filter,org.apache.spark.sql.sources.GlutenInsertSuite,org.apache.spark.sql.connector.GlutenGroupBasedUpdateTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedMergeIntoTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedMergeIntoTableUpdateAsDeleteAndInsertSuite,org.apache.spark.sql.connector.GlutenGroupBasedMergeIntoTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedUpdateAsDeleteAndInsertTableSuite,org.apache.spark.sql.connector.GlutenDeltaBasedUpdateTableSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
GlutenRuntimeNullChecksV2Writes:
…
- NOT NULL checks for atomic top-level fields (byName)
…
GlutenDataSourceV2SQLSuiteV1Filter:
…
- CreateTableAsSelect: nullable schema
…
GlutenInsertSuite:
…
- SPARK-24583 Wrong schema type in InsertIntoDataSourceCommand
…
GlutenDeltaBasedMergeIntoTableSuite:
…
- merge with NOT NULL checks
…
Tests: succeeded 567, failed 0, canceled 0, ignored 38, pending 0
All tests passed.
```

Spark 4.1 runs the same command with `-Pspark-4.1`, `gluten-ut/spark41` and `/opt/shims/spark41/spark_home/`:

```
Tests: succeeded 860, failed 0, canceled 0, ignored 38, pending 0
All tests passed.
```

Spark 4.2 runs it with `-Pspark-4.2`, `gluten-ut/spark42` and `/opt/shims/spark42/spark_home/`:

```
GlutenInsertSuite:
…
- SPARK-55716: V1 INSERT rejects null into NOT NULL column for file sources
…
GlutenGroupBasedMergeIntoTableSuite:
…
- merge with NOT NULL checks
…
GlutenDeltaBasedUpdateTableSuite:
…
- Gluten - update with constraint violation and transactional checks
…
Tests: succeeded 813, failed 0, canceled 0, ignored 41, pending 0
All tests passed.
```

On Spark 3.5, [GlutenRuntimeNullChecksV2Writes](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/gluten-ut/spark35/src/test/scala/org/apache/spark/sql/GlutenRuntimeNullChecksV2Writes.scala#L28) passes with JDK 8:

```bash
./build/mvn -ntp -B clean test -Pspark-3.5 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark35 -am -DargLine="-Dspark.test.home=/opt/shims/spark35/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.GlutenRuntimeNullChecksV2Writes -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
Tests: succeeded 8, failed 0, canceled 0, ignored 14, pending 0
All tests passed.
```

The change also compiles against Spark 3.4:

```bash
./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-3.4 -pl gluten-ut/spark34 -am
```

```
[INFO] BUILD SUCCESS
```

## Was this patch authored or co-authored using generative AI tooling?

Generated-by: Co-authored with Claude Code (claude-opus-5-5)
@pedrumj2
pedrumj2 force-pushed the gluten-11912-not-null-assert-violation branch from e32b690 to f13fcba Compare November 26, 2026 05:01
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@zhztheplayer
zhztheplayer merged commit 35398b7 into apache:main Nov 26, 2026
43 checks passed
pedrumj2 added a commit to pedrumj2/gluten that referenced this pull request Nov 26, 2026
Merging `main` into this branch resolved its overlap with apache#13780 in `ColumnarBatchOutIterator.java` by dropping five lines: the closing brace of `translateToInvalidBitPositionException`, the `/**` that opens the Javadoc after it, the closing brace of the `bitPositionEx` check, and two blank lines. Without them the file does not compile. This commit puts them back, so the file is `main`'s version plus this pull request's additions.

Generated-by: Co-authored with Claude Code (claude-opus-5-5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CORE works for Gluten Core VELOX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants