Skip to content

Commit 944f53b

Browse files
committed
feat(array): make every cast a session rule
The previous commit kept a built-in cast matrix beside the session rules, so there were two places deciding which casts exist. Now there is one: the `CastSession`. Its default registers the standard rules, one per family of dtype pairs (nullability, null, bool, primitive, decimal, list, map, struct, extension storage) plus `TimestampCast`, and `CastSession::empty` casts nothing at all. The standard rules resolve to `KernelCast`, a scalar function created only at execution time. The encodings' fused `CastKernel`s and the canonical kernels attach to it, so `Cast` itself does nothing but look up a rule: a rule registered later than the standard ones replaces them for the pairs it accepts, including on canonical inputs that have a fused kernel. `CastReduce` rules stay on `Cast`, because the optimizer has no session. `Scalar::cast` resolves through the default session's rules too, executed over a one-element constant, so scalar and array casts agree. The typed-view scalar casts remain as the scalar side of `KernelCast`. Refs #8570 Signed-off-by: Claude <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_012GiQdVvEQUshNCyjNFQUQL
1 parent 26d881b commit 944f53b

23 files changed

Lines changed: 604 additions & 338 deletions

File tree

‎encodings/bytebool/src/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use vortex_array::optimizer::kernels::ArrayKernelsExt;
88
use vortex_array::scalar_fn::ScalarFnVTable;
99
use vortex_array::scalar_fn::fns::binary::Binary;
1010
use vortex_array::scalar_fn::fns::binary::BooleanExecuteAdaptor;
11-
use vortex_array::scalar_fn::fns::cast::Cast;
1211
use vortex_array::scalar_fn::fns::cast::CastExecuteAdaptor;
12+
use vortex_array::scalar_fn::fns::cast::KernelCast;
1313
use vortex_session::VortexSession;
1414

1515
use crate::ByteBool;
1616

1717
pub(crate) fn initialize(session: &VortexSession) {
1818
let kernels = session.kernels();
1919
kernels.register_execute_parent_kernel(Binary.id(), ByteBool, BooleanExecuteAdaptor(ByteBool));
20-
kernels.register_execute_parent_kernel(Cast.id(), ByteBool, CastExecuteAdaptor(ByteBool));
20+
kernels.register_execute_parent_kernel(KernelCast.id(), ByteBool, CastExecuteAdaptor(ByteBool));
2121
kernels.register_execute_parent_kernel(Dict.id(), ByteBool, TakeExecuteAdaptor(ByteBool));
2222
}

‎encodings/fastlanes/src/bitpacking/vtable/kernels.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use vortex_array::scalar_fn::fns::between::Between;
1414
use vortex_array::scalar_fn::fns::between::BetweenExecuteAdaptor;
1515
use vortex_array::scalar_fn::fns::binary::Binary;
1616
use vortex_array::scalar_fn::fns::binary::CompareExecuteAdaptor;
17-
use vortex_array::scalar_fn::fns::cast::Cast;
1817
use vortex_array::scalar_fn::fns::cast::CastExecuteAdaptor;
18+
use vortex_array::scalar_fn::fns::cast::KernelCast;
1919
use vortex_session::VortexSession;
2020

2121
use crate::BitPacked;
@@ -27,7 +27,11 @@ pub(crate) fn initialize(session: &VortexSession) {
2727
BitPacked,
2828
BetweenExecuteAdaptor(BitPacked),
2929
);
30-
kernels.register_execute_parent_kernel(Cast.id(), BitPacked, CastExecuteAdaptor(BitPacked));
30+
kernels.register_execute_parent_kernel(
31+
KernelCast.id(),
32+
BitPacked,
33+
CastExecuteAdaptor(BitPacked),
34+
);
3135
kernels.register_execute_parent_kernel(
3236
Binary.id(),
3337
BitPacked,

‎encodings/fsst/src/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use vortex_array::scalar_fn::fns::binary::Binary;
1212
use vortex_array::scalar_fn::fns::binary::CompareExecuteAdaptor;
1313
use vortex_array::scalar_fn::fns::byte_length::ByteLength;
1414
use vortex_array::scalar_fn::fns::byte_length::ByteLengthExecuteAdaptor;
15-
use vortex_array::scalar_fn::fns::cast::Cast;
1615
use vortex_array::scalar_fn::fns::cast::CastExecuteAdaptor;
16+
use vortex_array::scalar_fn::fns::cast::KernelCast;
1717
use vortex_array::scalar_fn::fns::like::Like;
1818
use vortex_array::scalar_fn::fns::like::LikeExecuteAdaptor;
1919
use vortex_session::VortexSession;
@@ -22,7 +22,7 @@ use crate::FSST;
2222

2323
pub(super) fn initialize(session: &VortexSession) {
2424
let kernels = session.kernels();
25-
kernels.register_execute_parent_kernel(Cast.id(), FSST, CastExecuteAdaptor(FSST));
25+
kernels.register_execute_parent_kernel(KernelCast.id(), FSST, CastExecuteAdaptor(FSST));
2626
kernels.register_execute_parent_kernel(Binary.id(), FSST, CompareExecuteAdaptor(FSST));
2727
kernels.register_execute_parent_kernel(Filter.id(), FSST, FilterExecuteAdaptor(FSST));
2828
kernels.register_execute_parent_kernel(Dict.id(), FSST, TakeExecuteAdaptor(FSST));

‎encodings/onpair/src/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use vortex_array::scalar_fn::fns::binary::Binary;
1010
use vortex_array::scalar_fn::fns::binary::CompareExecuteAdaptor;
1111
use vortex_array::scalar_fn::fns::byte_length::ByteLength;
1212
use vortex_array::scalar_fn::fns::byte_length::ByteLengthExecuteAdaptor;
13-
use vortex_array::scalar_fn::fns::cast::Cast;
1413
use vortex_array::scalar_fn::fns::cast::CastExecuteAdaptor;
14+
use vortex_array::scalar_fn::fns::cast::KernelCast;
1515
use vortex_session::VortexSession;
1616

1717
use crate::OnPair;
1818

1919
pub(super) fn initialize(session: &VortexSession) {
2020
let kernels = session.kernels();
21-
kernels.register_execute_parent_kernel(Cast.id(), OnPair, CastExecuteAdaptor(OnPair));
21+
kernels.register_execute_parent_kernel(KernelCast.id(), OnPair, CastExecuteAdaptor(OnPair));
2222
kernels.register_execute_parent_kernel(Filter.id(), OnPair, FilterExecuteAdaptor(OnPair));
2323
kernels.register_execute_parent_kernel(Binary.id(), OnPair, CompareExecuteAdaptor(OnPair));
2424
kernels.register_execute_parent_kernel(

‎vortex-array/src/arrays/bool/vtable/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::optimizer::kernels::ArrayKernelsExt;
1111
use crate::scalar_fn::ScalarFnVTable;
1212
use crate::scalar_fn::fns::binary::Binary;
1313
use crate::scalar_fn::fns::binary::BooleanExecuteAdaptor;
14-
use crate::scalar_fn::fns::cast::Cast;
1514
use crate::scalar_fn::fns::cast::CastExecuteAdaptor;
15+
use crate::scalar_fn::fns::cast::KernelCast;
1616
use crate::scalar_fn::fns::fill_null::FillNull;
1717
use crate::scalar_fn::fns::fill_null::FillNullExecuteAdaptor;
1818
use crate::scalar_fn::fns::zip::Zip;
@@ -21,7 +21,7 @@ use crate::scalar_fn::fns::zip::ZipExecuteAdaptor;
2121
pub(crate) fn initialize(session: &VortexSession) {
2222
let kernels = session.kernels();
2323
kernels.register_execute_parent_kernel(Binary.id(), Bool, BooleanExecuteAdaptor(Bool));
24-
kernels.register_execute_parent_kernel(Cast.id(), Bool, CastExecuteAdaptor(Bool));
24+
kernels.register_execute_parent_kernel(KernelCast.id(), Bool, CastExecuteAdaptor(Bool));
2525
kernels.register_execute_parent_kernel(FillNull.id(), Bool, FillNullExecuteAdaptor(Bool));
2626
kernels.register_execute_parent_kernel(Dict.id(), Bool, TakeExecuteAdaptor(Bool));
2727
kernels.register_execute_parent_kernel(Zip.id(), Bool, ZipExecuteAdaptor(Bool));

‎vortex-array/src/arrays/constant/compute/cast.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ use crate::dtype::DType;
1212
use crate::scalar_fn::fns::cast::CastReduce;
1313

1414
impl CastReduce for Constant {
15-
/// Folds the built-in casts. Session cast rules are consulted when the cast executes, where
16-
/// the session is known, and a failing cast is left in place so its error surfaces there.
15+
/// Folds the cast with the rules of the default session, the only ones a reduce rule can
16+
/// reach. A failing cast is left in place so its error surfaces at execution.
1717
fn cast(array: ArrayView<'_, Constant>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
18-
match array.scalar().cast_builtin(dtype) {
19-
Ok(Some(scalar)) => Ok(Some(ConstantArray::new(scalar, array.len()).into_array())),
20-
Ok(None) | Err(_) => Ok(None),
21-
}
18+
Ok(array
19+
.scalar()
20+
.cast(dtype)
21+
.ok()
22+
.map(|scalar| ConstantArray::new(scalar, array.len()).into_array()))
2223
}
2324
}
2425

‎vortex-array/src/arrays/decimal/vtable/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use crate::optimizer::kernels::ArrayKernelsExt;
1111
use crate::scalar_fn::ScalarFnVTable;
1212
use crate::scalar_fn::fns::between::Between;
1313
use crate::scalar_fn::fns::between::BetweenExecuteAdaptor;
14-
use crate::scalar_fn::fns::cast::Cast;
1514
use crate::scalar_fn::fns::cast::CastExecuteAdaptor;
15+
use crate::scalar_fn::fns::cast::KernelCast;
1616
use crate::scalar_fn::fns::fill_null::FillNull;
1717
use crate::scalar_fn::fns::fill_null::FillNullExecuteAdaptor;
1818

1919
pub(crate) fn initialize(session: &VortexSession) {
2020
let kernels = session.kernels();
2121
kernels.register_execute_parent_kernel(Between.id(), Decimal, BetweenExecuteAdaptor(Decimal));
22-
kernels.register_execute_parent_kernel(Cast.id(), Decimal, CastExecuteAdaptor(Decimal));
22+
kernels.register_execute_parent_kernel(KernelCast.id(), Decimal, CastExecuteAdaptor(Decimal));
2323
kernels.register_execute_parent_kernel(FillNull.id(), Decimal, FillNullExecuteAdaptor(Decimal));
2424
kernels.register_execute_parent_kernel(Dict.id(), Decimal, TakeExecuteAdaptor(Decimal));
2525
}

‎vortex-array/src/arrays/extension/compute/cast.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl CastReduce for Extension {
1717
/// Handles the two extension casts that need no extension-specific knowledge: unwrapping to
1818
/// the storage dtype, and changing the nullability of the same extension dtype. Every other
1919
/// extension cast comes from a session [`CastRule`](crate::scalar_fn::fns::cast::CastRule),
20-
/// which the executor consults before this rule.
20+
/// consulted when the cast executes.
2121
fn cast(array: ArrayView<'_, Extension>, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
2222
let ext_dtype = array.ext_dtype();
2323

‎vortex-array/src/arrays/fixed_size_list/vtable/kernel.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use crate::arrays::FixedSizeList;
99
use crate::arrays::dict::TakeExecuteAdaptor;
1010
use crate::optimizer::kernels::ArrayKernelsExt;
1111
use crate::scalar_fn::ScalarFnVTable;
12-
use crate::scalar_fn::fns::cast::Cast;
1312
use crate::scalar_fn::fns::cast::CastExecuteAdaptor;
13+
use crate::scalar_fn::fns::cast::KernelCast;
1414

1515
pub(crate) fn initialize(session: &VortexSession) {
1616
let kernels = session.kernels();
1717
kernels.register_execute_parent_kernel(
18-
Cast.id(),
18+
KernelCast.id(),
1919
FixedSizeList,
2020
CastExecuteAdaptor(FixedSizeList),
2121
);

‎vortex-array/src/arrays/list/compute/kernels.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use crate::arrays::dict::TakeExecuteAdaptor;
1111
use crate::arrays::filter::FilterExecuteAdaptor;
1212
use crate::optimizer::kernels::ArrayKernelsExt;
1313
use crate::scalar_fn::ScalarFnVTable;
14-
use crate::scalar_fn::fns::cast::Cast;
1514
use crate::scalar_fn::fns::cast::CastExecuteAdaptor;
15+
use crate::scalar_fn::fns::cast::KernelCast;
1616

1717
pub(crate) fn initialize(session: &VortexSession) {
1818
let kernels = session.kernels();
19-
kernels.register_execute_parent_kernel(Cast.id(), List, CastExecuteAdaptor(List));
19+
kernels.register_execute_parent_kernel(KernelCast.id(), List, CastExecuteAdaptor(List));
2020
kernels.register_execute_parent_kernel(Filter.id(), List, FilterExecuteAdaptor(List));
2121
kernels.register_execute_parent_kernel(Dict.id(), List, TakeExecuteAdaptor(List));
2222
}

0 commit comments

Comments
 (0)