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

   ## What changes are proposed in this pull request?
   
   With range partitioning, the JVM computes the partition ids and prepends 
them as a column with `VeloxColumnarBatch::compose`. `compose` rebuilt the row 
type from the child vectors' own types, not from the batch's declared row type.
   
   For Spark `TimestampNTZType`, the two can differ:
   - The row-to-columnar converter produces `TIMESTAMP` vectors, because the 
Arrow schema has no `utc` metadata.
   - The Velox plan declares the column as `TIMESTAMP_UTC`.
   - A pass-through Project keeps the `TIMESTAMP` child vectors under a 
`TIMESTAMP_UTC` row type.
   - `VeloxBatchResizer` creates merged batches from the declared type, so 
their children are `TIMESTAMP_UTC`.
   
   As a result, batches of the same shuffle input could compose to different 
row types. The hash shuffle writer then failed with:
   
   ```
   VeloxRuntimeError: INVALID_STATE
   Expression: rowType_->equivalent(*rv.rowType())
   Function: initFromRowVector
   ```
   
   Changes:
   - `compose()` and `select()` take child types from the source RowVector's 
declared row type.
   - The shuffle writer check message now prints both the first batch's row 
type and the current one. For the failing query it showed `n0_16:TIMESTAMP` vs 
`n0_16:TIMESTAMP UTC`.
   
   ### Minimal reproduction (spark-shell)
   
   Launch `spark-shell` with the Gluten Velox bundle and:
   
   ```
   --master 'local[2]'
   --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
   --conf spark.sql.ansi.enabled=false
   --conf spark.sql.adaptive.enabled=false
   --conf spark.sql.shuffle.partitions=4
   --conf spark.gluten.sql.columnar.range=false
   ```
   
   ```scala
   import java.time.LocalDateTime
   import org.apache.spark.sql.functions._
   
   // A Scala UDF returning TimestampNTZ falls back, so the data enters native 
through RowToVeloxColumnar.
   // asNondeterministic() keeps CollapseProject from merging it into the 
native Project below.
   val toNtz = udf((i: Long) => LocalDateTime.of(2024, 1, 1, 0, 
0).plusSeconds(i)).asNondeterministic()
   
   val df = spark.range(0, 18000, 1, 2).               // 2 partitions x 9000 
rows
     select(col("id"), toNtz(col("id")).as("ts")).     // fallback Project -> 
R2C (TIMESTAMP vectors)
     select(col("ts"), (col("id") * 2).as("k")).       // 
ProjectExecTransformer (declares TIMESTAMP_UTC, passes vectors through)
     orderBy(col("k"))                                 // range shuffle: JVM 
pid + VeloxColumnarBatch::compose
   
   df.collect().length
   ```
   
   Plan:
   
   ```
   Sort
   +- VeloxColumnarToRow
      +- ColumnarExchange rangepartitioning(k ASC NULLS FIRST, 4), 
[shuffle_writer_type=hash]
         +- VeloxResizeBatches
            +- ProjectExecTransformer [ts, (id * 2) AS k]
               +- InputIteratorTransformer
                  +- RowToVeloxColumnar
                     +- Project [id, UDF(id) AS ts]
                        +- Range (0, 18000, step=1, splits=2)
   ```
   
   Each partition is converted into R2C batches of 4096, 4096 and 808 rows. The 
shuffle-input resizer minimum is 0.25 * 4096 = 1024 rows:
   - The two 4096-row batches pass through and compose to `TIMESTAMP`.
   - The 808-row tail is copied into a batch created from the declared type and 
composes to `TIMESTAMP_UTC`.
   
   On a build without this fix (Spark 4.0.1), the query fails with the 
`rowType_->equivalent(*rv.rowType())` check. With 
`spark.gluten.sql.columnar.backend.velox.resizeBatches.shuffleInput=false` it 
returns 18000 rows, because no batch is re-created and every batch consistently 
composes to `TIMESTAMP`. That setting only hides the bug.
   
   Notes on the conditions:
   - ANSI must be off on Spark 4.x, otherwise Gluten falls back entirely.
   - At least two input partitions are needed, otherwise ORDER BY plans no 
range exchange.
   - A partition must contain both batches of at least 1024 rows and batches of 
fewer than 1024 rows.
   
   ## How was this patch tested?
   
   - Added `VeloxColumnarBatchTest.composeAndSelectKeepDeclaredRowType`. It was 
only syntax-checked locally: the local Velox build has testing disabled, so the 
gtest was not run.
   - The spark-shell reproduction above fails on a build without this fix and 
passes with the resizer disabled. It has not yet been re-run on a build that 
includes this fix.
   
   ## Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: 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