Skip to content

[GLUTEN-11622][VL] Tag TimestampNTZ Arrow fields so Velox imports them as TIMESTAMP_UTC - #13756

Open
WangGuangxin wants to merge 2 commits into
apache:mainfrom
WangGuangxin:fix-range-shuffle-ntz-rowtype
Open

WangGuangxin wants to merge 2 commits into
apache:mainfrom
WangGuangxin:fix-range-shuffle-ntz-rowtype

Conversation

@WangGuangxin

@WangGuangxin WangGuangxin commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

The Substrait plan maps Spark TimestampNTZType to Velox TIMESTAMP_UTC. Velox imports an Arrow timestamp as TIMESTAMP_UTC only when the field carries velox.logical_timestamp=utc (facebookincubator/velox#17116). Otherwise it imports the field as TIMESTAMP.

Arrow schemas built on the JVM by SparkArrowUtil.toArrowField write NTZ as a timestamp with no timezone and no tag. Row-to-columnar, shuffle read and broadcast therefore produce TIMESTAMP vectors for NTZ columns, while the plan declares TIMESTAMP_UTC.

With range partitioning this fails the hash shuffle writer:

  • A pass-through Project keeps the row-to-columnar TIMESTAMP child vectors.
  • VeloxBatchResizer rebuilds short batches from the declared type, so their children are TIMESTAMP_UTC.
  • The JVM prepends the partition id with VeloxColumnarBatch::compose, which takes the row type from the child vectors. Batches of the same stream then reach the writer with different row types:
VeloxRuntimeError: INVALID_STATE
Reason: Shuffle input row type changed between batches. First batch: ROW<n0_2:TIMESTAMP,n0_3:BIGINT>, current batch: ROW<n0_2:TIMESTAMP UTC,n0_3:BIGINT>
Expression: rowType_->equivalent(*rv.rowType())
Function: initFromRowVector

Changes:

  • SparkArrowUtil.toArrowField tags NTZ fields with velox.logical_timestamp=utc, including fields nested in arrays, structs and maps.
  • ArrowUtil.toArrowSchema, which ColumnarBatches.load uses, keeps the field metadata Velox exported instead of rebuilding it from the Spark type. Velox exports TIMESTAMP without a timezone, and fromArrowField maps that to TimestampNTZType. Rebuilding with the new tag would therefore turn a TimestampType column into TIMESTAMP_UTC.
  • The shuffle writer check message now prints both row types.

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
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
  select(col("ts"), (col("id") * 2).as("k")).       // ProjectExecTransformer, passes vectors through
  orderBy(col("k"))                                 // range shuffle: JVM pid + VeloxColumnarBatch::compose

df.collect().length

Each partition is converted into R2C batches of 4096, 4096 and 808 rows. The shuffle-input resizer passes the two full batches through and rebuilds the 808-row tail from the declared type.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

Related issue: #11622

Range partitioning prepends a JVM-computed partition id column via
VeloxColumnarBatch::compose, which rebuilt the row type from the child
vectors' own types. For Spark TimestampNTZType, the row-to-columnar
converter produces TIMESTAMP vectors while the Velox plan declares
TIMESTAMP_UTC, and VeloxBatchResizer creates merged batches from the
declared type. Batches of one stream could therefore compose to different
row types, failing the hash shuffle writer check
rowType_->equivalent(*rv.rowType()).

Take child types from the source RowVector's declared row type in
compose() and select(), and include both row types in the shuffle writer
check message.
@WangGuangxin
WangGuangxin force-pushed the fix-range-shuffle-ntz-rowtype branch from 1072b43 to 4ff558d Compare November 24, 2026 09:36
@github-actions github-actions Bot added the VELOX label Nov 24, 2026
@WangGuangxin
WangGuangxin requested review from marin-ma, philo-he and zhztheplayer and removed request for marin-ma and philo-he November 24, 2026 12:03

@rui-mo rui-mo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@WangGuangxin Thanks for the fix. This looks a gap needs to be fixed for supporting TimestampNTZ type.

facebookincubator/velox#17116 tags schemas that Velox itself exports, and Velox imports an Arrow timestamp as TIMESTAMP_UTC when the field carries velox.logical_timestamp=utc.

The JVM builds row-to-columnar schema in SparkArrowUtil.toArrowField, which writes TimestampNTZType as a timestamp with no timezone and no tag, so Velox imports it as TIMESTAMP. The Substrait plan maps NTZ to TIMESTAMP_UTC, so mixed batches fail rowType_->equivalent in the shuffle writer; other JVM-built schemas such as shuffle read and broadcast have the same gap.

The suggested fix would be two JVM-only changes. First, SparkArrowUtil.toArrowField adds the utc tag to NTZ fields, including nested ones. Second, ArrowUtil.toArrowSchema, which ColumnarBatches.load uses, keeps the metadata Velox exported.

Do you think this make sense?

…_UTC

The Substrait plan maps Spark TimestampNTZType to Velox TIMESTAMP_UTC,
and Velox imports an Arrow timestamp as TIMESTAMP_UTC only when the field
carries velox.logical_timestamp=utc. Arrow schemas built on the JVM had
no such tag, so row-to-columnar, shuffle read and broadcast produced
TIMESTAMP vectors for NTZ columns. With range partitioning, batches
passed through from row-to-columnar and batches rebuilt by the
shuffle-input resizer from the declared type then reached the hash
shuffle writer with different row types and failed
rowType_->equivalent(*rv.rowType()).

- SparkArrowUtil.toArrowField tags NTZ fields, including nested ones.
- ArrowUtil.toArrowSchema, used by ColumnarBatches.load, keeps the field
  metadata Velox exported instead of rebuilding it from the Spark type.
  Velox exports TIMESTAMP without a timezone, which fromArrowField maps
  to TimestampNTZType, so rebuilding would tag TIMESTAMP columns as
  TIMESTAMP_UTC.

Revert the VeloxColumnarBatch compose()/select() change from the
previous commit, which only covered the symptom. Keep the more detailed
shuffle writer check message.
@WangGuangxin WangGuangxin changed the title [GLUTEN-11622][VL] Keep declared row type when composing batches for range shuffle [GLUTEN-11622][VL] Tag TimestampNTZ Arrow fields so Velox imports them as TIMESTAMP_UTC Nov 25, 2026
@WangGuangxin

Copy link
Copy Markdown
Contributor Author

@WangGuangxin Thanks for the fix. This looks a gap needs to be fixed for supporting TimestampNTZ type.

facebookincubator/velox#17116 tags schemas that Velox itself exports, and Velox imports an Arrow timestamp as TIMESTAMP_UTC when the field carries velox.logical_timestamp=utc.

The JVM builds row-to-columnar schema in SparkArrowUtil.toArrowField, which writes TimestampNTZType as a timestamp with no timezone and no tag, so Velox imports it as TIMESTAMP. The Substrait plan maps NTZ to TIMESTAMP_UTC, so mixed batches fail rowType_->equivalent in the shuffle writer; other JVM-built schemas such as shuffle read and broadcast have the same gap.

The suggested fix would be two JVM-only changes. First, SparkArrowUtil.toArrowField adds the utc tag to NTZ fields, including nested ones. Second, ArrowUtil.toArrowSchema, which ColumnarBatches.load uses, keeps the metadata Velox exported.

Do you think this make sense?

yes it's more reasonable, I'll try to refact it

@felipepessoto

Copy link
Copy Markdown
Contributor

I noticed #13512 adds the same velox.logical_timestamp=utc tag for native Delta writes. I’d prefer to reuse your implementation, especially since it also preserves the distinction between ordinary timestamps and NTZ when a batch is reloaded. If this lands first, I’ll rebase #13512 and drop its copy of the tagging code. I’ll keep the writer-specific changes there: preserving nested field names on disk and making sure NTZ map keys survive a shuffle before the write.

@felipepessoto felipepessoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The scalar NTZ tag survives, but the shuffle reader still loses it on map keys and values. I also noted how to keep the range-shuffle test on the batch-resize path.

)
case dataType =>
val fieldType = new FieldType(nullable, toArrowType(dataType, timeZoneId), null)
val metadata = if (isTimestampNTZ(dataType)) timestampUtcMetadata else null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This tag reaches row-to-columnar, but shuffle read still loses it when NTZ is the direct key or value of a map. ShuffleReaderJniWrapper.make calls arrow::ImportSchema before VeloxShuffleReader derives its row type. I reproduced that round-trip with the pinned Arrow 18.0.0: scalar and array-element tags survived, but both tags on map<timestamp_ntz,timestamp_ntz> disappeared. The reader therefore produces MAP<TIMESTAMP,TIMESTAMP> where the plan expects two TIMESTAMP_UTC children. Can we reuse the original-C-schema reader change from #13512 and cover both a map key and a map value after a shuffle? This affects shuffle consumers independently of Delta writes.

assert(find(plan)(_.isInstanceOf[RowToVeloxColumnarExec]).isDefined)
assert(find(plan) {
case e: ColumnarShuffleExchangeExec =>
e.outputPartitioning.isInstanceOf[RangePartitioning]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This assertion also passes for a sort shuffle, which doesn't install VeloxResizeBatchesExec. And with a max batch size of 1024, for example, the 808-row tail is above the default 256-row resize minimum, so no batch is rebuilt. Please pin the batch size and resize minimum in this test and assert that the range exchange's input contains the resizer and native project. That way a plan or default change can't make the original mismatch disappear from the test.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants