Skip to content

Commit

Permalink
chore: Move physical type types, add in addressable traits (#3390)
Browse files Browse the repository at this point in the history
Split out from #3382
  • Loading branch information
scsmithr authored Jan 8, 2025
1 parent 600a7e7 commit d262071
Show file tree
Hide file tree
Showing 86 changed files with 314 additions and 218 deletions.
25 changes: 13 additions & 12 deletions crates/rayexec_execution/src/arrays/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod buffer_manager;
pub mod physical_type;

mod raw;
mod shared_or_owned;
Expand All @@ -7,18 +8,7 @@ use std::fmt::Debug;
use std::sync::Arc;

use half::f16;
use rayexec_error::{not_implemented, RayexecError, Result, ResultExt};
use shared_or_owned::SharedOrOwned;

use crate::arrays::bitmap::Bitmap;
use crate::arrays::datatype::DataType;
use crate::arrays::executor::builder::{
ArrayBuilder,
BooleanBuffer,
GermanVarlenBuffer,
PrimitiveBuffer,
};
use crate::arrays::executor::physical_type::{
use physical_type::{
PhysicalAny,
PhysicalBinary,
PhysicalBool,
Expand All @@ -39,6 +29,17 @@ use crate::arrays::executor::physical_type::{
PhysicalU8,
PhysicalUtf8,
};
use rayexec_error::{not_implemented, RayexecError, Result, ResultExt};
use shared_or_owned::SharedOrOwned;

use crate::arrays::bitmap::Bitmap;
use crate::arrays::datatype::DataType;
use crate::arrays::executor::builder::{
ArrayBuilder,
BooleanBuffer,
GermanVarlenBuffer,
PrimitiveBuffer,
};
use crate::arrays::executor::scalar::UnaryExecutor;
use crate::arrays::scalar::decimal::{Decimal128Scalar, Decimal64Scalar};
use crate::arrays::scalar::interval::Interval;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::fmt::Debug;
use std::fmt::{self, Debug};

use half::f16;
use rayexec_error::{RayexecError, Result, ResultExt};
use rayexec_proto::ProtoConv;

use super::builder::{ArrayDataBuffer, BooleanBuffer, GermanVarlenBuffer, PrimitiveBuffer};
use crate::arrays::array::{Array, ArrayData, BinaryData};
use crate::arrays::executor::builder::{
ArrayDataBuffer,
BooleanBuffer,
GermanVarlenBuffer,
PrimitiveBuffer,
};
use crate::arrays::scalar::interval::Interval;
use crate::arrays::storage::{
AddressableStorage,
Expand Down Expand Up @@ -40,6 +45,8 @@ pub enum PhysicalType {
Binary,
Utf8,
List,
Struct,
Dictionary,
}

impl PhysicalType {
Expand Down Expand Up @@ -68,10 +75,43 @@ impl PhysicalType {
array: Array::new_untyped_null_array(0),
}
.into(),
_ => unimplemented!(),
}
}

pub const fn as_str(&self) -> &'static str {
match self {
Self::UntypedNull => "UntypedNull",
Self::Boolean => "Boolean",
Self::Int8 => "Int8",
Self::Int16 => "Int16",
Self::Int32 => "Int32",
Self::Int64 => "Int64",
Self::Int128 => "Int128",
Self::UInt8 => "UInt8",
Self::UInt16 => "UInt16",
Self::UInt32 => "UInt32",
Self::UInt64 => "UInt64",
Self::UInt128 => "UInt128",
Self::Float16 => "Float16",
Self::Float32 => "Float32",
Self::Float64 => "Float64",
Self::Interval => "Interval",
Self::Binary => "Binary",
Self::Utf8 => "Utf8",
Self::List => "List",
Self::Struct => "Struct",
Self::Dictionary => "Dictionary",
}
}
}

impl fmt::Display for PhysicalType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

impl ProtoConv for PhysicalType {
type ProtoType = rayexec_proto::generated::physical_type::PhysicalType;

Expand All @@ -96,6 +136,8 @@ impl ProtoConv for PhysicalType {
Self::Utf8 => Self::ProtoType::Utf8,
Self::Binary => Self::ProtoType::Binary,
Self::List => Self::ProtoType::List,
Self::Struct => Self::ProtoType::Struct,
Self::Dictionary => Self::ProtoType::Dictionary,
})
}

Expand All @@ -121,10 +163,81 @@ impl ProtoConv for PhysicalType {
Self::ProtoType::Utf8 => Self::Utf8,
Self::ProtoType::Binary => Self::Binary,
Self::ProtoType::List => Self::List,
Self::ProtoType::Struct => Self::Struct,
Self::ProtoType::Dictionary => Self::Dictionary,
})
}
}

/// Represents an in-memory array that can be indexed into to retrieve values.
pub trait Addressable: Debug {
/// The type that get's returned.
type T: Send + Debug + ?Sized;

fn len(&self) -> usize;

fn is_empty(&self) -> bool {
self.len() == 0
}

/// Get a value at the given index.
fn get(&self, idx: usize) -> Option<&Self::T>;
}

impl<T> Addressable for &[T]
where
T: Debug + Send,
{
type T = T;

fn len(&self) -> usize {
(**self).len()
}

fn get(&self, idx: usize) -> Option<&Self::T> {
(**self).get(idx)
}
}

/// Represents in-memory storage that we can get mutable references to.
pub trait AddressableMut: Debug {
type T: Debug + ?Sized;

fn len(&self) -> usize;

fn is_empty(&self) -> bool {
self.len() == 0
}

/// Get a mutable reference to a value at the given index.
fn get_mut(&mut self, idx: usize) -> Option<&mut Self::T>;

/// Put a value at the given index.
///
/// Should panic if index is out of bounds.
fn put(&mut self, idx: usize, val: &Self::T);
}

impl<T> AddressableMut for &mut [T]
where
T: Debug + Send + Copy,
{
type T = T;

fn len(&self) -> usize {
(**self).len()
}

fn get_mut(&mut self, idx: usize) -> Option<&mut Self::T> {
(**self).get_mut(idx)
}

fn put(&mut self, idx: usize, val: &Self::T) {
self[idx] = *val;
}
}

// TODO: Remove
/// Types able to convert themselves to byte slices.
pub trait AsBytes {
fn as_bytes(&self) -> &[u8];
Expand Down Expand Up @@ -154,6 +267,7 @@ impl AsBytes for &[u8] {
}
}

// TODO: Remove
/// Types that can be converted from bytes.
///
/// This should not be implemented for `&str`/`&[u8]`.
Expand All @@ -174,19 +288,20 @@ impl VarlenType for [u8] {
}

/// Helper trait for getting the underlying data for an array.
///
/// Contains a lifetime to enable tying the returned storage to the provided
/// array data.
pub trait PhysicalStorage: Debug + Sync + Send + Clone + Copy + 'static {
// TODO: Remove
/// The type that gets returned from the underlying array storage.
type Type<'a>: Sync + Send;
// TODO: Remove
/// The type of the underlying array storage.
type Storage<'a>: AddressableStorage<T = Self::Type<'a>>;

// TODO: Remove
/// Gets the storage for the array that we can access directly.
fn get_storage(data: &ArrayData) -> Result<Self::Storage<'_>>;
}

// TODO: Remove
/// Type that's able to be used for any physical type.
///
/// While this allows any array type to used in the executors, there's no way to
Expand Down
20 changes: 10 additions & 10 deletions crates/rayexec_execution/src/arrays/compute/cast/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,7 @@ use super::parse::{
UInt64Parser,
UInt8Parser,
};
use crate::arrays::array::{Array, ArrayData};
use crate::arrays::bitmap::Bitmap;
use crate::arrays::datatype::{DataType, TimeUnit};
use crate::arrays::executor::builder::{
ArrayBuilder,
BooleanBuffer,
GermanVarlenBuffer,
PrimitiveBuffer,
};
use crate::arrays::executor::physical_type::{
use crate::arrays::array::physical_type::{
PhysicalBool,
PhysicalF16,
PhysicalF32,
Expand All @@ -75,6 +66,15 @@ use crate::arrays::executor::physical_type::{
PhysicalU8,
PhysicalUtf8,
};
use crate::arrays::array::{Array, ArrayData};
use crate::arrays::bitmap::Bitmap;
use crate::arrays::datatype::{DataType, TimeUnit};
use crate::arrays::executor::builder::{
ArrayBuilder,
BooleanBuffer,
GermanVarlenBuffer,
PrimitiveBuffer,
};
use crate::arrays::executor::scalar::UnaryExecutor;
use crate::arrays::scalar::decimal::{Decimal128Type, Decimal64Type, DecimalType};
use crate::arrays::storage::{AddressableStorage, PrimitiveStorage};
Expand Down
2 changes: 1 addition & 1 deletion crates/rayexec_execution/src/arrays/compute/date.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::{DateTime, Datelike, NaiveDate, Timelike, Utc};
use rayexec_error::{not_implemented, RayexecError, Result};

use crate::arrays::array::physical_type::{PhysicalI32, PhysicalI64};
use crate::arrays::array::Array;
use crate::arrays::datatype::{DataType, DecimalTypeMeta, TimeUnit};
use crate::arrays::executor::builder::{ArrayBuilder, PrimitiveBuffer};
use crate::arrays::executor::physical_type::{PhysicalI32, PhysicalI64};
use crate::arrays::executor::scalar::UnaryExecutor;
use crate::arrays::scalar::decimal::{Decimal64Type, DecimalType};

Expand Down
2 changes: 1 addition & 1 deletion crates/rayexec_execution/src/arrays/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rayexec_error::{not_implemented, OptionExt, RayexecError, Result, ResultExt}
use rayexec_proto::ProtoConv;
use serde::{Deserialize, Serialize};

use crate::arrays::executor::physical_type::PhysicalType;
use crate::arrays::array::physical_type::PhysicalType;
use crate::arrays::field::Field;
use crate::arrays::scalar::decimal::{Decimal128Type, Decimal64Type, DecimalType};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rayexec_error::{RayexecError, Result};

use super::{AggregateState, RowToStateMapping};
use crate::arrays::array::physical_type::PhysicalStorage;
use crate::arrays::array::Array;
use crate::arrays::executor::physical_type::PhysicalStorage;
use crate::arrays::executor::scalar::check_validity;
use crate::arrays::selection;
use crate::arrays::storage::AddressableStorage;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl BinaryNonNullUpdater {
#[cfg(test)]
mod tests {
use super::*;
use crate::arrays::executor::physical_type::PhysicalI32;
use crate::arrays::array::physical_type::PhysicalI32;

// SUM(col) + PRODUCT(col)
#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rayexec_error::Result;

use super::{AggregateState, RowToStateMapping};
use crate::arrays::array::physical_type::PhysicalStorage;
use crate::arrays::array::Array;
use crate::arrays::executor::physical_type::PhysicalStorage;
use crate::arrays::selection;
use crate::arrays::storage::AddressableStorage;

Expand Down Expand Up @@ -60,7 +60,7 @@ impl UnaryNonNullUpdater {
#[cfg(test)]
mod tests {
use super::*;
use crate::arrays::executor::physical_type::{PhysicalI32, PhysicalUtf8};
use crate::arrays::array::physical_type::{PhysicalI32, PhysicalUtf8};

#[derive(Debug, Default)]
struct TestSumState {
Expand Down
2 changes: 1 addition & 1 deletion crates/rayexec_execution/src/arrays/executor/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use std::sync::Arc;

use super::physical_type::{AsBytes, VarlenType};
use crate::arrays::array::physical_type::{AsBytes, VarlenType};
use crate::arrays::array::{ArrayData, BinaryData};
use crate::arrays::bitmap::Bitmap;
use crate::arrays::datatype::DataType;
Expand Down
1 change: 0 additions & 1 deletion crates/rayexec_execution/src/arrays/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod aggregate;
pub mod builder;
pub mod physical_type;
pub mod scalar;
4 changes: 2 additions & 2 deletions crates/rayexec_execution/src/arrays/executor/scalar/binary.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rayexec_error::Result;

use super::check_validity;
use crate::arrays::array::physical_type::PhysicalStorage;
use crate::arrays::array::Array;
use crate::arrays::bitmap::Bitmap;
use crate::arrays::executor::builder::{ArrayBuilder, ArrayDataBuffer, OutputBuffer};
use crate::arrays::executor::physical_type::PhysicalStorage;
use crate::arrays::executor::scalar::validate_logical_len;
use crate::arrays::selection;
use crate::arrays::storage::AddressableStorage;
Expand Down Expand Up @@ -95,9 +95,9 @@ mod tests {
use selection::SelectionVector;

use super::*;
use crate::arrays::array::physical_type::{PhysicalI32, PhysicalUtf8};
use crate::arrays::datatype::DataType;
use crate::arrays::executor::builder::{GermanVarlenBuffer, PrimitiveBuffer};
use crate::arrays::executor::physical_type::{PhysicalI32, PhysicalUtf8};
use crate::arrays::scalar::ScalarValue;

#[test]
Expand Down
Loading

0 comments on commit d262071

Please sign in to comment.