use {
crate::ast::{Aggregate, Expr, ToSql},
serde::{Serialize, Serializer},
std::fmt::Debug,
thiserror::Error,
};
#[derive(Error, Serialize, Debug, PartialEq, Eq)]
pub enum EvaluateError {
#[error(transparent)]
#[serde(serialize_with = "error_serialize")]
FormatParseError(#[from] chrono::format::ParseError),
#[error("literal add on non-numeric")]
LiteralAddOnNonNumeric,
#[error("function requires string value: {0}")]
FunctionRequiresStringValue(String),
#[error("function requires integer value: {0}")]
FunctionRequiresIntegerValue(String),
#[error("function requires float or integer value: {0}")]
FunctionRequiresFloatOrIntegerValue(String),
#[error("function requires usize value: {0}")]
FunctionRequiresUSizeValue(String),
#[error("function requires float value: {0}")]
FunctionRequiresFloatValue(String),
#[error("extract format does not support value: {0}")]
ExtractFormatNotMatched(String),
#[error("function requires map value: {0}")]
FunctionRequiresMapValue(String),
#[error("function requires point value: {0}")]
FunctionRequiresPointValue(String),
#[error("value not found: {0}")]
ValueNotFound(String),
#[error("only boolean value is accepted: {0}")]
BooleanTypeRequired(String),
#[error("expr requires map or list value")]
MapOrListTypeRequired,
#[error("expr requires list value")]
ListTypeRequired,
#[error("map or string value required for json map conversion: {0}")]
MapOrStringValueRequired(String),
#[error("text literal required for json map conversion: {0}")]
TextLiteralRequired(String),
#[error("unsupported stateless expression: {}", .0.to_sql())]
UnsupportedStatelessExpr(Expr),
#[error("context is required for identifier evaluation: {}", .0.to_sql())]
ContextRequiredForIdentEvaluation(Expr),
#[error("unreachable empty aggregate value: {0:?}")]
UnreachableEmptyAggregateValue(Aggregate),
#[error("the divisor should not be zero")]
DivisorShouldNotBeZero,
#[error("negative substring length not allowed")]
NegativeSubstrLenNotAllowed,
#[error("subquery returns more than one row")]
MoreThanOneRowReturned,
#[error("schemaless projection is not allowed for IN (subquery)")]
SchemalessProjectionForInSubQuery,
#[error("schemaless projection is not allowed for subquery")]
SchemalessProjectionForSubQuery,
#[error("format function does not support following data_type: {0}")]
UnsupportedExprForFormatFunction(String),
#[error("support single character only")]
AsciiFunctionRequiresSingleCharacterValue,
#[error("non-ascii character not allowed")]
NonAsciiCharacterNotAllowed,
#[error("function requires integer value in range")]
ChrFunctionRequiresIntegerValueInRange0To255,
#[error("unsupported evaluate binary arithmetic between {0} and {1}")]
UnsupportedBinaryArithmetic(String, String),
#[error("unsupported evaluate string unary plus: {0}")]
UnsupportedUnaryPlus(String),
#[error("unsupported evaluate string unary minus: {0}")]
UnsupportedUnaryMinus(String),
#[error("unsupported evaluate string unary factorial: {0}")]
UnsupportedUnaryFactorial(String),
#[error("unsupported custom function in subqueries")]
UnsupportedCustomFunction,
#[error("function args.length not matching: {name}, expected: {expected_minimum} ~ {expected_maximum}, found: {found}")]
FunctionArgsLengthNotWithinRange {
name: String,
expected_minimum: usize,
expected_maximum: usize,
found: usize,
},
#[error("unsupported function: {0}")]
UnsupportedFunction(String),
}
fn error_serialize<S>(error: &chrono::format::ParseError, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let display = format!("{}", error);
serializer.serialize_str(&display)
}