Repository navigation
Commit feed6c1
committed
[SPARK-58963][ML][SQL] Add internal ML-specific vector posexplode function
### What changes were proposed in this pull request?
This PR adds an internal ML-specific `vector_posexplode` generator expression for MLlib vector
columns.
The Catalyst expression is registered as an internal expression and operates on the SQL struct
representation produced by `unwrap_udt`:
```scala
struct<type:tinyint,size:int,indices:array<int>,values:array<double>>
```
The ML-side helper is `private[ml]` and wraps vector UDT columns by calling `unwrap_udt` before
invoking the internal expression, so it can be used by Spark ML internals with both
`org.apache.spark.ml.linalg.VectorUDT` and `org.apache.spark.mllib.linalg.VectorUDT` columns.
The generator emits `(index, value)` rows with two modes:
* `sparse` emits nonzero vector entries.
* `dense` emits every vector entry, including zeros.
For each non-null vector, it first emits a marker row with index `-1 - vector.size` and value
`Double.NaN`. Null vectors emit no rows.
### Why are the changes needed?
Spark SQL generator functions such as `posexplode` only accept arrays and maps. Spark ML internals
sometimes need to explode vector columns into index-value rows while preserving sparse vectors
without densifying through `vector_to_array`.
This adds a vector-aware internal generator that works from the shared vector SQL representation
and keeps the API surface internal to ML.
### Does this PR introduce _any_ user-facing change?
No. The Scala helper is `private[ml]`, and the Catalyst expression is registered with
`registerInternalExpression`. This is intended only for Spark ML internals.
### How was this patch tested?
Added coverage in `FunctionsSuite` for:
* `spark.ml` and `spark.mllib` vector UDT columns
* dense vectors
* sparse vectors with explicit zero values
* null vectors
* empty sparse and dense vectors
* `dense` and `sparse` modes
* output schema
Also ran:
```bash
build/sbt "mllib/Test/compile"
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)
Closes #58194 from zhengruifeng/vector-posexplode-dev5.
Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>1 parent 06017e3 commit feed6c1
4 files changed
Lines changed: 387 additions & 1 deletion
File tree
- mllib/src
- main/scala/org/apache/spark/ml
- test/scala/org/apache/spark/ml
- sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst
- analysis
- expressions/ml
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
47 | 62 | | |
48 | 63 | | |
49 | 64 | | |
| |||
Lines changed: 79 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
66 | 73 | | |
67 | 74 | | |
68 | 75 | | |
| |||
137 | 144 | | |
138 | 145 | | |
139 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
140 | 218 | | |
141 | 219 | | |
142 | 220 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
1137 | 1138 | | |
1138 | 1139 | | |
1139 | 1140 | | |
| 1141 | + | |
1140 | 1142 | | |
1141 | 1143 | | |
1142 | 1144 | | |
| |||
0 commit comments