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

refactor(expr): redo cast #8641

Merged
merged 4 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions src/query/codegen/src/writes/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ pub fn codegen_register() {
.map(|n| n + 1)
.map(|n| format!("value{n}"))
.join(",");
let n_widecards = "_,".repeat(n_args);
let any_arg_has_null = (0..n_args)
.map(|n| n + 1)
.map(|n| format!("arg{n}.has_null"))
Expand Down Expand Up @@ -262,7 +261,7 @@ pub fn codegen_register() {
self.register_{n_args}_arg_core::<{arg_generics} NullableType<O>, _, _>(
name,
property.clone(),
|{n_widecards}| None,
calc_domain,
func
);

Expand Down
1,414 changes: 620 additions & 794 deletions src/query/expression/src/evaluator.rs

Large diffs are not rendered by default.

98 changes: 62 additions & 36 deletions src/query/expression/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ pub enum RawExpr {
},
Cast {
span: Span,
expr: Box<RawExpr>,
dest_type: DataType,
},
TryCast {
span: Span,
is_try: bool,
expr: Box<RawExpr>,
dest_type: DataType,
},
Expand All @@ -63,18 +59,16 @@ pub enum Expr {
Constant {
span: Span,
scalar: Scalar,
data_type: DataType,
},
ColumnRef {
span: Span,
id: usize,
data_type: DataType,
},
Cast {
span: Span,
expr: Box<Expr>,
dest_type: DataType,
},
TryCast {
span: Span,
is_try: bool,
expr: Box<Expr>,
dest_type: DataType,
},
Expand All @@ -85,6 +79,7 @@ pub enum Expr {
function: Arc<Function>,
generics: Vec<DataType>,
args: Vec<Expr>,
return_type: DataType,
},
}

Expand All @@ -97,18 +92,16 @@ pub enum RemoteExpr {
Constant {
span: Span,
scalar: Scalar,
data_type: DataType,
},
ColumnRef {
span: Span,
id: usize,
data_type: DataType,
},
Cast {
span: Span,
expr: Box<RemoteExpr>,
dest_type: DataType,
},
TryCast {
span: Span,
is_try: bool,
expr: Box<RemoteExpr>,
dest_type: DataType,
},
Expand All @@ -117,6 +110,7 @@ pub enum RemoteExpr {
id: FunctionID,
generics: Vec<DataType>,
args: Vec<RemoteExpr>,
return_type: DataType,
},
}

Expand Down Expand Up @@ -145,7 +139,6 @@ impl RawExpr {
buf.insert(*id);
}
RawExpr::Cast { expr, .. } => walk(expr, buf),
RawExpr::TryCast { expr, .. } => walk(expr, buf),
RawExpr::FunctionCall { args, .. } => args.iter().for_each(|expr| walk(expr, buf)),
RawExpr::Literal { .. } => (),
}
Expand All @@ -157,26 +150,46 @@ impl RawExpr {
}
}

impl Expr {
pub fn data_type(&self) -> &DataType {
match self {
Expr::Constant { data_type, .. } => data_type,
Expr::ColumnRef { data_type, .. } => data_type,
Expr::Cast { dest_type, .. } => dest_type,
Expr::FunctionCall { return_type, .. } => return_type,
}
}
}

impl RemoteExpr {
pub fn from_expr(expr: Expr) -> Self {
match expr {
Expr::Constant { span, scalar } => RemoteExpr::Constant { span, scalar },
Expr::ColumnRef { span, id } => RemoteExpr::ColumnRef { span, id },
Expr::Cast {
Expr::Constant {
span,
expr,
dest_type,
} => RemoteExpr::Cast {
scalar,
data_type,
} => RemoteExpr::Constant {
span,
expr: Box::new(RemoteExpr::from_expr(*expr)),
dest_type,
scalar,
data_type,
},
Expr::ColumnRef {
span,
id,
data_type,
} => RemoteExpr::ColumnRef {
span,
id,
data_type,
},
Expr::TryCast {
Expr::Cast {
span,
is_try,
expr,
dest_type,
} => RemoteExpr::TryCast {
} => RemoteExpr::Cast {
span,
is_try,
expr: Box::new(RemoteExpr::from_expr(*expr)),
dest_type,
},
Expand All @@ -186,34 +199,45 @@ impl RemoteExpr {
function: _,
generics,
args,
return_type,
} => RemoteExpr::FunctionCall {
span,
id,
generics,
args: args.into_iter().map(RemoteExpr::from_expr).collect(),
return_type,
},
}
}

pub fn into_expr(self, fn_registry: &FunctionRegistry) -> Option<Expr> {
Some(match self {
RemoteExpr::Constant { span, scalar } => Expr::Constant { span, scalar },
RemoteExpr::ColumnRef { span, id } => Expr::ColumnRef { span, id },
RemoteExpr::Cast {
RemoteExpr::Constant {
span,
expr,
dest_type,
} => Expr::Cast {
scalar,
data_type,
} => Expr::Constant {
span,
expr: Box::new(expr.into_expr(fn_registry)?),
dest_type,
scalar,
data_type,
},
RemoteExpr::TryCast {
RemoteExpr::ColumnRef {
span,
id,
data_type,
} => Expr::ColumnRef {
span,
id,
data_type,
},
RemoteExpr::Cast {
span,
is_try,
expr,
dest_type,
} => Expr::TryCast {
} => Expr::Cast {
span,
is_try,
expr: Box::new(expr.into_expr(fn_registry)?),
dest_type,
},
Expand All @@ -222,6 +246,7 @@ impl RemoteExpr {
id,
generics,
args,
return_type,
} => {
let function = fn_registry.get(&id)?;
Expr::FunctionCall {
Expand All @@ -233,6 +258,7 @@ impl RemoteExpr {
.into_iter()
.map(|arg| arg.into_expr(fn_registry))
.collect::<Option<_>>()?,
return_type,
}
}
})
Expand Down
14 changes: 10 additions & 4 deletions src/query/expression/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::utils::arrow::constant_bitmap;
use crate::values::Value;
use crate::values::ValueRef;
use crate::Column;
use crate::Expr;
use crate::Scalar;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -117,7 +118,7 @@ impl FunctionRegistry {
&self,
name: &str,
params: &[usize],
args_type: &[DataType],
args: &[Expr],
) -> Vec<(FunctionID, Arc<Function>)> {
let name = name.to_lowercase();
let name = self
Expand All @@ -135,7 +136,7 @@ impl FunctionRegistry {
.enumerate()
.filter_map(|(id, func)| {
if func.signature.name == name
&& func.signature.args_type.len() == args_type.len()
&& func.signature.args_type.len() == args.len()
{
Some((
FunctionID::Builtin {
Expand All @@ -157,20 +158,25 @@ impl FunctionRegistry {
}
}

let args_type = args
.iter()
.map(Expr::data_type)
.cloned()
.collect::<Vec<_>>();
self.factories
.get(name)
.map(|factories| {
factories
.iter()
.enumerate()
.filter_map(|(id, factory)| {
factory(params, args_type).map(|func| {
factory(params, &args_type).map(|func| {
(
FunctionID::Factory {
name: name.to_string(),
id,
params: params.to_vec(),
args_type: args_type.to_vec(),
args_type: args_type.clone(),
},
func,
)
Expand Down
1 change: 1 addition & 0 deletions src/query/expression/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum Domain {
Timestamp(SimpleDomain<i64>),
Date(SimpleDomain<i32>),
Nullable(NullableDomain<AnyType>),
/// `Array(None)` means that the array is empty, thus there is no inner domain information.
Array(Option<Box<Domain>>),
Tuple(Vec<Domain>),
Undefined,
Expand Down
10 changes: 5 additions & 5 deletions src/query/expression/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl FunctionRegistry {
self.register_1_arg_core::<I1, NullableType<O>, _, _>(
name,
property.clone(),
|_| None,
calc_domain,
func,
);

Expand Down Expand Up @@ -538,7 +538,7 @@ impl FunctionRegistry {
self.register_2_arg_core::<I1, I2, NullableType<O>, _, _>(
name,
property.clone(),
|_, _| None,
calc_domain,
func,
);

Expand Down Expand Up @@ -608,7 +608,7 @@ impl FunctionRegistry {
self.register_3_arg_core::<I1, I2, I3, NullableType<O>, _, _>(
name,
property.clone(),
|_, _, _| None,
calc_domain,
func,
);

Expand Down Expand Up @@ -685,7 +685,7 @@ impl FunctionRegistry {
self.register_4_arg_core::<I1, I2, I3, I4, NullableType<O>, _, _>(
name,
property.clone(),
|_, _, _, _| None,
calc_domain,
func,
);

Expand Down Expand Up @@ -771,7 +771,7 @@ impl FunctionRegistry {
self.register_5_arg_core::<I1, I2, I3, I4, I5, NullableType<O>, _, _>(
name,
property.clone(),
|_, _, _, _, _| None,
calc_domain,
func,
);

Expand Down
Loading