Skip to content

Commit

Permalink
switch to lightweight error
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Nov 21, 2024
1 parent ebaf213 commit 23c2196
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
8 changes: 4 additions & 4 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ parquet = { version = "53.2", features = ["async"] }
mysql_async = { version = "0.34", default-features = false, features = [
"default",
] }
thiserror-ext = "0.1.2"
thiserror-ext = { version = "0.2.1", features = ["backtrace"] }
tikv-jemalloc-ctl = { git = "https://github.com/risingwavelabs/jemallocator.git", rev = "64a2d9" }
tikv-jemallocator = { git = "https://github.com/risingwavelabs/jemallocator.git", features = [
"profiling",
Expand Down
19 changes: 12 additions & 7 deletions src/frontend/src/expr/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

use itertools::Itertools;
use risingwave_common::catalog::Schema;
use risingwave_common::error::{bail, def_anyhow_newtype};
use risingwave_common::types::{DataType, ScalarImpl};
use risingwave_common::util::iter_util::ZipEqFast;
use thiserror_ext::AsReport;
use thiserror::Error;
use thiserror_ext::{AsReport, Box, Macro};

use super::type_inference::cast;
use super::{infer_some_all, infer_type, CastContext, Expr, ExprImpl, Literal};
Expand Down Expand Up @@ -174,7 +174,7 @@ impl FunctionCall {
) -> Result<(), CastError> {
// Can only cast to a struct type.
let DataType::Struct(t) = &target_type else {
bail!(
bail_cast_error!(
"cannot cast type \"{}\" to \"{}\"",
func.return_type(), // typically "record"
target_type,
Expand All @@ -191,8 +191,8 @@ impl FunctionCall {
func.return_type = target_type;
Ok(())
}
std::cmp::Ordering::Less => bail!("input has too few columns"),
std::cmp::Ordering::Greater => bail!("input has too many columns"),
std::cmp::Ordering::Less => bail_cast_error!("input has too few columns"),
std::cmp::Ordering::Greater => bail_cast_error!("input has too many columns"),
}
}

Expand Down Expand Up @@ -423,9 +423,14 @@ pub fn is_row_function(expr: &ExprImpl) -> bool {
false
}

def_anyhow_newtype! {
pub CastError,
#[derive(Error, Debug, Box, Macro)]
#[thiserror_ext(newtype(name = CastError), macro(path = "crate::expr::function_call"))]
#[error("{message}")]
pub struct CastErrorInner {
pub source: Option<CastError>,
pub message: Box<str>,
}

pub type CastResult<T = ()> = Result<T, CastError>;

impl From<CastError> for ErrorCode {
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use enum_as_inner::EnumAsInner;
use fixedbitset::FixedBitSet;
use function_call::bail_cast_error;
use futures::FutureExt;
use paste::paste;
use risingwave_common::array::ListValue;
Expand Down Expand Up @@ -300,7 +301,7 @@ impl ExprImpl {
))),
DataType::Int32 => Ok(self),
dt if dt.is_int() => Ok(self.cast_explicit(DataType::Int32)?),
_ => bail!("unsupported input type"),
_ => bail_cast_error!("unsupported input type"),
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/frontend/src/expr/type_inference/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
use std::collections::BTreeMap;
use std::sync::LazyLock;

use anyhow::Context;
use itertools::Itertools as _;
use parse_display::Display;
use risingwave_common::error::bail;
use risingwave_common::types::{DataType, DataTypeName};
use risingwave_common::util::iter_util::ZipEqFast;

use crate::error::ErrorCode;
use crate::expr::function_call::{CastError, CastResult};
use crate::expr::function_call::{bail_cast_error, cast_error, CastError, CastResult};
use crate::expr::{Expr as _, ExprImpl, InputRef, Literal};

/// Find the least restrictive type. Used by `VALUES`, `CASE`, `UNION`, etc.
Expand Down Expand Up @@ -121,7 +119,7 @@ fn canmeh(ok: bool) -> CastResult {
if ok {
Ok(())
} else {
bail!("")
bail_cast_error!()
}
}
fn cannot() -> CastResult {
Expand All @@ -144,13 +142,15 @@ pub fn cast(source: &DataType, target: &DataType, allows: CastContext) -> Result
} else {
canmeh(cast_ok_base(source, target, allows))
}
.with_context(|| {
format!(
.map_err(|inner| {
cast_error!(
source = inner,
"cannot cast type \"{}\" to \"{}\" in {:?} context",
source, target, allows
source,
target,
allows
)
})
.map_err(Into::into)
}

/// Checks whether casting from `source` to `target` is ok in `allows` context.
Expand All @@ -171,7 +171,7 @@ fn cast_ok_struct(source: &DataType, target: &DataType, allows: CastContext) ->
unreachable!("record type should be already processed at this point");
}
if lty.len() != rty.len() {
bail!("cannot cast structs of different lengths");
bail_cast_error!("cannot cast structs of different lengths");
}
// ... and all fields are castable
lty.types()
Expand Down

0 comments on commit 23c2196

Please sign in to comment.