Skip to content
Snippets Groups Projects
Commit b16aa19c authored by EmirVildanov's avatar EmirVildanov
Browse files

fix: change values cast error message

parent b7df38df
No related branches found
No related tags found
1 merge request!480Support SubQueries interpretation as expressions
...@@ -192,7 +192,7 @@ update_queries.test_invalid = function() ...@@ -192,7 +192,7 @@ update_queries.test_invalid = function()
_, err = api:call("sbroad.execute", { _, err = api:call("sbroad.execute", {
[[ update "testing_space" set "product_units" = 'hello']], {} [[ update "testing_space" set "product_units" = 'hello']], {}
}) })
t.assert_str_contains(tostring(err), "(FailedTo(Serialize, Some(Value)") t.assert_str_contains(tostring(err), "Failed to cast 'hello' to integer.")
-- table name can't specified on the left side of update -- table name can't specified on the left side of update
_, err = api:call("sbroad.execute", { _, err = api:call("sbroad.execute", {
......
...@@ -15,7 +15,7 @@ use tarantool::tuple::{FieldType, KeyDefPart}; ...@@ -15,7 +15,7 @@ use tarantool::tuple::{FieldType, KeyDefPart};
use tarantool::uuid::Uuid; use tarantool::uuid::Uuid;
use crate::error; use crate::error;
use crate::errors::{Action, Entity, SbroadError}; use crate::errors::{Entity, SbroadError};
use crate::executor::hash::ToHashString; use crate::executor::hash::ToHashString;
use crate::ir::relation::Type; use crate::ir::relation::Type;
use crate::ir::value::double::Double; use crate::ir::value::double::Double;
...@@ -782,62 +782,37 @@ impl Value { ...@@ -782,62 +782,37 @@ impl Value {
/// - the value cannot be cast to the given type. /// - the value cannot be cast to the given type.
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
pub fn cast(&self, column_type: &Type) -> Result<EncodedValue, SbroadError> { pub fn cast(&self, column_type: &Type) -> Result<EncodedValue, SbroadError> {
let cast_error = SbroadError::Invalid(
Entity::Value,
Some(format_smolstr!("Failed to cast {self} to {column_type}.")),
);
match column_type { match column_type {
Type::Any => Ok(self.into()), Type::Any => Ok(self.into()),
Type::Map => match self { Type::Array | Type::Map => match self {
Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into map"),
)),
},
Type::Array => match self {
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into array"),
)),
}, },
Type::Boolean => match self { Type::Boolean => match self {
Value::Boolean(_) => Ok(self.into()), Value::Boolean(_) => Ok(self.into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into boolean"),
)),
}, },
Type::Datetime => match self { Type::Datetime => match self {
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
Value::Datetime(_) => Ok(self.into()), Value::Datetime(_) => Ok(self.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into datetime"),
)),
}, },
Type::Decimal => match self { Type::Decimal => match self {
Value::Decimal(_) => Ok(self.into()), Value::Decimal(_) => Ok(self.into()),
Value::Double(v) => Ok(Value::Decimal( Value::Double(v) => Ok(Value::Decimal(
Decimal::from_str(&format!("{v}")).map_err(|e| { Decimal::from_str(&format!("{v}")).map_err(|_| cast_error)?,
SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{e:?}"),
)
})?,
) )
.into()), .into()),
Value::Integer(v) => Ok(Value::Decimal(Decimal::from(*v)).into()), Value::Integer(v) => Ok(Value::Decimal(Decimal::from(*v)).into()),
Value::Unsigned(v) => Ok(Value::Decimal(Decimal::from(*v)).into()), Value::Unsigned(v) => Ok(Value::Decimal(Decimal::from(*v)).into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into decimal"),
)),
}, },
Type::Double => match self { Type::Double => match self {
Value::Double(_) => Ok(self.into()), Value::Double(_) => Ok(self.into()),
...@@ -845,130 +820,61 @@ impl Value { ...@@ -845,130 +820,61 @@ impl Value {
Value::Integer(v) => Ok(Value::Double(Double::from(*v)).into()), Value::Integer(v) => Ok(Value::Double(Double::from(*v)).into()),
Value::Unsigned(v) => Ok(Value::Double(Double::from(*v)).into()), Value::Unsigned(v) => Ok(Value::Double(Double::from(*v)).into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into double"),
)),
}, },
Type::Integer => match self { Type::Integer => match self {
Value::Integer(_) => Ok(self.into()), Value::Integer(_) => Ok(self.into()),
Value::Decimal(v) => Ok(Value::Integer(v.to_i64().ok_or_else(|| { Value::Decimal(v) => Ok(Value::Integer(v.to_i64().ok_or(cast_error)?).into()),
SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into integer"),
)
})?)
.into()),
Value::Double(v) => v Value::Double(v) => v
.to_string() .to_string()
.parse::<i64>() .parse::<i64>()
.map(Value::Integer) .map(Value::Integer)
.map(EncodedValue::from) .map(EncodedValue::from)
.map_err(|e| { .map_err(|_| cast_error),
SbroadError::FailedTo( Value::Unsigned(v) => {
Action::Serialize, Ok(Value::Integer(i64::try_from(*v).map_err(|_| cast_error)?).into())
Some(Entity::Value), }
e.to_smolstr(),
)
}),
Value::Unsigned(v) => Ok(Value::Integer(i64::try_from(*v).map_err(|e| {
SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("u64 {v} into i64: {e}"),
)
})?)
.into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into integer"),
)),
}, },
Type::Scalar => match self { Type::Scalar => match self {
Value::Tuple(_) => Err(SbroadError::FailedTo( Value::Tuple(_) => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into scalar"),
)),
_ => Ok(self.into()), _ => Ok(self.into()),
}, },
Type::String => match self { Type::String => match self {
Value::String(_) => Ok(self.into()), Value::String(_) => Ok(self.into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into string"),
)),
}, },
Type::Uuid => match self { Type::Uuid => match self {
Value::Uuid(_) => Ok(self.into()), Value::Uuid(_) => Ok(self.into()),
Value::String(v) => Ok(Value::Uuid(Uuid::parse_str(v).map_err(|e| { Value::String(v) => {
SbroadError::FailedTo( Ok(Value::Uuid(Uuid::parse_str(v).map_err(|_| cast_error)?).into())
Action::Serialize, }
Some(Entity::Value),
format_smolstr!("uuid {v} into string: {e}"),
)
})?)
.into()),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into uuid"),
)),
}, },
Type::Number => match self { Type::Number => match self {
Value::Integer(_) | Value::Decimal(_) | Value::Double(_) | Value::Unsigned(_) => { Value::Integer(_) | Value::Decimal(_) | Value::Double(_) | Value::Unsigned(_) => {
Ok(self.into()) Ok(self.into())
} }
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into number"),
)),
}, },
Type::Unsigned => match self { Type::Unsigned => match self {
Value::Unsigned(_) => Ok(self.into()), Value::Unsigned(_) => Ok(self.into()),
Value::Integer(v) => Ok(Value::Unsigned(u64::try_from(*v).map_err(|e| { Value::Integer(v) => {
SbroadError::FailedTo( Ok(Value::Unsigned(u64::try_from(*v).map_err(|_| cast_error)?).into())
Action::Serialize, }
Some(Entity::Value), Value::Decimal(v) => Ok(Value::Unsigned(v.to_u64().ok_or(cast_error)?).into()),
format_smolstr!("i64 {v} into u64: {e}"),
)
})?)
.into()),
Value::Decimal(v) => Ok(Value::Unsigned(v.to_u64().ok_or_else(|| {
SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into unsigned"),
)
})?)
.into()),
Value::Double(v) => v Value::Double(v) => v
.to_string() .to_string()
.parse::<u64>() .parse::<u64>()
.map(Value::Unsigned) .map(Value::Unsigned)
.map(EncodedValue::from) .map(EncodedValue::from)
.map_err(|_| { .map_err(|_| cast_error),
SbroadError::FailedTo(
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into unsigned"),
)
}),
Value::Null => Ok(Value::Null.into()), Value::Null => Ok(Value::Null.into()),
_ => Err(SbroadError::FailedTo( _ => Err(cast_error),
Action::Serialize,
Some(Entity::Value),
format_smolstr!("{self:?} into unsigned"),
)),
}, },
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment