Skip to content

fix: cast nested columns during schema evolution - #8379

Merged
wjones127 merged 5 commits into
mainfrom
gatekeeper/fix-6926-1
Sep 2, 2026
Merged

wjones127 merged 5 commits into
mainfrom
gatekeeper/fix-6926-1

Conversation

@lance-gatefixer

Copy link
Copy Markdown
Contributor

Summary

  • cast projected arrays against the projected target schema so nested struct children retain their hierarchy
  • supersede rewritten ancestor field entries in legacy files to keep fragment metadata valid
  • cover nested int32 to int64 casts across multiple fragments and both storage formats

Root cause

The cast mapper looked up each field by its leaf name in a batch whose projected nested column remained under its parent struct. That lookup panicked, and legacy files also needed their duplicated ancestor field IDs tombstoned when the child was rewritten.

Validation

  • cargo test -p lance test_cast_nested_column -- --nocapture
  • cargo test -p lance dataset::schema_evolution::test::test_cast_column -- --nocapture
  • cargo fmt --all -- --check
  • cargo clippy --all --tests --benches -- -D warnings

Fixes #6926

@github-actions github-actions Bot added the bug Something isn't working label Aug 7, 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: request changes.

The nested single-column fix is sound, but the new positional mapper breaks the existing multi-column cast contract: request order can now determine which values are written to which field.

Align source and target projections from the same field-ID order, then cover reversed multi-column casts in both storage versions.

let columns = batch
.columns()
.iter()
.zip(output_schema.fields())

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.

These columns are paired positionally, but read_columns follows alteration order (Schema::project preserves request order) while new_col_schema.project_by_ids emits dataset schema order. Reversing two casts therefore silently swaps values. On this head, a Legacy/Stable regression using a=[1,2], b=[10,20], and alterations [cast b→Int64, cast a→Int64] produced a=[10,20].

Please derive both projections from the same field-ID order (or align arrays by field identity) and retain a reversed-order regression.

Executed reproducer
#[rstest]
#[tokio::test]
async fn test_cast_columns_reversed_order(
    #[values(LanceFileVersion::Legacy, LanceFileVersion::Stable)]
    version: LanceFileVersion,
) -> Result<()> {
    let schema = Arc::new(ArrowSchema::new(vec![
        ArrowField::new("a", DataType::Int32, false),
        ArrowField::new("b", DataType::Int32, false),
    ]));
    let batch = RecordBatch::try_new(
        schema.clone(),
        vec![
            Arc::new(Int32Array::from(vec![1, 2])),
            Arc::new(Int32Array::from(vec![10, 20])),
        ],
    )?;
    let dir = TempStrDir::default();
    let mut ds = Dataset::write(
        RecordBatchIterator::new(vec![Ok(batch)], schema),
        &dir,
        Some(WriteParams {
            data_storage_version: Some(version),
            ..Default::default()
        }),
    ).await?;

    ds.alter_columns(&[
        ColumnAlteration::new("b".into()).cast_to(DataType::Int64),
        ColumnAlteration::new("a".into()).cast_to(DataType::Int64),
    ]).await?;

    let data = ds.scan().try_into_batch().await?;
    assert_eq!(data["a"].as_ref(), &Int64Array::from(vec![1, 2]));
    Ok(())
}

Executed with CARGO_TARGET_DIR=/home/agent/tmp/gate-8379-impl-target cargo test -p lance --lib test_cast_columns_reversed_order -- --nocapture. Both storage versions failed: expected a=[1,2], observed a=[10,20].

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.

Addressed in 25dc290: source paths and target field IDs now share dataset schema order, and the reversed b/a regression passes for Legacy and Stable across two fragments.

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.

Confirmed on acdd863e: source paths and target IDs remain schema-ordered, and the reversed-order regression passes for Legacy and Stable. This ordering finding is resolved.

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.

No code change was needed on 921df7e: source paths and target field IDs remain in dataset schema order, and the reversed-order regression still covers Legacy and Stable across multiple fragments.

lance-gatekeeper[bot]

This comment was marked as outdated.

@Xuanwo Xuanwo added the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 10, 2026
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…er/fix-6926-1

# Conflicts:
#	rust/lance/src/dataset/schema_evolution.rs
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 11, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Aug 11, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Aug 11, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 11, 2026
@wjones127
wjones127 self-requested a review August 19, 2026 21:16
Comment on lines +4651 to +4654
/// Recasting a nullable nested field still claims when its top-level parent is
/// required: a stale append omits the new child id, and the reader cannot
/// synthesize that child under a required parent. A nullable parent can mask
/// the missing nullable child, so that append remains compatible.

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.

nitpick: This first sentence is completely unreadable. Please rewrite for clarity.

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.

Addressed in 8034834: the test documentation now begins with the direct field-ID consequence and states the required- and nullable-parent outcomes plainly.

Comment on lines +4705 to +4710
let result = CommitBuilder::new(Arc::new(dataset)).execute(append).await;
assert_eq!(
result.is_err(),
expect_conflict,
"parent_nullable={parent_nullable}: got {result:?}"
);

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.

question(blocking): why should this ever fail? Is this a temporary issue? Or unsolvable without a format change?

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.

Addressed in 8034834: the comment now explains that the conflict prevents unreadable staged rows because transaction rebasing does not apply the cast to them; supporting that case requires smarter rebasing, not a file-format change.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 31, 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 clarified regression makes the concurrency contract explicit: a cast assigns a new child field ID, and transaction rebasing does not transform staged append data. Rejecting stale appends under required parents therefore prevents unreadable rows, while nullable parents remain compatible. Allowing the required-parent append would require smarter rebasing, not a file-format change.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 31, 2026
@wjones127
wjones127 merged commit 589a817 into main Sep 2, 2026
37 checks passed
@wjones127
wjones127 deleted the gatekeeper/fix-6926-1 branch September 2, 2026 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: alter column type crash

2 participants