Skip to content

Instantly share code, notes, and snippets.

@kszucs
Last active March 20, 2018 15:10
Show Gist options
  • Save kszucs/5e49d6f738fee38c949d17f17c136afd to your computer and use it in GitHub Desktop.
Save kszucs/5e49d6f738fee38c949d17f17c136afd to your computer and use it in GitHub Desktop.
arrow-rs datatypes sample
use dtypes::{DataType, PrimitiveType};
pub type Buffer<T> = RawVec<T>;
pub type BitMap = Buffer<bool>;
trait Array<T: DataType> {
fn new(dtype: T) -> Self;
}
pub struct PrimitiveArray<T: PrimitiveType> {
len: usize,
nulls: BitMap,
dtype: T,
values: Buffer<T::Item>
}
pub struct ListArray<T: DataType> {
len: usize,
nulls: BitMap,
dtype: T,
offsets: Buffer<i32>,
values: T::Array,
}
struct StructArray<T: DataType> {
len: usize,
nulls: BitMap,
children: Vec<Box<Array>>
}
impl<T: PrimitiveType> Array<T> for PrimitiveArray<T> {
fn new(dtype: T) -> PrimitiveArray<T> {
PrimitiveArray {
len: 0,
nulls: BitMap::new(),
dtype: dtype,
values: Buffer::new()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use dtypes::*;
#[test]
fn test_length() {
// let a = PrimitiveArray::new(Int32);
// let b = PrimitiveArray::new(Int64);
let a = PrimitiveArray::new(Int64);
let b = PrimitiveArray::new(Int32);
let c = PrimitiveArray::new(UInt32);
//let a = Primitive::with_dtype(Int32);
//let a = Array::new(dtype);
}
}
use std::mem;
use std::fmt;
use array::PrimitiveArray;
//TODO: default implementations
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum TimeUnit {
Second,
Milli,
Micro,
Nano
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum DateUnit {
Day,
Milli
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum IntervalUnit {
YearMonth,
DayTime
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Precision {
Half,
Single,
Double
}
pub struct Boolean;
pub struct Int8;
pub struct Int16;
pub struct Int32;
pub struct Int64;
pub struct UInt8;
pub struct UInt16;
pub struct UInt32;
pub struct UInt64;
// struct HalfFloat;
struct Float32;
struct Float64;
struct Decimal {
precision: i32,
scale: i32
}
// type String = String;
// type Binary =
// FixedSizedBinary(i32), // byte_width
struct Time32(TimeUnit);
struct Time64(TimeUnit);
struct Date32(DateUnit);
struct Date64(DateUnit);
struct Interval(IntervalUnit);
struct List<T: DataType>(T);
// every datatype mmust have an array type, nested types
pub trait DataType {
type Array;
fn name(&self) -> &str;
fn bits(&self) -> usize;
}
pub trait PrimitiveType: DataType {
type Item;
}
pub trait FloatingType: PrimitiveType {
fn precision(&self) -> Precision;
}
pub trait NestedType: DataType {
}
// rename to numeric?
macro_rules! primitive {
($DT:ty, $T:ty, $name:expr) => (
impl DataType for $DT {
type Array = PrimitiveArray<Self>;
fn name(&self) -> &str {
$name
}
fn bits(&self) -> usize {
mem::size_of::<$T>() * 8
}
}
impl PrimitiveType for $DT {
type Item = $T;
}
// impl fmt::Display for $DT {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// write!(f, "{}", self.name())
// }
// }
)
}
macro_rules! floating {
($DT:ty, $precision:expr) => (
impl FloatingType for $DT {
fn precision(&self) -> Precision {
$precision
}
}
)
}
primitive!(Int8, i8, "int8");
primitive!(Int16, i16, "int16");
primitive!(Int32, i32, "int32");
primitive!(Int64, i64, "int64");
primitive!(UInt8, u8, "uint8");
primitive!(UInt16, u16, "uint16");
primitive!(UInt32, u32, "uint32");
primitive!(UInt64, u64, "uint64");
primitive!(Float32, f32, "float32"); // Float
primitive!(Float64, f64, "float64"); // Double
floating!(Float32, Precision::Single);
floating!(Float64, Precision::Double);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment