Repository navigation
feat(scanner): make Scanner::project_with_schema public - #9735
westonpace merged 3 commits into
Conversation
`ProjectionPlan::from_schema` checked that each requested top-level name exists, then handed the schema to `Projection::union_schema`, which records every field carrying a non-negative id without checking the base issued it. Nested children were therefore never checked, and ids were taken at face value. Two shapes get through. A child the base does not have resolves to nothing, so a caller asking for two children silently receives one. And a schema numbered independently of the dataset, which `Schema::try_from(&ArrowSchema)` produces, carries ids that can land on unrelated fields of the base, so the projection reads the wrong column. Both trip a debug assertion further down in debug builds and pass silently in release. Resolve the requested fields through `Schema::project_by_schema` with `OnMissing::Error` and `OnTypeMismatch::Error` before building the projection, which matches by name, rewrites the ids to the base's own and reports anything the base does not have. This is what the take path already does before calling here.
`project_with_schema` projects by a possibly partial schema, which expression-based projection cannot express: a nested field narrowed to a subset of its children. It is crate-private, so callers outside `lance` cannot request such a projection and must round-trip through field-path expressions, which flatten nested fields and lose their field ids. Make it public and document the contract, which crate-internal callers could rely on implicitly but an external one cannot: the schema must be a subset of the dataset schema, matched by name and type including nested children, with field ids resolved against the dataset rather than trusted. Covered by a test over a partial nested struct that drops one child of a nested field, the legacy row-id and row-address flags, and configured blob handling surviving the projection as it does for `project_with_transform`.
A schema projection naming `_rowid` set `must_add_row_offset`, which adds `AddRowOffsetExec` to the plan. That node reads `_rowaddr`, and the `_rowid` arm never enables it, so the scan failed to plan with "Input plan does not have a _rowaddr column" before reading any rows. The `_rowoffset` arm, which does need the node, enables both. Only `_rowoffset` needs `AddRowOffsetExec`, so `_rowid` no longer asks for it. Requesting row ids through a schema now behaves like requesting them with `with_row_id()`. Reachable from outside the crate now that `project_with_schema` is public, which is how it was found. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The previous _rowid projection issue is fixed in 9ed68c4. Schemas can now request row IDs directly, and _rowoffset still returns the expected offsets across fragments.
|
The third commit (9ed68c4) is outside the original scope of this PR. It fixes a A schema naming Before 9ed68c4 this failed with let requested = Schema::try_from(&ArrowSchema::new(vec![
ArrowField::new("idx", DataType::Int32, true),
ArrowField::new(ROW_ID, DataType::UInt64, true),
]))?;
let mut scan = dataset.scan();
scan.project_with_schema(&requested)?;
let batch = scan.try_into_batch().await?; // <- Err before the fixThe workaround was a data-only schema plus Pre-existing, but only reachable from outside the crate once |
westonpace
left a comment
There was a problem hiding this comment.
Still sounds good to me, and thanks for the additional fix.
I'm pretty sure I've wanted this method at least once when working on lancedb stuff. Appreciate the addition.
Make
Scanner::project_with_schemapublic, and fix the projection path underneath so that exposing it is safe.Public API
Scanner::project_with_schemapub(crate)topub. No signature change.ProjectionPlan::from_schemaWhy
project_with_schemashould be publicIt projects by a possibly partial schema: a nested field narrowed to a subset of its children. Expression-based projection cannot express that, since
projectflattensmetadata.location.xinto a top-level column of that name. Only the mem-wal scanner can reach it today, so an external caller must rebuild the nested shape by hand.Companion fix: validate the schema where it is accepted
from_schemais where a caller's schema becomes a set of field ids, but it checks only top-level names. Everything below that is taken on trust, and field ids are dataset-scoped identities, so trusting them only works when the schema came from the same dataset. Two shapes get through:Both trip a
debug_assertin debug builds and pass silently in release.The fix resolves the requested fields through
Schema::project_by_schemawithOnMissing::ErrorandOnTypeMismatch::Error, matching by name, rewriting ids to the base's own, and rejecting what the dataset does not have. The take path already validates this way before calling here, and no in-crate caller depends on the lax behaviour.Name resolution rather than an id check is deliberate: a caller holding an Arrow-derived schema should not have to know how Lance numbers fields internally. It also catches type mismatches, which an id check cannot.
Tests
Each fails without the change it covers.
projection.rs: a nested child absent from the base is reported rather than dropped.projection.rs: an independently numbered schema resolves by name rather than by colliding id.scanner.rs: a partial nested projection keeps its shape, carries the legacy row-id and row-address flags, preservesBlobHandling, and matchestake_rows.pub(crate).Related validation gaps found while writing this
None is a live bug, and none is addressed here.
Projection::union_schemadocuments that fields are added "only if they exist in the base schema" and that it "will panic" on a non-negative id the base lacks. It does neither: such ids are inserted unchecked, and thedebug_assertmeant to catch them sits in theid < 0branch it cannot reach. All four callers pass base-derived ids, so nothing breaks today; the contract and the dead assertion mislead.ProjectionRequest::into_projection_planpre-validates only when the schema has no system columns, sinceproject_by_schemacannot handle_rowid. This PR closes that branch,from_schemanow validating after system columns are separated out, which also makes the outer special-casing redundant.Projection::union_arrow_schemapassesOnTypeMismatch::TakeSelf, silently coercing a field whose type disagrees with the dataset, where the path hardened here errors.PR History
Scanner::projection_from_schema#5134 proposed adding such a method. Closed as stale.project_with_schemafor the mem-wal scanner, but withpub(crate)visibility.This PR opens visibility and adds validation.
Tracking: The visibility change is ported in part from rerun-io#72; the projection fix was found while preparing it.