[GLUTEN-6101][VL] Enable map_from_arrays function - #12976
Conversation
| } | ||
|
|
||
| test("map_from_arrays honors a lower-case mapKeyDedupPolicy value") { | ||
| withSQLConf(SQLConf.MAP_KEY_DEDUP_POLICY.key -> "last_win") { |
There was a problem hiding this comment.
Spark's SQLConf parses enum configs case-insensitively, so Gluten always receives a normalized enum value from Spark. Testing lower-case inputs in Gluten isn't necessary since that behavior is already guaranteed by Spark.
There was a problem hiding this comment.
@philo-he I see thanks for sharing that. I remove this test.
| "array(l_partkey, l_suppkey, l_linenumber))) as k from lineitem limit 10") { | ||
| checkGlutenPlan[ProjectExecTransformer] | ||
| } | ||
| } |
There was a problem hiding this comment.
Can we consolidate the above two tests into one test with two cases included? One is for EXCEPTION policy, producing exception in duplicate case, and the other is for LAST_WIN policy.
There was a problem hiding this comment.
@philo-he Thanks for the review. Please see the latest changes. Three tests:
- Duplicate + EXCEPTION policy --> exception
- Duplicate + LAST_WIN policy --> no exception
- No Duplicate + EXCEPTION policy --> no exception
## What changes are proposed in this pull request? facebookincubator/velox#18630 implemented the Spark version of `map_from_arrays` in Velox. apache#12968 pulled that commit (a6b9f7754) into the Velox revision this repo pins. This PR drops `map_from_arrays` from [`kBlackList`](https://github.com/apache/gluten/blob/b77fdef08e4a733d1ed424bbf807b2904b92af86/cpp/velox/substrait/SubstraitToVeloxPlanValidator.cc#L59), so a query using it now runs in Velox instead of falling back to the JVM. The function was denylisted by apache#2440 and moved into `kBlackList` by apache#6690. [`GlutenConfig.getNativeSessionConf`](https://github.com/apache/gluten/blob/b77fdef08e4a733d1ed424bbf807b2904b92af86/gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala#L575) already forwards `spark.sql.mapKeyDedupPolicy` to Velox and [`ExpressionMappings`](https://github.com/apache/gluten/blob/b77fdef08e4a733d1ed424bbf807b2904b92af86/gluten-substrait/src/main/scala/org/apache/gluten/expression/ExpressionMappings.scala#L268) already maps the expression, so nothing else had to be wired up. The [scalar function support doc](https://github.com/apache/gluten/blob/b77fdef08e4a733d1ed424bbf807b2904b92af86/docs/velox-backend-scalar-function-support.md#L218) row is updated to mark the function supported. Fixes apache#6101 ## How was this patch tested? ### Local Testing Save this as `verify-map-from-arrays.sh` and run it against a checkout of this branch. It builds the Velox backend and the Spark 3.5 jars in the CI dev image, runs the query in a real Spark session, and asserts on the executed plan. It exits non-zero if the projection falls back to the JVM. ```bash #!/usr/bin/env bash # Verifies end to end that map_from_arrays is offloaded to Velox. # # ./verify-map-from-arrays.sh [path-to-gluten-checkout] # # Env: # DOCKER container runtime (default: docker) # NUM_THREADS build parallelism (default: nproc) # EXTRA_DOCKER_ARGS extra flags for your runtime, e.g. proxy or network settings set -euo pipefail GLUTEN_DIR=$(cd "${1:-$PWD}" && pwd) IMAGE=apache/gluten:centos-9-jdk8 DOCKER=${DOCKER:-docker} THREADS=${NUM_THREADS:-$(nproc)} "$DOCKER" pull "$IMAGE" # shellcheck disable=SC2086 "$DOCKER" run --rm ${EXTRA_DOCKER_ARGS:-} \ -v "$GLUTEN_DIR:/work/gluten" -w /work/gluten \ -e http_proxy -e https_proxy -e no_proxy \ -e NUM_THREADS="$THREADS" \ "$IMAGE" bash -eo pipefail -c ' ./dev/buildbundle-veloxbe.sh --run_setup_script=OFF --build_arrow=OFF --spark_version=3.5 JAR=$(ls /work/gluten/package/target/gluten-velox-bundle-spark3.5_*.jar) SPARK_HOME=/opt/shims/spark35/spark_home # range() keeps the arguments non-literal so Spark cannot constant-fold the call # away before the validator sees it. cat > /tmp/q.sql <<"SQL" CREATE OR REPLACE TEMPORARY VIEW t AS SELECT id AS k, CAST(id AS STRING) AS v FROM range(5); EXPLAIN SELECT map_from_arrays(array(k, k + 1), array(v, concat(v, "x"))) AS m FROM t; SELECT map_from_arrays(array(k, k + 1), array(v, concat(v, "x"))) AS m FROM t; SQL "$SPARK_HOME"/bin/spark-sql --master "local[2]" \ --conf spark.plugins=org.apache.gluten.GlutenPlugin \ --conf spark.driver.extraClassPath="$JAR" \ --conf spark.executor.extraClassPath="$JAR" \ --conf spark.memory.offHeap.enabled=true \ --conf spark.memory.offHeap.size=2g \ --conf spark.shuffle.manager=org.apache.spark.shuffle.sort.ColumnarShuffleManager \ -f /tmp/q.sql 2>&1 | tee /tmp/verify.out sed -n "/== Physical Plan ==/,/^$/p" /tmp/verify.out > /tmp/plan.out grep -q "ProjectExecTransformer \[map_from_arrays" /tmp/plan.out ! grep -qE "^\*?\([0-9]+\) Project \[map_from_arrays" /tmp/plan.out grep -q "{0:\"0\",1:\"0x\"}" /tmp/verify.out ' echo "PASS: map_from_arrays executed in Velox as a ProjectExecTransformer" ``` Output on this branch, from a clean tree. The plan and the rows are contiguous runs from the script's own log; `[...]` marks where Spark's other output was cut. ``` == Physical Plan == VeloxColumnarToRow +- ^(1) ProjectExecTransformer [map_from_arrays(array(k#11L, (k#11L + 1)), array(v#12, concat(v#12, x))) AS m#3] +- ^(1) ProjectExecTransformer [id#13L AS k#11L, cast(id#13L as string) AS v#12] +- ^(1) InputIteratorTransformer[id#13L] +- ArrowColumnarToVeloxColumnar +- OffloadArrowData +- ColumnarRange 0, 5, 1, 2, 5, [id#13L] [...] {0:"0",1:"0x"} {1:"1",2:"1x"} {2:"2",3:"2x"} {3:"3",4:"3x"} {4:"4",5:"4x"} [...] PASS: map_from_arrays executed in Velox as a ProjectExecTransformer ``` The projection carrying the function is a `ProjectExecTransformer`, so it ran in Velox. Restoring the `kBlackList` entry turns that line into `*(1) Project [map_from_arrays(...)]`, which both of the script's plan assertions reject. ### Automated Tests One test in [`ScalarFunctionsValidateSuite`](https://github.com/apache/gluten/blob/b77fdef08e4a733d1ed424bbf807b2904b92af86/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala) covers the offload under both `spark.sql.mapKeyDedupPolicy` values. Under `EXCEPTION` it checks a distinct key array, which Velox builds through its no-shrink path, and a key array repeating `l_orderkey`, which fails the query. Under `LAST_WIN` the repeated key keeps its first position and its last value. Both non-throwing cases are compared against vanilla Spark. ## Was this patch authored or co-authored using generative AI tooling? Generated-by: Co-authored with claude
5fb5b78 to
936b59a
Compare
|
@pedrumj2, please check the test failures which should be related. Thanks. |
What changes are proposed in this pull request?
facebookincubator/velox#18630 implemented the Spark version of
map_from_arraysin Velox.#12968 pulled that commit (a6b9f7754) into the Velox revision this repo pins.
This PR drops
map_from_arraysfromkBlackList, so a query using it now runs in Velox instead of falling back to the JVM. The function was denylisted by #2440 and moved intokBlackListby #6690.GlutenConfig.getNativeSessionConfalready forwardsspark.sql.mapKeyDedupPolicyto Velox andExpressionMappingsalready maps the expression, so nothing else had to be wired up. The scalar function support doc row is updated to mark the function supported.Fixes #6101
How was this patch tested?
Local Testing
Save this as
verify-map-from-arrays.shand run it against a checkout of this branch. It builds the Velox backend and the Spark 3.5 jars in the CI dev image, runs the query in a real Spark session, and asserts on the executed plan. It exits non-zero if the projection falls back to the JVM.Output on this branch, from a clean tree. The plan and the rows are contiguous runs from the script's own log;
[...]marks where Spark's other output was cut.The projection carrying the function is a
ProjectExecTransformer, so it ran in Velox. Restoring thekBlackListentry turns that line into*(1) Project [map_from_arrays(...)], which both of the script's plan assertions reject.Automated Tests
Four tests in
ScalarFunctionsValidateSuitecover the offload, duplicate keys under bothspark.sql.mapKeyDedupPolicyvalues, and the lower-case form of that config.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Co-authored with claude