Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(sequence): fall back to canonical cast when params can't fit target#7630

Closed
changjoon-park wants to merge 1 commit into
vortex-data:developfrom
changjoon-park:fix/sequence-cast-narrower-type
Closed

fix(sequence): fall back to canonical cast when params can't fit target#7630
changjoon-park wants to merge 1 commit into
vortex-data:developfrom
changjoon-park:fix/sequence-cast-narrower-type

Conversation

@changjoon-park

Copy link
Copy Markdown

Summary

Closes #5102.

SequenceArray represents arithmetic sequences as (base, multiplier, len). When cast to a narrower or differently-signed target type, the current implementation tries to cast the stored base and multiplier to the target type directly. This fails when either parameter cannot be represented in the target type even if every produced value is representable.

Concrete failing case (matches the test file's TODO(DK) comment):

Sequence(base=100i32, multiplier=-10i32, len=5)
  → values [100, 90, 80, 70, 60]   (all fit in u8)
  → current: fails with "Cannot cast -10i32 to u8"
  → expected: succeed with [100u8, 90, 80, 70, 60]

Fix

Return Ok(None) from CastReduce::cast when casting base or multiplier fails. This delegates to the canonical cast path, which decodes the sequence into individual values and casts them one by one — correctly succeeding whenever all produced values fit the target type.

This matches the existing behavior for float targets, which already returns Ok(None) in the same file. CastReduce's Ok(None) contract is for "rule doesn't apply, use another approach" situations.

What this PR does not do

The issue suggests a more invasive fix: separate PTypes for calculation vs. return value. That would preserve Sequence's compact representation through the cast and avoid the canonical decode. This PR takes the narrower approach of using the existing canonical fallback to make the operation correct; a parameter-type redesign would be a separate PR with its own design discussion.

Testing

  • Reproduced the bug first: uncommented #[case::negative_step(...)] and confirmed test_cast_sequence_conformance::case_3_negative_step failed with Cannot cast -10i32 to u8.
  • After fix: same case passes, and all 5 conformance cases pass.
  • cargo test -p vortex-sequence — 57/57 pass.
  • cargo test -p vortex --lib — 5/5 pass.
  • cargo +nightly fmt --all -- --check — clean.
  • cargo clippy -p vortex-sequence --all-targets --all-features — no warnings.

No public API changes other than extending the set of casts that succeed.

Enables casting a Sequence to a narrower type when values fit but the
stored base/multiplier don't (e.g. negative multiplier to unsigned).
Closes vortex-data#5102.

Signed-off-by: changjoon-park <[email protected]>
@robert3005

Copy link
Copy Markdown
Contributor

The fix here should be to have two ptypes not fallback to canonical as the issue describes.

@changjoon-park

Copy link
Copy Markdown
Author

The fix here should be to have two ptypes not fallback to canonical as the issue describes.

make sense. the two-ptype approach is a larger redesign than I' d scoped

thanks for the review !

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SequenceArray cannot cast to a narrower type

2 participants