Skip to content

[GLUTEN-11912][VL] Translate Velox getbit position errors to Spark's exception - #13782

Open
pedrumj2 wants to merge 3 commits into
apache:mainfrom
pedrumj2:gluten-11912-invalid-bit-position
Open

pedrumj2 wants to merge 3 commits into
apache:mainfrom
pedrumj2:gluten-11912-invalid-bit-position

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
Query Spark 3.5.5 without Gluten Gluten before this change Gluten after this change
getbit(11L, -1) IllegalArgumentException GlutenException IllegalArgumentException
getbit(11L, 64) IllegalArgumentException GlutenException IllegalArgumentException

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 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 two queries that read a bit outside a BIGINT from a Parquet table:
cat > negative.sql <<'EOF'
DROP TABLE IF EXISTS src;
CREATE TABLE src USING parquet AS SELECT 11L AS x, -1 AS pos;
SELECT getbit(x, pos) FROM src;
EOF
cat > too_large.sql <<'EOF'
DROP TABLE IF EXISTS src;
CREATE TABLE src USING parquet AS SELECT 11L AS x, 64 AS pos;
SELECT getbit(x, pos) 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 '
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark3.5_2.12-linux_amd64-*.jar)
  for q in negative.sql too_large.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: (-1 vs. 0) The value of 'pos' argument must be greater than or equal to zero.
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (64 vs. 64) The value of 'pos' argument must not exceed the number of bits in 'x' - 1.

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

java.lang.IllegalArgumentException: Invalid bit position: -1 is less than zero
Reason: (-1 vs. 0) The value of 'pos' argument must be greater than or equal to zero.
java.lang.IllegalArgumentException: Invalid bit position: 64 exceeds the bit upper limit
Reason: (64 vs. 64) The value of 'pos' argument must not exceed the number of bits in 'x' - 1.

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

java.lang.IllegalArgumentException: Invalid bit position: -1 is less than zero
java.lang.IllegalArgumentException: Invalid bit position: 64 exceeds the bit upper limit

Automated tests

Inside the apache/gluten:centos-9-jdk8 image, GlutenSQLQueryTestSuite passes on Spark 3.5, running Spark's own bitwise.sql:

./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.GlutenSQLQueryTestSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
GlutenSQLQueryTestSuite:
…
- bitwise.sql
…
Tests: succeeded 212, failed 0, canceled 0, ignored 104, pending 0
All tests passed.

ScalarFunctionsValidateSuite passes on Spark 3.5:

./build/mvn -ntp -B clean test -Pspark-3.5 -Pbackends-velox -pl backends-velox -am -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.gluten.functions.ScalarFunctionsValidateSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
Tests: succeeded 94, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

The change also compiles against Spark 3.4 and 4.0:

./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-3.4 -pl gluten-ut/spark34 -am
./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-4.0 -Pscala-2.13 -Pjava-17 -pl gluten-ut/spark40 -am -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
[INFO] BUILD SUCCESS
[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

…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

| Query | Spark 3.5.5 without Gluten | Gluten before this change | Gluten after this change |
|---|---|---|---|
| `getbit(11L, -1)` | `IllegalArgumentException` | `GlutenException` | `IllegalArgumentException` |
| `getbit(11L, 64)` | `IllegalArgumentException` | `GlutenException` | `IllegalArgumentException` |

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 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 two queries that read a bit outside a BIGINT from a Parquet table:

```bash
cat > negative.sql <<'EOF'
DROP TABLE IF EXISTS src;
CREATE TABLE src USING parquet AS SELECT 11L AS x, -1 AS pos;
SELECT getbit(x, pos) FROM src;
EOF
cat > too_large.sql <<'EOF'
DROP TABLE IF EXISTS src;
CREATE TABLE src USING parquet AS SELECT 11L AS x, 64 AS pos;
SELECT getbit(x, pos) 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 '
  cd /work/gluten
  BUNDLE=$(ls package/target/gluten-velox-bundle-spark3.5_2.12-linux_amd64-*.jar)
  for q in negative.sql too_large.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: (-1 vs. 0) The value of 'pos' argument must be greater than or equal to zero.
org.apache.gluten.exception.GlutenException: Exception: VeloxUserError
Reason: (64 vs. 64) The value of 'pos' argument must not exceed the number of bits in 'x' - 1.
```

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

```
java.lang.IllegalArgumentException: Invalid bit position: -1 is less than zero
Reason: (-1 vs. 0) The value of 'pos' argument must be greater than or equal to zero.
java.lang.IllegalArgumentException: Invalid bit position: 64 exceeds the bit upper limit
Reason: (64 vs. 64) The value of 'pos' argument must not exceed the number of bits in 'x' - 1.
```

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

```
java.lang.IllegalArgumentException: Invalid bit position: -1 is less than zero
java.lang.IllegalArgumentException: Invalid bit position: 64 exceeds the bit upper limit
```

### Automated tests

Inside the `apache/gluten:centos-9-jdk8` image, [GlutenSQLQueryTestSuite](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/gluten-ut/spark35/src/test/scala/org/apache/spark/sql/GlutenSQLQueryTestSuite.scala#L61) passes on Spark 3.5, running Spark's own [bitwise.sql](https://github.com/apache/spark/blob/v3.5.5/sql/core/src/test/resources/sql-tests/inputs/bitwise.sql):

```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.GlutenSQLQueryTestSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
GlutenSQLQueryTestSuite:
…
- bitwise.sql
…
Tests: succeeded 212, failed 0, canceled 0, ignored 104, pending 0
All tests passed.
```

[ScalarFunctionsValidateSuite](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala#L31) passes on Spark 3.5:

```bash
./build/mvn -ntp -B clean test -Pspark-3.5 -Pbackends-velox -pl backends-velox -am -Dtest=none -DfailIfNoTests=false -DwildcardSuites=org.apache.gluten.functions.ScalarFunctionsValidateSuite -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

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

The change also compiles against Spark 3.4 and 4.0:

```bash
./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-3.4 -pl gluten-ut/spark34 -am
./build/mvn -ntp -B test-compile -Pbackends-velox -Pspark-ut -Pspark-4.0 -Pscala-2.13 -Pjava-17 -pl gluten-ut/spark40 -am -Dspotless.check.skip=true -Dscalastyle.skip=true -Dcheckstyle.skip=true
```

```
[INFO] BUILD SUCCESS
[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

@pedrumj2

Copy link
Copy Markdown
Contributor Author

CC @zhztheplayer @rui-mo when you get the chance can you please help take a look at this PR and these ones too #13782 #13783 #13784 #13781 #13778 #13779 (last one keeps the approach consistent with other tests)

Thanks again!

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

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)
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@zhztheplayer zhztheplayer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrumj2 LGTM

Can we also do a followup to move the exception conversion code to a new file, future enhancement of error code mapping can also happen in that file, so the scope can be made clearer.

@pedrumj2

Copy link
Copy Markdown
Contributor Author

Can we also do a followup to move the exception conversion code to a new file, future enhancement of error code mapping can also happen in that file, so the scope can be made clearer.

Thanks @zhztheplayer sounds good, I'll follow up with this. Would it be alright to do the refactor after merging these PRs? #13779 #13781 #13784 #13783 #13778

(they're all similar error translation PRs)

@felipepessoto

Copy link
Copy Markdown
Contributor

@Abhinav-Agarwal021 is already moving the schema and char/varchar handlers into NativeErrorTranslator.scala in #13697. Could we use that file for the follow-up rather than moving the same code twice?

@Abhinav-Agarwal021, when you rebase, would you be willing to bring over the NOT NULL handler from #13780 and any of Pedrum’s other handlers that have landed by then? Any PRs that land afterward could add their rules directly to the translator. That gives us one place to check the Velox error code first and fall back to message matching where a code isn’t available yet.

@pedrumj2

Copy link
Copy Markdown
Contributor Author

NativeErrorTranslator.scala in #13697. Could we use that file for the follow-up rather than moving the same code twice?

@felipepessoto sounds good, thanks for the heads up. Once that merges I'll move everything into NativeErrorTranslator.scala

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.

3 participants