Skip to content

[GLUTEN-13487][VL] Fall back Lance scans the Arrow stream cannot carry - #13554

Open
malinjawi wants to merge 2 commits into
apache:mainfrom
malinjawi:fix/lance-scan-fallback
Open

malinjawi wants to merge 2 commits into
apache:mainfrom
malinjawi:fix/lance-scan-fallback

Conversation

@malinjawi

@malinjawi malinjawi commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

Part of #13487.

#13488 fixed vector columns. Some other Lance scans still get offloaded even though the Arrow stream can't carry their columns, so they fail on the first batch. This PR makes them fall back to vanilla Spark at plan time.

LanceScanTransformer.isExportable now falls back when a scan reads:

  • a blob or JSON field, also one nested in a struct, list or map, or a metadata column (_rowid, _rowaddr, _fragid, row versions, _score). lance-spark fixes these up itself after the native scan.
  • no columns at all, like SELECT 1 FROM t. The native scan returns _rowid instead.
  • a column Spark's ArrowColumnVector can't wrap: FixedSizeBinary, Date(MILLISECOND) and Float16 vectors. The check maps the column with lance-spark's LanceArrowUtils and rewrites vectors as lists, the same as the scan does since [GLUTEN-13487][VL] Offload Lance scans with vector columns #13488.
  • a vector that lance-spark maps to a plain list, like a vector of ints or a vector of vectors. lance-spark 0.8.0 only maps float and double vectors back to FixedSizeList, but the native scan returns every vector as one.

Only the columns a query reads count, and other vector columns stay offloaded.

Not covered yet: unsigned ints, LargeList, time, non-microsecond timestamps and timestamps with a time zone other than UTC. lance-spark 0.8.0's Spark schema doesn't tell these apart, so they can't be caught at plan time. That needs lance-format/lance-spark#908.

How was this patch tested?

Four new tests in VeloxLanceSuite cover each case above. They also check that the same table still offloads when the unsupported column isn't read. The Float16 case runs on Spark 4.0+ only, because lance-spark can't write it on 3.5. The int vector and the vector of vectors are written with lance-core, since lance-spark can't write them.

VeloxLanceSuite and LanceFixedSizeListsSuite pass on Spark 3.5 and 4.0 with the lance-spark 0.8.0 release. Without this change, the four new tests fail.

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

Generated-by: IBM Bob

Since the Lance scan offload was added in apache#12958, isExportable has only
rejected a pushed aggregation and a full-text query. Other scans that
the Arrow C stream cannot serve are offloaded and then fail on their
first batch (apache#13487). apache#13488 makes vector columns readable; this covers
the rest.

isExportable now also checks the scan's columns at plan time, following
what LanceArrowStreamScanner.export and LanceFragmentScanner do:

- Columns that lance-spark's own reader fixes up after the native scan
  fall back. Blob (v1 and v2) and JSON columns come back in their stored
  form. The _rowid, _rowaddr, _fragid, row-version, _score and blob
  position and size columns are left out of the native projection.
- A scan that reads no columns falls back: the native scan returns
  _rowid instead.
- Every other column is mapped to Arrow with lance-spark's
  LanceArrowUtils, the mapping the export enforces, with vectors
  rewritten as lists as the scan emits them. An empty vector of that
  type is wrapped in ArrowColumnVector, as the scan will. FixedSizeBinary,
  Date(MILLISECOND) and Float16 vector columns fall back this way. Other
  vector columns stay offloaded.

With lance-spark 0.8.0, unsigned integer, LargeList, time and
non-microsecond timestamp columns still fail at execution: its Spark
schema does not record these Arrow types, so they cannot be told apart
at plan time.
@malinjawi

Copy link
Copy Markdown
Contributor Author

cc @zhouyuan @sezruby this covers the rest of #13487 after #13488. Thanks!

@sezruby sezruby 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.

Thanks @malinjawi. I ran this check against lance-spark 0.8.0 on datasets written with pylance 12. For each scan it offloads, I called LanceArrowStreamScanner.export, which is what LanceFragmentIterator calls. Float/double vectors at the top level, in structs, lists and maps, multivectors, string/binary (also large), decimals and microsecond UTC/naive timestamps offload and export fine. FixedSizeBinary, Date(MILLISECOND) and a nested blob v2 fall back as intended. Three groups still pass the check and then fail at export; details inline. Two of them can be detected from the Spark schema.

For the cases the Spark schema can't show (unsigned ints, LargeList, time, non-microsecond timestamps), lance-format/lance-spark#908 decides at plan time from the dataset's Arrow schema (LanceScan.arrowBatchSchema() / arrowBatchUnsupportedReason()). That is on the Arrow-batch path in Felix's plan on #12958.

* columns, are left out of the native scan's projection: lance-spark requests, computes or
* reorders them itself.
*/
private def needsLanceReader(field: StructField): Boolean =

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 only looks at top-level fields, but lance-spark keeps the blob and JSON markers on nested fields too, and the native scan returns those nested values in their stored form as well. With lance-spark 0.8.0, a legacy blob inside a struct (info struct<name string, blob binary> with lance-encoding:blob on blob, file format 2.1) passes the check. So does a JSON field inside a struct or inside a list of structs. Export then throws on its schema check: Declared [id Int(64, true) null, info Struct null] but the native scan produced [id Int(64, true) null, info Struct null]. Could this walk struct fields, array elements and map keys and values, checking each field's metadata (BlobUtils.isBlobSparkMetadata / isBlobV2SparkMetadata, JsonUtils.hasJsonMetadata)? A blob v2 inside a struct already falls back here, because its mapped field isn't wrappable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! I missed that lance-spark keeps those markers on nested fields too. Fixed in c32f49a: the check now looks inside structs, lists and maps, and falls back if it finds a blob or JSON field anywhere. I added tests for JSON in a struct, in a list of structs and as a map value, and for a blob inside a struct.

arrowSchema =>
schema.fields.zip(arrowSchema.getFields.asScala).forall {
case (field, arrowField) =>
isWrappable(FixedSizeLists.toListField(arrowField), field.dataType)

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.

An int-element vector or a vector of vectors passes this check but still fails at export. lance-spark 0.8.0's toArrowSchema restores a FixedSizeList only for float/double elements (VectorUtils.shouldBeFixedSizeList). So a FixedSizeList(4) of int8 maps to List, which is wrappable, while the native scan returns FixedSizeList(4): Declared [id Int(32, true) null, v List null] but the native scan produced [id Int(32, true) null, v FixedSizeList(4) null]. The same happens for int32 vectors and for a FixedSizeList(2) of float FixedSizeList(2). Both show in the Spark schema: the field carries arrow.fixed-size-list.size, but its mapped field isn't a FixedSizeList. Checking that at any depth would make these fall back, and they would offload again on their own once lance-spark maps them as FixedSizeList.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, done in c32f49a. If a field has arrow.fixed-size-list.size but doesn't map back to a FixedSizeList, the scan now falls back, at any depth. I tested an int8 vector and a vector of vectors, written with lance-core since lance-spark can't write them. And as you said, they'll start offloading again on their own once lance-spark maps them as FixedSizeList.

(in the Lance reader) before the Arrow stream is exported, not inside Velox.
- Rare schemas that require JVM post-processing (for example `_rowid` / `_rowaddr` / blob columns)
are not offloaded.
- With lance-spark 0.8.0, a scan over an unsigned integer, `LargeList`, time, or non-microsecond

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.

One more case for this list: a microsecond timestamp with a time zone other than UTC, e.g. Timestamp(us, America/New_York). lance-spark reads it as TimestampType without the zone, and both this check and the export map it back to Timestamp(MICROSECOND, UTC). So export throws: Declared [id Int(32, true) null, v Timestamp(MICROSECOND, UTC) null] but the native scan produced [id Int(32, true) null, v Timestamp(MICROSECOND, America/New_York) null].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good one, added it to the list. Thanks!

Review follow-up. Two more kinds of columns passed the plan-time check
and then failed at export:

- Blob and JSON fields nested in a struct, list or map. The native scan
  returns these in their stored form at any depth, so their markers are
  now checked on every nested field. lance-spark embeds the metadata of
  array elements and map keys and values in the parent field's
  (_lance.element, _lance.key, _lance.value), and the check reads it
  from there, as the mapping does.
- Vectors the mapping does not restore as a FixedSizeList. lance-spark
  0.8.0 restores only vectors of floats or doubles, so an int8 vector or
  a vector of vectors maps to a List while the native scan returns a
  FixedSizeList. A field that carries arrow.fixed-size-list.size but
  does not map to a FixedSizeList now falls back, at any depth. These
  offload again once lance-spark maps them as FixedSizeList.

The doc also lists timestamps with a time zone other than UTC among
the columns that cannot be detected at plan time.
@malinjawi

Copy link
Copy Markdown
Contributor Author

Thanks a lot @sezruby for testing this so carefully, really appreciate it. I've pushed a fix for all three points and updated the description. And agreed, lance-format/lance-spark#908 is the proper fix for the types we can't see from the Spark schema.

@malinjawi

Copy link
Copy Markdown
Contributor Author

@zhouyuan @sezruby quick question on this one. I saw lance-format/lance-spark#908 got merged, which is the right fix. But until it's in a lance-spark release, these scans will keep failing.

Should we keep this PR as a stopgap? I can add a TODO saying to remove the check once Gluten moves to a release with #908. Or I can close it and we wait for the release. Let me know what you'd prefer.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants