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

improve(function): Decouple Function with DataField #5119

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/functions/src/scalars/arithmetics/arithmetic_modulo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ where
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
_input_rows: usize,
) -> Result<ColumnRef> {
let lhs = columns[0].column();
let rhs = columns[1].column();
let lhs = &columns[0];
let rhs = &columns[1];
match (lhs.is_const(), rhs.is_const()) {
(false, true) => {
let left: &<L as Scalar>::ColumnType = unsafe { Series::static_cast(lhs) };
Expand Down
6 changes: 3 additions & 3 deletions common/functions/src/scalars/arithmetics/binary_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ where
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
_input_rows: usize,
) -> Result<ColumnRef> {
let col = scalar_binary_op(
columns[0].column(),
columns[1].column(),
&columns[0],
&columns[1],
self.func.clone(),
&mut EvalContext::default(),
)?;
Expand Down
8 changes: 2 additions & 6 deletions common/functions/src/scalars/arithmetics/unary_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,10 @@ where
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
_input_rows: usize,
) -> Result<ColumnRef> {
let col = scalar_unary_op(
columns[0].column(),
self.func.clone(),
&mut EvalContext::default(),
)?;
let col = scalar_unary_op(&columns[0], self.func.clone(), &mut EvalContext::default())?;
Ok(Arc::new(col))
}

Expand Down
37 changes: 14 additions & 23 deletions common/functions/src/scalars/comparisons/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use num::traits::AsPrimitive;

use super::utils::*;
use crate::scalars::assert_string;
use crate::scalars::cast_column_field;
use crate::scalars::cast_column;
use crate::scalars::primitive_simd_op_boolean;
use crate::scalars::scalar_binary_op;
use crate::scalars::ComparisonEqFunction;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Function for ComparisonFunction {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
_input_rows: usize,
) -> Result<ColumnRef> {
let col = self.func.eval(&columns[0], &columns[1])?;
Expand Down Expand Up @@ -236,7 +236,7 @@ pub trait ComparisonImpl: Sync + Send + Clone + 'static {
}

pub trait ComparisonExpression: Sync + Send {
fn eval(&self, l: &ColumnWithField, r: &ColumnWithField) -> Result<BooleanColumn>;
fn eval(&self, l: &ColumnRef, r: &ColumnRef) -> Result<BooleanColumn>;
}

pub struct ComparisonScalarImpl<L: Scalar, R: Scalar, F> {
Expand All @@ -261,13 +261,8 @@ where
R: Scalar + Send + Sync + Clone,
F: Fn(L::RefType<'_>, R::RefType<'_>, &mut EvalContext) -> bool + Send + Sync + Clone,
{
fn eval(&self, l: &ColumnWithField, r: &ColumnWithField) -> Result<BooleanColumn> {
scalar_binary_op(
l.column(),
r.column(),
self.func.clone(),
&mut EvalContext::default(),
)
fn eval(&self, l: &ColumnRef, r: &ColumnRef) -> Result<BooleanColumn> {
scalar_binary_op(l, r, self.func.clone(), &mut EvalContext::default())
}
}

Expand Down Expand Up @@ -298,17 +293,17 @@ where
T: PrimitiveType + comparison::Simd8 + Send + Sync + Clone,
F: Fn(T::Simd, T::Simd) -> u8 + Send + Sync + Clone,
{
fn eval(&self, l: &ColumnWithField, r: &ColumnWithField) -> Result<BooleanColumn> {
let lhs = if self.need_cast && l.data_type() != &self.least_supertype {
cast_column_field(l, &self.least_supertype)?
fn eval(&self, l: &ColumnRef, r: &ColumnRef) -> Result<BooleanColumn> {
let lhs = if self.need_cast && l.data_type() != self.least_supertype {
cast_column(l, &self.least_supertype)?
} else {
l.column().clone()
l.clone()
};

let rhs = if self.need_cast && r.data_type() != &self.least_supertype {
cast_column_field(r, &self.least_supertype)?
let rhs = if self.need_cast && r.data_type() != self.least_supertype {
cast_column(r, &self.least_supertype)?
} else {
r.column().clone()
r.clone()
};
primitive_simd_op_boolean::<T, F>(&lhs, &rhs, self.func.clone())
}
Expand All @@ -327,9 +322,7 @@ impl<F: BooleanSimdImpl> ComparisonBooleanImpl<F> {
}

impl<F: BooleanSimdImpl> ComparisonExpression for ComparisonBooleanImpl<F> {
fn eval(&self, l: &ColumnWithField, r: &ColumnWithField) -> Result<BooleanColumn> {
let lhs = l.column();
let rhs = r.column();
fn eval(&self, lhs: &ColumnRef, rhs: &ColumnRef) -> Result<BooleanColumn> {
let res = match (lhs.is_const(), rhs.is_const()) {
(false, false) => {
let lhs: &BooleanColumn = Series::check_get(lhs)?;
Expand Down Expand Up @@ -369,9 +362,7 @@ impl<T: StringSearchImpl> ComparisonStringImpl<T> {
}

impl<T: StringSearchImpl> ComparisonExpression for ComparisonStringImpl<T> {
fn eval(&self, l: &ColumnWithField, r: &ColumnWithField) -> Result<BooleanColumn> {
let lhs = l.column();
let rhs = r.column();
fn eval(&self, lhs: &ColumnRef, rhs: &ColumnRef) -> Result<BooleanColumn> {
let res = match rhs.is_const() {
true => {
let lhs: &StringColumn = Series::check_get(lhs)?;
Expand Down
48 changes: 20 additions & 28 deletions common/functions/src/scalars/conditionals/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use common_datavalues::type_coercion::aggregate_types;
use common_datavalues::with_match_scalar_type;
use common_exception::Result;

use crate::scalars::cast_column_field;
use crate::scalars::cast_column;
use crate::scalars::Function;
use crate::scalars::FunctionContext;
use crate::scalars::FunctionDescription;
Expand Down Expand Up @@ -53,18 +53,14 @@ impl IfFunction {
}

// handle cond is const or nullable or null column
fn eval_cond_const(
&self,
cond_col: &ColumnRef,
columns: &ColumnsWithField,
) -> Result<ColumnRef> {
fn eval_cond_const(&self, cond_col: &ColumnRef, columns: &[ColumnRef]) -> Result<ColumnRef> {
debug_assert!(cond_col.is_const());
// whether nullable or not, we can use viewer to make it
let cond_viewer = bool::try_create_viewer(cond_col)?;
if cond_viewer.value_at(0) {
return Ok(columns[0].column().clone());
Ok(columns[0].clone())
} else {
return Ok(columns[1].column().clone());
Ok(columns[1].clone())
}
}

Expand All @@ -75,18 +71,18 @@ impl IfFunction {
fn eval_const(
&self,
cond_col: &BooleanColumn,
columns: &ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<ColumnRef> {
debug_assert!(columns[0].column().is_const() || columns[1].column().is_const());
let (lhs_col, rhs_col, reverse) = if columns[0].column().is_const() {
debug_assert!(columns[0].is_const() || columns[1].is_const());
let (lhs_col, rhs_col, reverse) = if columns[0].is_const() {
(&columns[0], &columns[1], false)
} else {
(&columns[1], &columns[0], true)
};

let lhs = cast_column_field(lhs_col, &self.least_supertype)?;
let rhs = cast_column_field(rhs_col, &self.least_supertype)?;
let lhs = cast_column(lhs_col, &self.least_supertype)?;
let rhs = cast_column(rhs_col, &self.least_supertype)?;

let type_id = remove_nullable(&lhs.data_type()).data_type_id();

Expand All @@ -110,7 +106,7 @@ impl IfFunction {
}
}
return Ok(builder.build(input_rows));
}else {
} else {
for (predicate, (row, r_val)) in iter {
if predicate {
builder.append(l_val, true);
Expand Down Expand Up @@ -179,14 +175,14 @@ impl IfFunction {
fn eval_nullable(
&self,
cond_col: &BooleanColumn,
columns: &ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<ColumnRef> {
let lhs_col = &columns[0];
let rhs_col = &columns[1];

let lhs = cast_column_field(lhs_col, &self.least_supertype)?;
let rhs = cast_column_field(rhs_col, &self.least_supertype)?;
let lhs = cast_column(lhs_col, &self.least_supertype)?;
let rhs = cast_column(rhs_col, &self.least_supertype)?;

let type_id = remove_nullable(&self.least_supertype).data_type_id();

Expand All @@ -213,16 +209,12 @@ impl IfFunction {
}

// handle when both are not nullable or const
fn eval_generic(
&self,
cond_col: &BooleanColumn,
columns: &ColumnsWithField,
) -> Result<ColumnRef> {
fn eval_generic(&self, cond_col: &BooleanColumn, columns: &[ColumnRef]) -> Result<ColumnRef> {
let lhs_col = &columns[0];
let rhs_col = &columns[1];

let lhs = cast_column_field(lhs_col, &self.least_supertype)?;
let rhs = cast_column_field(rhs_col, &self.least_supertype)?;
let lhs = cast_column(lhs_col, &self.least_supertype)?;
let rhs = cast_column(rhs_col, &self.least_supertype)?;

debug_assert!(!self.least_supertype.is_nullable());
let type_id = self.least_supertype.data_type_id();
Expand Down Expand Up @@ -257,10 +249,10 @@ impl Function for IfFunction {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<ColumnRef> {
let cond_col = columns[0].column();
let cond_col = &columns[0];
let cond_col = DataBlock::cast_to_nonull_boolean(cond_col)?;

// 1. fast path for cond nullable or const or null column
Expand All @@ -271,13 +263,13 @@ impl Function for IfFunction {
let cond_col = Series::check_get_scalar::<bool>(&cond_col)?;

// 2. handle when lhs / rhs is const
if columns[1].column().is_const() || columns[2].column().is_const() {
if columns[1].is_const() || columns[2].is_const() {
return self.eval_const(cond_col, &columns[1..], input_rows);
}

// 3. handle nullable column
let whether_nullable = |col: &ColumnRef| col.is_nullable() || col.data_type().is_null();
if whether_nullable(columns[1].column()) || whether_nullable(columns[2].column()) {
if whether_nullable(&columns[1]) || whether_nullable(&columns[2]) {
return self.eval_nullable(cond_col, &columns[1..], input_rows);
}

Expand Down
23 changes: 9 additions & 14 deletions common/functions/src/scalars/conditionals/in_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use common_exception::ErrorCode;
use common_exception::Result;
use ordered_float::OrderedFloat;

use crate::scalars::cast_column_field;
use crate::scalars::cast_column;
use crate::scalars::Function;
use crate::scalars::FunctionContext;
use crate::scalars::FunctionDescription;
Expand Down Expand Up @@ -63,7 +63,7 @@ macro_rules! scalar_contains {
let mut builder: ColumnBuilder<bool> = ColumnBuilder::with_capacity($ROWS);
let mut vals_set = HashSet::with_capacity($ROWS - 1);
for col in &$COLUMNS[1..] {
let col = cast_column_field(col, &$CAST_TYPE)?;
let col = cast_column(col, &$CAST_TYPE)?;
let col_viewer = $T::try_create_viewer(&col)?;
if col_viewer.valid_at(0) {
let val = col_viewer.value_at(0).to_owned_scalar();
Expand All @@ -85,7 +85,7 @@ macro_rules! float_contains {
let mut builder: ColumnBuilder<bool> = ColumnBuilder::with_capacity($ROWS);
let mut vals_set = HashSet::with_capacity($ROWS - 1);
for col in &$COLUMNS[1..] {
let col = cast_column_field(col, &$CAST_TYPE)?;
let col = cast_column(col, &$CAST_TYPE)?;
let col_viewer = $T::try_create_viewer(&col)?;
if col_viewer.valid_at(0) {
let val = col_viewer.value_at(0);
Expand Down Expand Up @@ -117,28 +117,23 @@ impl<const NEGATED: bool> Function for InFunction<NEGATED> {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<ColumnRef> {
if self.is_null {
let col = NullType::arc().create_constant_column(&DataValue::Null, input_rows)?;
return Ok(col);
}

let types: Vec<DataTypeImpl> = columns.iter().map(|col| col.column().data_type()).collect();
let types: Vec<DataTypeImpl> = columns.iter().map(|col| col.data_type()).collect();

let least_super_dt = if columns[0]
.field()
.data_type()
.data_type_id()
.is_date_or_date_time()
{
let least_super_dt = if columns[0].data_type().data_type_id().is_date_or_date_time() {
match columns[1..]
.iter()
.map(|column| column.field().data_type().data_type_id())
.map(|column| column.data_type().data_type_id())
.all(|t| t.is_string() || t.is_date_or_date_time())
{
true => columns[0].field().data_type().clone(),
true => columns[0].data_type(),
false => {
return Result::Err(ErrorCode::BadDataValueType("test"));
}
Expand All @@ -148,7 +143,7 @@ impl<const NEGATED: bool> Function for InFunction<NEGATED> {
};

let least_super_type_id = remove_nullable(&least_super_dt).data_type_id();
let input_col = cast_column_field(&columns[0], &least_super_dt)?;
let input_col = cast_column(&columns[0], &least_super_dt)?;

match least_super_type_id {
TypeID::Boolean => {
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/scalars/conditionals/is_not_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl Function for IsNotNullFunction {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &common_datavalues::ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<common_datavalues::ColumnRef> {
let (all_null, validity) = columns[0].column().validity();
let (all_null, validity) = columns[0].validity();
if all_null {
return Ok(ConstColumn::new(Series::from_data(vec![false]), input_rows).arc());
}
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/scalars/conditionals/is_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl Function for IsNullFunction {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &common_datavalues::ColumnsWithField,
columns: &[ColumnRef],
input_rows: usize,
) -> Result<common_datavalues::ColumnRef> {
let (all_null, validity) = columns[0].column().validity();
let (all_null, validity) = columns[0].validity();
if all_null {
return Ok(ConstColumn::new(Series::from_data(vec![true]), input_rows).arc());
}
Expand Down
7 changes: 4 additions & 3 deletions common/functions/src/scalars/contexts/current_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::fmt;

use common_datavalues::ColumnRef;
use common_datavalues::DataTypeImpl;
use common_datavalues::StringType;
use common_exception::Result;
Expand Down Expand Up @@ -52,10 +53,10 @@ impl Function for CurrentUserFunction {
fn eval(
&self,
_func_ctx: FunctionContext,
columns: &common_datavalues::ColumnsWithField,
columns: &[ColumnRef],
_input_rows: usize,
) -> Result<common_datavalues::ColumnRef> {
Ok(columns[0].column().clone())
) -> Result<ColumnRef> {
Ok(columns[0].clone())
}
}

Expand Down
Loading