Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(query): migrate agg (count, sum, avg) and combinators into v2 #7615

Merged
merged 15 commits into from
Sep 16, 2022
Merged
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/common/hashtable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test = false
common-base = { path = "../base" }

# Crates.io dependencies
ordered-float = "3.0.0"
ordered-float = { git = "https://github.com/andylokandy/rust-ordered-float.git", branch = "as", features = ["serde"] }
BohuTANG marked this conversation as resolved.
Show resolved Hide resolved
primitive-types = "0.11.1"

[dev-dependencies]
Expand Down
1 change: 0 additions & 1 deletion src/query/expression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_mut_refs)]
#![feature(generic_const_exprs)]

#![allow(incomplete_features)]

#[allow(dead_code)]
Expand Down
6 changes: 4 additions & 2 deletions src/query/expression/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ pub const ALL_INTEGER_TYPES: &[NumberDataType; 8] = &[
NumberDataType::Int64,
];

pub const ALL_FLOAT_TYPES: &[NumberDataType; 2] = &[NumberDataType::Float32, NumberDataType::Float64];
pub const ALL_NUMERICS_TYPES: &[NumberDataType; 10] = &concat_array(ALL_INTEGER_TYPES, ALL_FLOAT_TYPES);
pub const ALL_FLOAT_TYPES: &[NumberDataType; 2] =
&[NumberDataType::Float32, NumberDataType::Float64];
pub const ALL_NUMERICS_TYPES: &[NumberDataType; 10] =
&concat_array(ALL_INTEGER_TYPES, ALL_FLOAT_TYPES);

impl DataType {
pub fn wrap_nullable(&self) -> Self {
Expand Down
46 changes: 27 additions & 19 deletions src/query/expression/src/types/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use ordered_float::OrderedFloat;
use serde::Deserialize;
use serde::Serialize;

use crate::ColumnBuilder;
use crate::property::Domain;
use crate::types::ArgType;
use crate::types::DataType;
Expand All @@ -33,6 +32,7 @@ use crate::types::ValueType;
use crate::util::buffer_into_mut;
use crate::values::Column;
use crate::values::Scalar;
use crate::ColumnBuilder;
use crate::ScalarRef;

pub type F32 = OrderedFloat<f32>;
Expand All @@ -52,11 +52,10 @@ pub type UInt64Type = NumberType<u64>;
pub type Float32Type = NumberType<F32>;
pub type Float64Type = NumberType<F64>;


impl <Num: Number> NumberType<Num> {
pub fn try_downcast_builder<'a>(builder: &'a mut ColumnBuilder) -> Option<&'a mut Vec<Num>> {
impl<Num: Number> NumberType<Num> {
pub fn try_downcast_builder<'a>(builder: &'a mut ColumnBuilder) -> Option<&'a mut Vec<Num>> {
match builder {
ColumnBuilder::Number(num) => Num::try_downcast_builder(num) ,
ColumnBuilder::Number(num) => Num::try_downcast_builder(num),
_ => None,
}
}
Expand Down Expand Up @@ -831,8 +830,6 @@ fn overflow_cast<T: Number, U: Number>(src: T) -> (U, bool) {
(dest, overflowing)
}



#[macro_export]
macro_rules! with_number_type {
( | $t:tt | $($tail:tt)* ) => {
Expand All @@ -858,15 +855,27 @@ macro_rules! with_number_mapped_type {
}

pub trait Number:
Copy + Debug + NumCast + Default + Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Sync + Send + 'static
Copy
+ Debug
+ NumCast
+ Default
+ Clone
+ Copy
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Sync
+ Send
+ 'static
{
const MIN: Self;
const MAX: Self;

fn data_type() -> NumberDataType;
fn try_downcast_scalar(scalar: &NumberScalar) -> Option<Self>;
fn try_downcast_column(col: &NumberColumn) -> Option<Buffer<Self>>;
//TODO
// TODO
fn try_downcast_builder(col: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>>;
fn try_downcast_domain(domain: &NumberDomain) -> Option<SimpleDomain<Self>>;
fn upcast_scalar(scalar: Self) -> NumberScalar;
Expand Down Expand Up @@ -927,7 +936,7 @@ impl Number for u16 {
col.as_u_int16().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_u_int16_mut()
}

Expand Down Expand Up @@ -964,7 +973,7 @@ impl Number for u32 {
col.as_u_int32().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_u_int32_mut()
}

Expand Down Expand Up @@ -1001,7 +1010,7 @@ impl Number for u64 {
col.as_u_int64().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_u_int64_mut()
}

Expand Down Expand Up @@ -1038,7 +1047,7 @@ impl Number for i8 {
col.as_int8().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_int8_mut()
}

Expand Down Expand Up @@ -1075,7 +1084,7 @@ impl Number for i16 {
col.as_int16().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_int16_mut()
}

Expand Down Expand Up @@ -1112,7 +1121,7 @@ impl Number for i32 {
col.as_int32().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_int32_mut()
}

Expand Down Expand Up @@ -1149,15 +1158,14 @@ impl Number for i64 {
col.as_int64().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_int64_mut()
}

fn try_downcast_domain(domain: &NumberDomain) -> Option<SimpleDomain<Self>> {
domain.as_int64().cloned()
}


fn upcast_scalar(scalar: Self) -> NumberScalar {
NumberScalar::Int64(scalar)
}
Expand Down Expand Up @@ -1187,7 +1195,7 @@ impl Number for F32 {
col.as_float32().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_float32_mut()
}

Expand Down Expand Up @@ -1224,7 +1232,7 @@ impl Number for F64 {
col.as_float64().cloned()
}

fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
fn try_downcast_builder(builder: &mut NumberColumnBuilder) -> Option<&mut Vec<Self>> {
builder.as_float64_mut()
}

Expand Down
12 changes: 11 additions & 1 deletion src/query/expression/src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::ops::Range;

use common_arrow::arrow::buffer::Buffer;
use common_arrow::arrow::trusted_len::TrustedLen;
use serde::Deserialize;
use serde::Serialize;

use crate::property::Domain;
use crate::types::ArgType;
Expand Down Expand Up @@ -200,7 +202,7 @@ impl<'a> Iterator for StringIterator<'a> {

unsafe impl<'a> TrustedLen for StringIterator<'a> {}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StringColumnBuilder {
pub data: Vec<u8>,
pub offsets: Vec<u64>,
Expand Down Expand Up @@ -284,6 +286,14 @@ impl StringColumnBuilder {
assert_eq!(self.offsets.len(), 2);
self.data[(self.offsets[0] as usize)..(self.offsets[1] as usize)].to_vec()
}

/// # Safety
pub unsafe fn index_unchecked(&self, row: usize) -> &[u8] {
let start = *self.offsets.get_unchecked(row) as usize;
let end = *self.offsets.get_unchecked(row + 1) as usize;
// soundness: the invariant of the struct
self.data.get_unchecked(start..end)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
1 change: 0 additions & 1 deletion src/query/expression/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub fn deserialize_arrow_array(bytes: &[u8]) -> Option<Box<dyn Array>> {
Some(col)
}


pub const fn concat_array<T, const A: usize, const B: usize>(a: &[T; A], b: &[T; B]) -> [T; A + B] {
let mut result = std::mem::MaybeUninit::uninit();
let dest = result.as_mut_ptr() as *mut T;
Expand Down