Skip to content

Commit c4a9ae3

Browse files
committed
More vector scalars
Signed-off-by: Nicholas Gates <[email protected]>
1 parent d2e6eb2 commit c4a9ae3

35 files changed

+382
-114
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
use std::fmt::Formatter;
55

6-
use prost::Message;
7-
use vortex_dtype::{DType, match_each_float_ptype};
8-
use vortex_error::{VortexResult, vortex_bail, vortex_err};
9-
use vortex_proto::expr as pb;
10-
use vortex_scalar::Scalar;
11-
126
use crate::arrays::ConstantArray;
137
use crate::expr::{ChildName, ExprId, Expression, ExpressionView, StatsCatalog, VTable, VTableExt};
148
use crate::stats::Stat;
159
use crate::{Array, ArrayRef, IntoArray};
10+
use prost::Message;
11+
use vortex_dtype::{match_each_float_ptype, DType};
12+
use vortex_error::{vortex_bail, vortex_err, VortexResult};
13+
use vortex_proto::expr as pb;
14+
use vortex_scalar::Scalar;
15+
use vortex_vector::Vector;
1616

1717
/// Expression that represents a literal scalar value.
1818
pub struct Literal;
@@ -73,6 +73,14 @@ impl VTable for Literal {
7373
Ok(ConstantArray::new(expr.data().clone(), scope.len()).into_array())
7474
}
7575

76+
fn execute(
77+
&self,
78+
_expr: &ExpressionView<Self>,
79+
_vector: &Vector,
80+
_dtype: &DType,
81+
) -> VortexResult<Vector> {
82+
}
83+
7684
fn stat_expression(
7785
&self,
7886
expr: &ExpressionView<Self>,

vortex-scalar/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ vortex-dtype = { workspace = true, features = ["arrow"] }
2929
vortex-error = { workspace = true }
3030
vortex-proto = { workspace = true, features = ["scalar"] }
3131
vortex-utils = { workspace = true }
32+
vortex-vector = { workspace = true }
3233

3334
[dev-dependencies]
3435
rstest = { workspace = true }

vortex-scalar/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod struct_;
2828
#[cfg(test)]
2929
mod tests;
3030
mod utf8;
31+
mod vectors;
3132

3233
pub use binary::*;
3334
pub use bool::*;

vortex-scalar/src/vectors.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Conversion logic from this "legacy" scalar crate to Vortex Vector scalars.
5+
6+
use crate::Scalar;
7+
use vortex_dtype::{
8+
match_each_decimal_value_type, match_each_native_ptype, DType, DecimalType, PrecisionScale,
9+
};
10+
use vortex_error::VortexExpect;
11+
use vortex_vector::binaryview::{BinaryViewScalar, StringScalar};
12+
use vortex_vector::bool::BoolScalar;
13+
use vortex_vector::decimal::DScalar;
14+
use vortex_vector::null::NullScalar;
15+
use vortex_vector::primitive::PScalar;
16+
17+
impl Scalar {
18+
pub fn into_vector(self) -> vortex_vector::Scalar {
19+
match self.dtype() {
20+
DType::Null => NullScalar.into(),
21+
DType::Bool(_) => BoolScalar::new(self.as_bool().value()).into(),
22+
DType::Primitive(ptype, _) => {
23+
match_each_native_ptype!(ptype, |T| {
24+
PScalar::new(self.as_primitive().typed_value()).into()
25+
})
26+
}
27+
DType::Decimal(dec_dtype, _) => {
28+
let dscalar = self.as_decimal();
29+
let dec_type = DecimalType::smallest_decimal_value_type(dec_dtype);
30+
match_each_decimal_value_type!(dec_type, |D| {
31+
let ps = PrecisionScale::<D>::new(dec_dtype.precision(), dec_dtype.scale());
32+
DScalar::maybe_new(
33+
ps,
34+
dscalar
35+
.decimal_value()
36+
.map(|d| d.cast::<D>().vortex_expect("Failed to cast decimal value")),
37+
)
38+
.vortex_expect("Failed to create decimal scalar")
39+
.into()
40+
})
41+
}
42+
DType::Utf8(_) => StringScalar::new(self.as_utf8().value()).into(),
43+
DType::Binary(_) => BinaryViewScalar::new(self.as_binary().value()).into(),
44+
DType::List(_, _) => {}
45+
DType::FixedSizeList(_, _, _) => {}
46+
DType::Struct(_, _) => {}
47+
DType::Extension(_) => {}
48+
}
49+
}
50+
}

vortex-vector/src/binaryview/scalar.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use crate::{Scalar, ScalarOps, VectorMutOps};
1010
#[derive(Debug)]
1111
pub struct BinaryViewScalar<T: BinaryViewType>(Option<T::Scalar>);
1212

13-
impl<T: BinaryViewType> From<Option<T::Scalar>> for BinaryViewScalar<T> {
14-
fn from(value: Option<T::Scalar>) -> Self {
13+
impl<T: BinaryViewType> BinaryViewScalar<T> {
14+
/// Creates a new binary view scalar with the given value.
15+
pub fn new(value: Option<T::Scalar>) -> Self {
1516
Self(value)
1617
}
1718
}

vortex-vector/src/binaryview/vector.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use std::ops::RangeBounds;
88
use std::sync::Arc;
99

1010
use vortex_buffer::{Alignment, Buffer, ByteBuffer};
11-
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
11+
use vortex_error::{vortex_ensure, VortexExpect, VortexResult};
1212
use vortex_mask::Mask;
1313

1414
use crate::binaryview::vector_mut::BinaryViewVectorMut;
15-
use crate::binaryview::view::{BinaryView, validate_views};
15+
use crate::binaryview::view::{validate_views, BinaryView};
1616
use crate::binaryview::{BinaryViewScalar, BinaryViewType};
17-
use crate::{Scalar, VectorOps};
17+
use crate::VectorOps;
1818

1919
/// A variable-length binary vector.
2020
///
@@ -193,6 +193,7 @@ impl<T: BinaryViewType> BinaryViewVector<T> {
193193

194194
impl<T: BinaryViewType> VectorOps for BinaryViewVector<T> {
195195
type Mutable = BinaryViewVectorMut<T>;
196+
type Scalar = BinaryViewScalar<T>;
196197

197198
fn len(&self) -> usize {
198199
self.views.len()
@@ -202,9 +203,9 @@ impl<T: BinaryViewType> VectorOps for BinaryViewVector<T> {
202203
&self.validity
203204
}
204205

205-
fn scalar_at(&self, index: usize) -> Scalar {
206+
fn scalar_at(&self, index: usize) -> BinaryViewScalar<T> {
206207
assert!(index < self.len());
207-
BinaryViewScalar::<T>::from(self.get(index)).into()
208+
BinaryViewScalar::<T>::new(self.get(index))
208209
}
209210

210211
fn slice(&self, _range: impl RangeBounds<usize> + Clone + Debug) -> Self {
@@ -281,7 +282,7 @@ impl<T: BinaryViewType> VectorOps for BinaryViewVector<T> {
281282
mod tests {
282283
use std::sync::Arc;
283284

284-
use vortex_buffer::{ByteBuffer, buffer};
285+
use vortex_buffer::{buffer, ByteBuffer};
285286
use vortex_mask::Mask;
286287

287288
use crate::binaryview::view::BinaryView;

vortex-vector/src/binaryview/vector_mut.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
use std::sync::Arc;
77

88
use vortex_buffer::{BufferMut, ByteBuffer, ByteBufferMut};
9-
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
9+
use vortex_error::{vortex_ensure, VortexExpect, VortexResult};
1010
use vortex_mask::MaskMut;
1111

12-
use crate::binaryview::BinaryViewType;
1312
use crate::binaryview::vector::BinaryViewVector;
14-
use crate::binaryview::view::{BinaryView, validate_views};
13+
use crate::binaryview::view::{validate_views, BinaryView};
14+
use crate::binaryview::{BinaryViewScalar, BinaryViewType};
1515
use crate::{VectorMutOps, VectorOps};
1616

1717
// Default capacity for new string data buffers of 2MiB.
@@ -264,6 +264,20 @@ impl<T: BinaryViewType> VectorMutOps for BinaryViewVectorMut<T> {
264264
self.validity.append_n(false, n);
265265
}
266266

267+
fn append_zeros(&mut self, n: usize) {
268+
self.views.push_n(BinaryView::empty_view(), n);
269+
self.validity.append_n(true, n);
270+
}
271+
272+
fn append_scalars(&mut self, scalar: &BinaryViewScalar<T>, n: usize) {
273+
match scalar.value() {
274+
None => self.append_nulls(n),
275+
Some(v) => {
276+
self.append_owned_values(v.clone(), n);
277+
}
278+
}
279+
}
280+
267281
fn freeze(mut self) -> BinaryViewVector<T> {
268282
// Freeze all components, close any in-progress views
269283
self.flush_open_buffer();
@@ -296,7 +310,7 @@ mod tests {
296310
use std::ops::Deref;
297311
use std::sync::Arc;
298312

299-
use vortex_buffer::{ByteBuffer, buffer, buffer_mut};
313+
use vortex_buffer::{buffer, buffer_mut, ByteBuffer};
300314
use vortex_mask::{Mask, MaskMut};
301315

302316
use crate::binaryview::view::BinaryView;

vortex-vector/src/bool/scalar.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ use crate::{Scalar, ScalarOps, VectorMut, VectorMutOps};
88
#[derive(Debug)]
99
pub struct BoolScalar(Option<bool>);
1010

11-
impl From<Option<bool>> for BoolScalar {
12-
fn from(value: Option<bool>) -> Self {
11+
impl BoolScalar {
12+
/// Creates a new bool scalar with the given value.
13+
pub fn new(value: Option<bool>) -> Self {
1314
Self(value)
1415
}
16+
17+
/// Returns the value of the bool scalar, or `None` if the scalar is null.
18+
pub fn value(&self) -> Option<bool> {
19+
self.0
20+
}
1521
}
1622

1723
impl ScalarOps for BoolScalar {

vortex-vector/src/bool/vector.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::fmt::Debug;
77
use std::ops::RangeBounds;
88

99
use vortex_buffer::BitBuffer;
10-
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
10+
use vortex_error::{vortex_ensure, VortexExpect, VortexResult};
1111
use vortex_mask::Mask;
1212

13-
use crate::bool::BoolVectorMut;
14-
use crate::{Scalar, VectorOps};
13+
use crate::bool::{BoolScalar, BoolVectorMut};
14+
use crate::VectorOps;
1515

1616
/// An immutable vector of boolean values.
1717
///
@@ -74,6 +74,7 @@ impl BoolVector {
7474

7575
impl VectorOps for BoolVector {
7676
type Mutable = BoolVectorMut;
77+
type Scalar = BoolScalar;
7778

7879
fn len(&self) -> usize {
7980
debug_assert!(self.validity.len() == self.bits.len());
@@ -84,13 +85,13 @@ impl VectorOps for BoolVector {
8485
&self.validity
8586
}
8687

87-
fn scalar_at(&self, index: usize) -> Scalar {
88+
fn scalar_at(&self, index: usize) -> BoolScalar {
8889
assert!(index < self.len());
8990

9091
let is_valid = self.validity.value(index);
9192
let value = is_valid.then(|| self.bits.value(index));
9293

93-
Scalar::Bool(value.into())
94+
BoolScalar::new(value)
9495
}
9596

9697
fn slice(&self, range: impl RangeBounds<usize> + Clone + Debug) -> Self {

0 commit comments

Comments
 (0)