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

fix: when an expression such as if returns a result, return the resul… #145

Merged
merged 2 commits into from
Feb 19, 2024
Merged
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
25 changes: 17 additions & 8 deletions src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ impl<'a, T: Transaction> Binder<'a, T> {
results,
else_result,
} => {
let fn_check_ty = |ty: &mut LogicalType, result_ty| {
if result_ty != LogicalType::SqlNull {
if ty == &LogicalType::SqlNull {
*ty = result_ty;
} else if ty != &result_ty {
return Err(DatabaseError::Incomparable(*ty, result_ty));
}
}

Ok(())
};
let mut operand_expr = None;
let mut ty = LogicalType::SqlNull;
if let Some(expr) = operand {
Expand All @@ -138,19 +149,17 @@ impl<'a, T: Transaction> Binder<'a, T> {
let result = self.bind_expr(&results[i])?;
let result_ty = result.return_type();

if result_ty != LogicalType::SqlNull {
if ty == LogicalType::SqlNull {
ty = result_ty;
} else if ty != result_ty {
return Err(DatabaseError::Incomparable(ty, result_ty));
}
}
fn_check_ty(&mut ty, result_ty)?;
expr_pairs.push((self.bind_expr(&conditions[i])?, result))
}

let mut else_expr = None;
if let Some(expr) = else_result {
else_expr = Some(Box::new(self.bind_expr(expr)?));
let temp_expr = Box::new(self.bind_expr(expr)?);
let else_ty = temp_expr.return_type();

fn_check_ty(&mut ty, else_ty)?;
else_expr = Some(temp_expr);
}

Ok(ScalarExpression::CaseWhen {
Expand Down
37 changes: 22 additions & 15 deletions src/expression/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ macro_rules! eval_to_num {

impl ScalarExpression {
pub fn eval(&self, tuple: &Tuple) -> Result<ValueRef, DatabaseError> {
let check_cast = |value: ValueRef, return_type: &LogicalType| {
if value.logical_type() != *return_type {
return Ok(Arc::new(DataValue::clone(&value).cast(return_type)?));
}
Ok(value)
};

match self {
ScalarExpression::Constant(val) => Ok(val.clone()),
ScalarExpression::ColumnRef(col) => {
Expand Down Expand Up @@ -186,39 +193,39 @@ impl ScalarExpression {
condition,
left_expr,
right_expr,
..
ty,
} => {
if condition.eval(tuple)?.is_true()? {
left_expr.eval(tuple)
check_cast(left_expr.eval(tuple)?, ty)
} else {
right_expr.eval(tuple)
check_cast(right_expr.eval(tuple)?, ty)
}
}
ScalarExpression::IfNull {
left_expr,
right_expr,
..
ty,
} => {
let value = left_expr.eval(tuple)?;
let mut value = left_expr.eval(tuple)?;

if value.is_null() {
return right_expr.eval(tuple);
value = right_expr.eval(tuple)?;
}
Ok(value)
check_cast(value, ty)
}
ScalarExpression::NullIf {
left_expr,
right_expr,
..
ty,
} => {
let value = left_expr.eval(tuple)?;
let mut value = left_expr.eval(tuple)?;

if right_expr.eval(tuple)? == value {
return Ok(NULL_VALUE.clone());
value = NULL_VALUE.clone();
}
Ok(value)
check_cast(value, ty)
}
ScalarExpression::Coalesce { exprs, .. } => {
ScalarExpression::Coalesce { exprs, ty } => {
let mut value = None;

for expr in exprs {
Expand All @@ -229,13 +236,13 @@ impl ScalarExpression {
break;
}
}
Ok(value.unwrap_or_else(|| NULL_VALUE.clone()))
check_cast(value.unwrap_or_else(|| NULL_VALUE.clone()), ty)
}
ScalarExpression::CaseWhen {
operand_expr,
expr_pairs,
else_expr,
..
ty,
} => {
let mut operand_value = None;
let mut result = None;
Expand All @@ -262,7 +269,7 @@ impl ScalarExpression {
result = Some(expr.eval(tuple)?);
}
}
Ok(result.unwrap_or_else(|| NULL_VALUE.clone()))
check_cast(result.unwrap_or_else(|| NULL_VALUE.clone()), ty)
}
}
}
Expand Down
Loading