pedrumj2 opened a new pull request, #13780:
URL: https://github.com/apache/gluten/pull/13780

   ## 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](https://github.com/apache/gluten/pull/13639#issuecomment-7111475915)
   
   ## 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 85c89803ed2d050db4052f1813d76711ebe29fb8
   ```
   
   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)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to