Skip to content

[GLUTEN-12238][VL] Translate Velox invalid bitmap position errors to Spark's INVALID_BITMAP_POSITION exception - #13778

Open
pedrumj2 wants to merge 1 commit into
apache:mainfrom
pedrumj2:gluten-12238-invalid-bitmap-position-exception
Open

pedrumj2 wants to merge 1 commit into
apache:mainfrom
pedrumj2:gluten-12238-invalid-bitmap-position-exception

Conversation

@pedrumj2

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
Position Spark 3.5.5 without Gluten Gluten before this pull request Gluten after this pull request
32768 SparkArrayIndexOutOfBoundsException GlutenException wrapping Velox's (32768 vs. 32768) Bitmap position out of bounds GlutenException wrapping Spark's SparkArrayIndexOutOfBoundsException
-1 SparkArrayIndexOutOfBoundsException GlutenException wrapping Velox's (-1 vs. 0) Bitmap position out of bounds GlutenException wrapping Spark's SparkArrayIndexOutOfBoundsException

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

Conclusion can be found here

Fixes #12238

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 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
  ./build/mvn clean install -Pbackends-velox -Pspark-3.5 -DskipTests
'
  1. Fetch Spark 3.5.5:
curl -sSL https://archive.apache.org/dist/spark/spark-3.5.5/spark-3.5.5-bin-hadoop3.tgz | tar -xz
  1. Write Spark's two test queries:
cat > out_of_bounds.sql <<'EOF'
SELECT bitmap_construct_agg(col) FROM VALUES (32768) AS tab(col);
EOF
cat > negative.sql <<'EOF'
SELECT bitmap_construct_agg(col) FROM VALUES (-1) AS tab(col);
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 '
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark3.5_2.12-linux_amd64-*.jar)
  for q in out_of_bounds.sql negative.sql; do
    spark-3.5.5-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 "^Job aborted" err.log | sed "s/.*executor driver): //"
    grep -m 1 "^Reason:" err.log
  done
'

On the base commit, both queries fail with a GlutenException that wraps Velox's error:

org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (32768 vs. 32768) Bitmap position out of bounds
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (-1 vs. 0) Bitmap position out of bounds

On this pull request's branch, rerunning steps 2 and 5 gives a GlutenException that wraps Spark's exception, with Velox's error kept as the cause of Spark's exception:

org.apache.gluten.exception.GlutenException: org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position 32768 is out of bounds. The bitmap has 32768 bits (4096 bytes).
Reason: (32768 vs. 32768) Bitmap position out of bounds
org.apache.gluten.exception.GlutenException: org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position -1 is out of bounds. The bitmap has 32768 bits (4096 bytes).
Reason: (-1 vs. 0) Bitmap position out of bounds

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

org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position 32768 is out of bounds. The bitmap has 32768 bits (4096 bytes).
org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position -1 is out of bounds. The bitmap has 32768 bits (4096 bytes).

Automated tests

Inside the apache/gluten:centos-9-jdk8 image, GlutenQueryExecutionErrorsSuite and GlutenBitmapExpressionsQuerySuite pass on Spark 3.5:

./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.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
GlutenQueryExecutionErrorsSuite:
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
GlutenBitmapExpressionsQuerySuite:
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 50, failed 0, canceled 0, ignored 3, pending 0
All tests passed.

On Spark 4.0, 4.1 and 4.2, the same suites pass with JDK 17 and SPARK_ANSI_SQL_MODE=false, which Gluten's CI sets:

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.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 64, failed 0, canceled 0, ignored 3, pending 0
All tests passed.
SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.1 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark41 -am -DargLine="-Dspark.test.home=/opt/shims/spark41/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 68, failed 0, canceled 0, ignored 3, pending 0
All tests passed.
SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.2 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark42 -am -DargLine="-Dspark.test.home=/opt/shims/spark42/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 68, failed 0, canceled 0, ignored 3, 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)

…Spark's INVALID_BITMAP_POSITION 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

| Position | Spark 3.5.5 without Gluten | Gluten before this pull request | Gluten after this pull request |
|---|---|---|---|
| 32768 | `SparkArrayIndexOutOfBoundsException` | `GlutenException` wrapping Velox's `(32768 vs. 32768) Bitmap position out of bounds` | `GlutenException` wrapping Spark's `SparkArrayIndexOutOfBoundsException` |
| -1 | `SparkArrayIndexOutOfBoundsException` | `GlutenException` wrapping Velox's `(-1 vs. 0) Bitmap position out of bounds` | `GlutenException` wrapping Spark's `SparkArrayIndexOutOfBoundsException` |

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

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

Fixes apache#12238

## 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 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
  ./build/mvn clean install -Pbackends-velox -Pspark-3.5 -DskipTests
'
```

3. Fetch Spark 3.5.5:

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

4. Write Spark's two test queries:

```bash
cat > out_of_bounds.sql <<'EOF'
SELECT bitmap_construct_agg(col) FROM VALUES (32768) AS tab(col);
EOF
cat > negative.sql <<'EOF'
SELECT bitmap_construct_agg(col) FROM VALUES (-1) AS tab(col);
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 '
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark3.5_2.12-linux_amd64-*.jar)
  for q in out_of_bounds.sql negative.sql; do
    spark-3.5.5-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 "^Job aborted" err.log | sed "s/.*executor driver): //"
    grep -m 1 "^Reason:" err.log
  done
'
```

On the base commit, both queries fail 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:

```
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (32768 vs. 32768) Bitmap position out of bounds
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (-1 vs. 0) Bitmap position out of bounds
```

On this pull request's branch, rerunning steps 2 and 5 gives a `GlutenException` that wraps Spark's exception, with Velox's error kept as the cause of Spark's exception:

```
org.apache.gluten.exception.GlutenException: org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position 32768 is out of bounds. The bitmap has 32768 bits (4096 bytes).
Reason: (32768 vs. 32768) Bitmap position out of bounds
org.apache.gluten.exception.GlutenException: org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position -1 is out of bounds. The bitmap has 32768 bits (4096 bytes).
Reason: (-1 vs. 0) Bitmap position out of bounds
```

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

```
org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position 32768 is out of bounds. The bitmap has 32768 bits (4096 bytes).
org.apache.spark.SparkArrayIndexOutOfBoundsException: [INVALID_BITMAP_POSITION] The 0-indexed bitmap position -1 is out of bounds. The bitmap has 32768 bits (4096 bytes).
```

### Automated tests

Inside the `apache/gluten:centos-9-jdk8` image, [GlutenQueryExecutionErrorsSuite](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/gluten-ut/spark35/src/test/scala/org/apache/spark/sql/errors/GlutenQueryExecutionErrorsSuite.scala) and [GlutenBitmapExpressionsQuerySuite](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/gluten-ut/spark35/src/test/scala/org/apache/spark/sql/GlutenBitmapExpressionsQuerySuite.scala#L28) pass on Spark 3.5:

```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.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
GlutenQueryExecutionErrorsSuite:
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
GlutenBitmapExpressionsQuerySuite:
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 50, failed 0, canceled 0, ignored 3, pending 0
All tests passed.
```

On Spark 4.0, 4.1 and 4.2, the same suites pass 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):

```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.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 64, failed 0, canceled 0, ignored 3, pending 0
All tests passed.
```

```bash
SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.1 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark41 -am -DargLine="-Dspark.test.home=/opt/shims/spark41/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 68, failed 0, canceled 0, ignored 3, pending 0
All tests passed.
```

```bash
SPARK_ANSI_SQL_MODE=false ./build/mvn -ntp -B clean test -Pspark-4.2 -Pscala-2.13 -Pjava-17 -Pbackends-velox -Pspark-ut -pl gluten-ut/spark42 -am -DargLine="-Dspark.test.home=/opt/shims/spark42/spark_home/" -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.spark.sql.errors.GlutenQueryExecutionErrorsSuite,org.apache.spark.sql.GlutenBitmapExpressionsQuerySuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
…
- INVALID_BITMAP_POSITION: position out of bounds
…
- INVALID_BITMAP_POSITION: negative position
…
- bitmap_construct_agg routes to native
…
Tests: succeeded 68, failed 0, canceled 0, ignored 3, 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)
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

This branch has not been deployed

No deployments
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.

[VL] Translate Velox bitmap_construct_agg exceptions to Spark-native exception types

1 participant