From 25d45970e6c545f4963cf3895399ec34dae86802 Mon Sep 17 00:00:00 2001 From: Xiangjin Date: Fri, 27 Oct 2023 15:34:03 +0800 Subject: [PATCH] fix(expr): include data type in parse error message --- src/expr/impl/src/scalar/cast.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/expr/impl/src/scalar/cast.rs b/src/expr/impl/src/scalar/cast.rs index f22b643bb9a09..ae2ed50472b86 100644 --- a/src/expr/impl/src/scalar/cast.rs +++ b/src/expr/impl/src/scalar/cast.rs @@ -37,14 +37,14 @@ use risingwave_pb::expr::expr_node::PbType; #[function("cast(varchar) -> timestamp")] #[function("cast(varchar) -> interval")] #[function("cast(varchar) -> jsonb")] -pub fn str_parse(elem: &str) -> Result +pub fn str_parse(elem: &str, ctx: &Context) -> Result where T: FromStr, ::Err: std::fmt::Display, { - elem.trim() - .parse() - .map_err(|err: ::Err| ExprError::Parse(err.to_string().into())) + elem.trim().parse().map_err(|err: ::Err| { + ExprError::Parse(format!("{} {}", ctx.return_type, err).into()) + }) } // TODO: introduce `FromBinary` and support all types @@ -521,7 +521,11 @@ mod tests { async fn test_unary() { test_unary_bool::(|x| !x, PbType::Not).await; test_unary_date::(|x| try_cast(x).unwrap(), PbType::Cast).await; - test_str_to_int16::(|x| str_parse(x).unwrap()).await; + let ctx_str_to_int16 = Context { + arg_types: vec![DataType::Varchar], + return_type: DataType::Int16, + }; + test_str_to_int16::(|x| str_parse(x, &ctx_str_to_int16).unwrap()).await; } #[tokio::test]