Skip to content

feat(scanner): make Scanner::project_with_schema public - #9735

Merged
westonpace merged 3 commits into
lance-format:mainfrom
rerun-io:upstream/projection-schema-validation
Oct 7, 2026
Merged

westonpace merged 3 commits into
lance-format:mainfrom
rerun-io:upstream/projection-schema-validation

Conversation

@amunra

@amunra amunra commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Make Scanner::project_with_schema public, and fix the projection path underneath so that exposing it is safe.

Public API

item change
Scanner::project_with_schema pub(crate) to pub. No signature change.
ProjectionPlan::from_schema Behaviour. A field the dataset lacks, or a type mismatch, is now an error instead of a silently narrower or wrong projection.

Why project_with_schema should be public

It projects by a possibly partial schema: a nested field narrowed to a subset of its children. Expression-based projection cannot express that, since project flattens metadata.location.x into 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_schema is 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:

  • a nested child the base lacks resolves to nothing, so a caller asking for two children receives one;
  • a schema numbered independently carries ids that can land on unrelated base fields, so the projection reads the wrong column.

Both trip a debug_assert in debug builds and pass silently in release.

The fix resolves the requested fields through Schema::project_by_schema with OnMissing::Error and OnTypeMismatch::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, preserves BlobHandling, and matches take_rows.
  • Doc-test: the scanner test is in-crate and so blind to visibility, so a doc-test covers it, failing to build at pub(crate).

Related validation gaps found while writing this

None is a live bug, and none is addressed here.

  • Projection::union_schema documents 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 the debug_assert meant to catch them sits in the id < 0 branch it cannot reach. All four callers pass base-derived ids, so nothing breaks today; the contract and the dead assertion mislead.
  • ProjectionRequest::into_projection_plan pre-validates only when the schema has no system columns, since project_by_schema cannot handle _rowid. This PR closes that branch, from_schema now validating after system columns are separated out, which also makes the outer special-casing redundant.
  • Projection::union_arrow_schema passes OnTypeMismatch::TakeSelf, silently coercing a field whose type disagrees with the dataset, where the path hardened here errors.

PR History

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.

amunra and others added 2 commits October 5, 2026 19:05
`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`.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added the enhancement New feature or request label Oct 6, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 6, 2026
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>
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 6, 2026

@lance-gatekeeper lance-gatekeeper Bot 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.

✅ 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.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 6, 2026
@amunra

amunra commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

The third commit (9ed68c4) is outside the original scope of this PR. It fixes a _rowid
projection issue that lance-gatekeeper identified while reviewing the second commit.

A schema naming _rowid without _rowaddr failed to plan. The ROW_ID arm in
ProjectionPlan::from_schema set must_add_row_offset, which adds AddRowOffsetExec; that
node reads _rowaddr, which the arm never enabled. Only _rowoffset needs it, and that arm
enables both. So the fix is to stop requesting the node for _rowid.

Before 9ed68c4 this failed with Internal: Input plan does not have a _rowaddr column,
before reading any rows:

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 fix

The workaround was a data-only schema plus with_row_id(). Both forms now work.

Pre-existing, but only reachable from outside the crate once project_with_schema is
public, which is what this PR does — hence fixing it here rather than separately. Covered by
test_project_with_schema_row_id_without_row_address.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 6, 2026

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants