new `to_int` conversion function

main
Ziyang Hu 2 years ago
parent 936eee48c1
commit 8d7bb9e454

@ -698,6 +698,7 @@ pub(crate) fn get_op(name: &str) -> Option<&'static Op> {
"chunks" => &OP_CHUNKS,
"chunks_exact" => &OP_CHUNKS_EXACT,
"windows" => &OP_WINDOWS,
"to_int" => &OP_TO_INT,
"to_float" => &OP_TO_FLOAT,
"to_string" => &OP_TO_STRING,
"rand_float" => &OP_RAND_FLOAT,

@ -1294,6 +1294,28 @@ pub(crate) fn op_to_unity(args: &[DataValue]) -> Result<DataValue> {
}))
}
define_op!(OP_TO_INT, 1, false);
pub(crate) fn op_to_int(args: &[DataValue]) -> Result<DataValue> {
Ok(match &args[0] {
DataValue::Num(n) => match n.get_int() {
None => {
let f = n.get_float();
DataValue::Num(Num::Int(f as i64))
}
Some(i) => DataValue::Num(Num::Int(i)),
},
DataValue::Null => DataValue::from(0),
DataValue::Bool(b) => DataValue::from(if *b { 1 } else { 0 }),
DataValue::Str(t) => match t as &str {
s => i64::from_str(s)
.map_err(|_| miette!("The string cannot be interpreted as int"))?
.into(),
},
DataValue::Validity(vld) => DataValue::Num(Num::Int(vld.timestamp.0 .0)),
v => bail!("'to_int' does not recognize {:?}", v),
})
}
define_op!(OP_TO_FLOAT, 1, false);
pub(crate) fn op_to_float(args: &[DataValue]) -> Result<DataValue> {
Ok(match &args[0] {

Loading…
Cancel
Save