Skip to content

Commit c6aa122

Browse files
committed
Flat Layout Execution
Signed-off-by: Nicholas Gates <[email protected]>
1 parent 412aff2 commit c6aa122

File tree

12 files changed

+79
-118
lines changed

12 files changed

+79
-118
lines changed

vortex-array/src/expr/exprs/binary.rs

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33

44
use std::fmt::Formatter;
55

6-
use arrow_array::Array;
76
use arrow_ord::cmp;
87
use prost::Message;
9-
use vortex_compute::arithmetic::Add;
10-
use vortex_compute::arithmetic::Arithmetic;
11-
use vortex_compute::arithmetic::CheckedArithmetic;
12-
use vortex_compute::arithmetic::Div;
13-
use vortex_compute::arithmetic::Mul;
14-
use vortex_compute::arithmetic::Sub;
8+
use vortex_compute::arrow::IntoArrow;
9+
use vortex_compute::arrow::IntoVector;
1510
use vortex_dtype::DType;
1611
use vortex_error::VortexExpect;
1712
use vortex_error::VortexResult;
1813
use vortex_error::vortex_bail;
1914
use vortex_error::vortex_err;
2015
use vortex_proto::expr as pb;
2116
use vortex_vector::Datum;
22-
use vortex_vector::PrimitiveDatum;
23-
use vortex_vector::Vector;
2417

2518
use crate::ArrayRef;
2619
use crate::compute;
@@ -142,50 +135,52 @@ impl VTable for Binary {
142135

143136
match op {
144137
Operator::And => {
145-
let lhs = lhs.ensure_vector(args.row_count).into_bool().into();
146-
let rhs = rhs.ensure_vector(args.row_count).into_bool().into();
147-
return Ok(Datum::Vector(Vector::try_from(
148-
&arrow_arith::boolean::and_kleene(&lhs, &rhs)? as &dyn Array,
149-
)?));
138+
let lhs = lhs.ensure_vector(args.row_count).into_bool().into_arrow()?;
139+
let rhs = rhs.ensure_vector(args.row_count).into_bool().into_arrow()?;
140+
return Ok(Datum::Vector(
141+
arrow_arith::boolean::and_kleene(&lhs, &rhs)?
142+
.into_vector()?
143+
.into(),
144+
));
150145
}
151146
Operator::Or => {
152-
let lhs = lhs.ensure_vector(args.row_count).into_bool().into();
153-
let rhs = rhs.ensure_vector(args.row_count).into_bool().into();
154-
return Ok(Datum::Vector(Vector::try_from(
155-
&arrow_arith::boolean::or_kleene(&lhs, &rhs)? as &dyn Array,
156-
)?));
147+
let lhs = lhs.ensure_vector(args.row_count).into_bool().into_arrow()?;
148+
let rhs = rhs.ensure_vector(args.row_count).into_bool().into_arrow()?;
149+
return Ok(Datum::Vector(
150+
arrow_arith::boolean::or_kleene(&lhs, &rhs)?
151+
.into_vector()?
152+
.into(),
153+
));
157154
}
158155
_ => {}
159156
}
160157

161-
let lhs: Box<dyn arrow_array::Datum> = lhs.try_into()?;
162-
let rhs: Box<dyn arrow_array::Datum> = rhs.try_into()?;
158+
let lhs = lhs.into_arrow()?;
159+
let rhs = rhs.into_arrow()?;
163160

164161
let vector = match op {
165-
Operator::Eq => Vector::try_from(&cmp::eq(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?,
166-
Operator::NotEq => {
167-
Vector::try_from(&cmp::neq(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?
168-
}
169-
Operator::Gt => Vector::try_from(&cmp::gt(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?,
170-
Operator::Gte => {
171-
Vector::try_from(&cmp::gt_eq(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?
172-
}
173-
Operator::Lt => Vector::try_from(&cmp::lt(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?,
174-
Operator::Lte => {
175-
Vector::try_from(&cmp::lt_eq(lhs.as_ref(), rhs.as_ref())? as &dyn Array)?
176-
}
162+
Operator::Eq => cmp::eq(lhs.as_ref(), rhs.as_ref())?.into_vector()?.into(),
163+
Operator::NotEq => cmp::neq(lhs.as_ref(), rhs.as_ref())?.into_vector()?.into(),
164+
Operator::Gt => cmp::gt(lhs.as_ref(), rhs.as_ref())?.into_vector()?.into(),
165+
Operator::Gte => cmp::gt_eq(lhs.as_ref(), rhs.as_ref())?
166+
.into_vector()?
167+
.into(),
168+
Operator::Lt => cmp::lt(lhs.as_ref(), rhs.as_ref())?.into_vector()?.into(),
169+
Operator::Lte => cmp::lt_eq(lhs.as_ref(), rhs.as_ref())?
170+
.into_vector()?
171+
.into(),
177172

178173
Operator::Add => {
179-
Vector::try_from(arrow_arith::numeric::add(lhs.as_ref(), rhs.as_ref())?.as_ref())?
174+
arrow_arith::numeric::add(lhs.as_ref(), rhs.as_ref())?.into_vector()?
180175
}
181176
Operator::Sub => {
182-
Vector::try_from(arrow_arith::numeric::sub(lhs.as_ref(), rhs.as_ref())?.as_ref())?
177+
arrow_arith::numeric::sub(lhs.as_ref(), rhs.as_ref())?.into_vector()?
183178
}
184179
Operator::Mul => {
185-
Vector::try_from(arrow_arith::numeric::mul(lhs.as_ref(), rhs.as_ref())?.as_ref())?
180+
arrow_arith::numeric::mul(lhs.as_ref(), rhs.as_ref())?.into_vector()?
186181
}
187182
Operator::Div => {
188-
Vector::try_from(arrow_arith::numeric::div(lhs.as_ref(), rhs.as_ref())?.as_ref())?
183+
arrow_arith::numeric::div(lhs.as_ref(), rhs.as_ref())?.into_vector()?
189184
}
190185
Operator::And | Operator::Or => {
191186
unreachable!("Already dealt with above")
@@ -323,35 +318,6 @@ impl VTable for Binary {
323318
}
324319
}
325320

326-
fn execute_arithmetic_primitive(
327-
lhs: PrimitiveDatum,
328-
rhs: PrimitiveDatum,
329-
op: Operator,
330-
) -> VortexResult<Datum> {
331-
// Float arithmetic - no overflow checking needed
332-
if lhs.ptype().is_float() && lhs.ptype() == rhs.ptype() {
333-
let result: PrimitiveDatum = match op {
334-
Operator::Add => Arithmetic::<Add>::eval(lhs, rhs),
335-
Operator::Sub => Arithmetic::<Sub>::eval(lhs, rhs),
336-
Operator::Mul => Arithmetic::<Mul>::eval(lhs, rhs),
337-
Operator::Div => Arithmetic::<Div>::eval(lhs, rhs),
338-
_ => unreachable!("Not an arithmetic operator"),
339-
};
340-
return Ok(result.into());
341-
}
342-
// Integer arithmetic - use checked operations
343-
let result: Option<PrimitiveDatum> = match op {
344-
Operator::Add => CheckedArithmetic::<Add>::checked_eval(lhs, rhs),
345-
Operator::Sub => CheckedArithmetic::<Sub>::checked_eval(lhs, rhs),
346-
Operator::Mul => CheckedArithmetic::<Mul>::checked_eval(lhs, rhs),
347-
Operator::Div => CheckedArithmetic::<Div>::checked_eval(lhs, rhs),
348-
_ => unreachable!("Not an arithmetic operator"),
349-
};
350-
result
351-
.map(|d| d.into())
352-
.ok_or_else(|| vortex_err!("Arithmetic overflow/underflow or type mismatch"))
353-
}
354-
355321
/// Create a new [`Binary`] using the [`Eq`](crate::expr::exprs::operators::Operator::Eq) operator.
356322
///
357323
/// ## Example usage

vortex-array/src/expr/exprs/like.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
use std::fmt::Formatter;
55

66
use prost::Message;
7+
use vortex_compute::arrow::IntoArrow;
8+
use vortex_compute::arrow::IntoVector;
79
use vortex_dtype::DType;
810
use vortex_error::VortexResult;
911
use vortex_error::vortex_bail;
1012
use vortex_error::vortex_err;
1113
use vortex_proto::expr as pb;
1214
use vortex_vector::Datum;
13-
use vortex_vector::bool::BoolVector;
1415

1516
use crate::ArrayRef;
1617
use crate::compute::LikeOptions;
@@ -117,8 +118,8 @@ impl VTable for Like {
117118
.try_into()
118119
.map_err(|_| vortex_err!("Wrong argument count"))?;
119120

120-
let child: Box<dyn arrow_array::Datum> = child.try_into()?;
121-
let pattern: Box<dyn arrow_array::Datum> = pattern.try_into()?;
121+
let child = child.into_arrow()?;
122+
let pattern = pattern.into_arrow()?;
122123

123124
let array = match (options.negated, options.case_insensitive) {
124125
(false, false) => arrow_string::like::like(child.as_ref(), pattern.as_ref()),
@@ -127,7 +128,7 @@ impl VTable for Like {
127128
(true, true) => arrow_string::like::nilike(child.as_ref(), pattern.as_ref()),
128129
}?;
129130

130-
Ok(Datum::Vector(BoolVector::from(&array).into()))
131+
Ok(Datum::Vector(array.into_vector()?.into()).into())
131132
}
132133

133134
fn is_null_sensitive(&self, _instance: &Self::Options) -> bool {

vortex-compute/src/arrow/binaryview.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::sync::Arc;
55

66
use arrow_array::Array;
7-
use arrow_array::ArrayRef;
87
use arrow_array::GenericByteViewArray;
98
use vortex_buffer::Buffer;
109
use vortex_buffer::ByteBuffer;
@@ -21,7 +20,7 @@ use crate::arrow::nulls_to_mask;
2120
macro_rules! impl_binaryview_to_arrow {
2221
($T:ty, $A:ty) => {
2322
impl IntoArrow for BinaryViewVector<$T> {
24-
type Output = ArrayRef;
23+
type Output = GenericByteViewArray<$A>;
2524

2625
fn into_arrow(self) -> VortexResult<Self::Output> {
2726
let (views, buffers, validity) = self.into_parts();
@@ -35,10 +34,9 @@ macro_rules! impl_binaryview_to_arrow {
3534
.collect();
3635

3736
// SAFETY: our own guarantees are the same as Arrow's guarantees for BinaryViewArray
38-
let array = unsafe {
37+
Ok(unsafe {
3938
GenericByteViewArray::<$A>::new_unchecked(views, buffers, validity.into())
40-
};
41-
Ok(Arc::new(array))
39+
})
4240
}
4341
}
4442
};

vortex-compute/src/arrow/bool.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use std::sync::Arc;
5-
64
use arrow_array::Array;
7-
use arrow_array::ArrayRef;
85
use arrow_array::BooleanArray;
96
use vortex_buffer::BitBuffer;
107
use vortex_error::VortexResult;
@@ -15,11 +12,11 @@ use crate::arrow::IntoVector;
1512
use crate::arrow::nulls_to_mask;
1613

1714
impl IntoArrow for BoolVector {
18-
type Output = ArrayRef;
15+
type Output = BooleanArray;
1916

2017
fn into_arrow(self) -> VortexResult<Self::Output> {
2118
let (bits, validity) = self.into_parts();
22-
Ok(Arc::new(BooleanArray::new(bits.into(), validity.into())))
19+
Ok(BooleanArray::new(bits.into(), validity.into()))
2320
}
2421
}
2522

vortex-compute/src/arrow/decimal.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,30 @@ impl IntoArrow for DecimalVector {
2626

2727
fn into_arrow(self) -> VortexResult<Self::Output> {
2828
match self {
29-
DecimalVector::D8(v) => v.into_arrow(),
30-
DecimalVector::D16(v) => v.into_arrow(),
31-
DecimalVector::D32(v) => v.into_arrow(),
32-
DecimalVector::D64(v) => v.into_arrow(),
33-
DecimalVector::D128(v) => v.into_arrow(),
34-
DecimalVector::D256(v) => v.into_arrow(),
29+
DecimalVector::D8(v) => Ok(Arc::new(v.into_arrow()?)),
30+
DecimalVector::D16(v) => Ok(Arc::new(v.into_arrow()?)),
31+
DecimalVector::D32(v) => Ok(Arc::new(v.into_arrow()?)),
32+
DecimalVector::D64(v) => Ok(Arc::new(v.into_arrow()?)),
33+
DecimalVector::D128(v) => Ok(Arc::new(v.into_arrow()?)),
34+
DecimalVector::D256(v) => Ok(Arc::new(v.into_arrow()?)),
3535
}
3636
}
3737
}
3838

3939
macro_rules! impl_decimal_upcast_i32 {
4040
($T:ty) => {
4141
impl IntoArrow for DVector<$T> {
42-
type Output = ArrayRef;
42+
type Output = PrimitiveArray<Decimal32Type>;
4343

4444
fn into_arrow(self) -> VortexResult<Self::Output> {
4545
let (_, elements, validity) = self.into_parts();
4646
// Upcast the DVector to Arrow's smallest decimal type (Decimal32)
4747
let elements =
4848
Buffer::<i32>::from_trusted_len_iter(elements.iter().map(|i| *i as i32));
49-
Ok(Arc::new(PrimitiveArray::<Decimal32Type>::new(
49+
Ok(PrimitiveArray::<Decimal32Type>::new(
5050
elements.into_arrow_scalar_buffer(),
5151
validity.into(),
52-
)))
52+
))
5353
}
5454
}
5555
};
@@ -62,14 +62,14 @@ impl_decimal_upcast_i32!(i16);
6262
macro_rules! impl_decimal_to_arrow {
6363
($T:ty, $A:ty) => {
6464
impl IntoArrow for DVector<$T> {
65-
type Output = ArrayRef;
65+
type Output = PrimitiveArray<$A>;
6666

6767
fn into_arrow(self) -> VortexResult<Self::Output> {
6868
let (_, elements, validity) = self.into_parts();
69-
Ok(Arc::new(PrimitiveArray::<$A>::new(
69+
Ok(PrimitiveArray::<$A>::new(
7070
elements.into_arrow_scalar_buffer(),
7171
validity.into(),
72-
)))
72+
))
7373
}
7474
}
7575
};
@@ -80,7 +80,7 @@ impl_decimal_to_arrow!(i64, Decimal64Type);
8080
impl_decimal_to_arrow!(i128, Decimal128Type);
8181

8282
impl IntoArrow for DVector<i256> {
83-
type Output = ArrayRef;
83+
type Output = PrimitiveArray<Decimal256Type>;
8484

8585
fn into_arrow(self) -> VortexResult<Self::Output> {
8686
let (_, elements, validity) = self.into_parts();
@@ -90,10 +90,10 @@ impl IntoArrow for DVector<i256> {
9090
let elements =
9191
unsafe { std::mem::transmute::<Buffer<i256>, Buffer<arrow_buffer::i256>>(elements) };
9292

93-
Ok(Arc::new(PrimitiveArray::<Decimal256Type>::new(
93+
Ok(PrimitiveArray::<Decimal256Type>::new(
9494
elements.into_arrow_scalar_buffer(),
9595
validity.into(),
96-
)))
96+
))
9797
}
9898
}
9999

vortex-compute/src/arrow/fixed_size_list.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::sync::Arc;
55

66
use arrow_array::Array;
7-
use arrow_array::ArrayRef;
87
use arrow_array::FixedSizeListArray;
98
use arrow_schema::DataType;
109
use arrow_schema::Field;
@@ -18,7 +17,7 @@ use crate::arrow::IntoVector;
1817
use crate::arrow::nulls_to_mask;
1918

2019
impl IntoArrow for FixedSizeListVector {
21-
type Output = ArrayRef;
20+
type Output = FixedSizeListArray;
2221

2322
fn into_arrow(self) -> VortexResult<Self::Output> {
2423
let (elements, list_size, validity) = self.into_parts();
@@ -29,12 +28,12 @@ impl IntoArrow for FixedSizeListVector {
2928
true, // Vectors are always nullable.
3029
));
3130

32-
Ok(Arc::new(FixedSizeListArray::try_new(
31+
Ok(FixedSizeListArray::try_new(
3332
field,
3433
list_size as i32,
3534
converted_elements,
3635
validity.into(),
37-
)?))
36+
)?)
3837
}
3938
}
4039

vortex-compute/src/arrow/list.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use std::sync::Arc;
5-
6-
use arrow_array::ArrayRef;
74
use arrow_array::ListViewArray;
85
use arrow_buffer::ScalarBuffer;
96
use arrow_schema::Field;
@@ -16,7 +13,7 @@ use vortex_vector::match_each_integer_pvector;
1613
use crate::arrow::IntoArrow;
1714

1815
impl IntoArrow for ListViewVector {
19-
type Output = ArrayRef;
16+
type Output = ListViewArray;
2017

2118
#[allow(clippy::unnecessary_fallible_conversions)]
2219
#[allow(clippy::useless_conversion)]
@@ -40,12 +37,12 @@ impl IntoArrow for ListViewVector {
4037
)
4138
});
4239

43-
Ok(Arc::new(ListViewArray::new(
40+
Ok(ListViewArray::new(
4441
FieldRef::new(Field::new("elements", elements.data_type().clone(), true)),
4542
offsets,
4643
sizes,
4744
elements,
4845
validity.into(),
49-
)))
46+
))
5047
}
5148
}

0 commit comments

Comments
 (0)