add back `Bool column` type

main
Ziyang Hu 2 years ago
parent c8ceada869
commit 12d2806fd2

2
Cargo.lock generated

@ -386,7 +386,7 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "cozo"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"approx",
"base64",

@ -179,7 +179,7 @@ literal = _{ null | boolean | number | string}
table_schema = {"{" ~ table_cols ~ ("=>" ~ table_cols)? ~ "}"}
table_cols = {(table_col ~ ",")* ~ table_col?}
table_col = {ident ~ (":" ~ col_type)? ~ (("default" ~ expr) | ("=" ~ out_arg))?}
col_type = {(any_type | int_type | float_type | string_type | bytes_type | uuid_type | list_type | tuple_type) ~ "?"?}
col_type = {(any_type | bool_type | int_type | float_type | string_type | bytes_type | uuid_type | list_type | tuple_type) ~ "?"?}
col_type_with_term = {SOI ~ col_type ~ EOI}
any_type = {"Any"}
int_type = {"Int"}
@ -187,5 +187,6 @@ float_type = {"Float"}
string_type = {"String"}
bytes_type = {"Bytes"}
uuid_type = {"Uuid"}
bool_type = {"Bool"}
list_type = {"[" ~ col_type ~ (";" ~ expr)? ~ "]"}
tuple_type = {"(" ~ (col_type ~ ",")* ~ col_type? ~ ")"}

@ -695,6 +695,7 @@ pub(crate) fn get_op(name: &str) -> Option<&'static Op> {
"intersection" => &OP_INTERSECTION,
"difference" => &OP_DIFFERENCE,
"to_uuid" => &OP_TO_UUID,
"to_bool" => &OP_TO_BOOL,
"rand_uuid_v1" => &OP_RAND_UUID_V1,
"rand_uuid_v4" => &OP_RAND_UUID_V4,
"uuid_timestamp" => &OP_UUID_TIMESTAMP,

@ -183,8 +183,12 @@ pub(crate) fn op_min(args: &[DataValue]) -> Result<DataValue> {
define_op!(OP_SUB, 2, false);
pub(crate) fn op_sub(args: &[DataValue]) -> Result<DataValue> {
Ok(match (&args[0], &args[1]) {
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Int(b))) => DataValue::Num(Num::Int(*a - *b)),
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => DataValue::Num(Num::Float(*a - *b)),
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Int(b))) => {
DataValue::Num(Num::Int(*a - *b))
}
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float(*a - *b))
}
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float((*a as f64) - b))
}
@ -219,7 +223,9 @@ pub(crate) fn op_div(args: &[DataValue]) -> Result<DataValue> {
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Int(b))) => {
DataValue::Num(Num::Float((*a as f64) / (*b as f64)))
}
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => DataValue::Num(Num::Float(*a / *b)),
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float(*a / *b))
}
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float((*a as f64) / b))
}
@ -498,8 +504,12 @@ pub(crate) fn op_pow(args: &[DataValue]) -> Result<DataValue> {
define_op!(OP_MOD, 2, false);
pub(crate) fn op_mod(args: &[DataValue]) -> Result<DataValue> {
Ok(match (&args[0], &args[1]) {
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Int(b))) => DataValue::Num(Num::Int(a.rem(b))),
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => DataValue::Num(Num::Float(a.rem(*b))),
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Int(b))) => {
DataValue::Num(Num::Int(a.rem(b)))
}
(DataValue::Num(Num::Float(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float(a.rem(*b)))
}
(DataValue::Num(Num::Int(a)), DataValue::Num(Num::Float(b))) => {
DataValue::Num(Num::Float((*a as f64).rem(b)))
}
@ -1234,6 +1244,23 @@ pub(crate) fn op_decode_base64(args: &[DataValue]) -> Result<DataValue> {
}
}
define_op!(OP_TO_BOOL, 1, false);
pub(crate) fn op_to_bool(args: &[DataValue]) -> Result<DataValue> {
Ok(DataValue::Bool(match &args[0] {
DataValue::Null => false,
DataValue::Bool(b) => *b,
DataValue::Num(n) => n.get_int() != Some(0),
DataValue::Str(s) => !s.is_empty(),
DataValue::Bytes(b) => !b.is_empty(),
DataValue::Uuid(u) => !u.0.is_nil(),
DataValue::Regex(r) => !r.0.as_str().is_empty(),
DataValue::List(l) => !l.is_empty(),
DataValue::Set(s) => !s.is_empty(),
DataValue::Guard => false,
DataValue::Bot => false,
}))
}
define_op!(OP_TO_FLOAT, 1, false);
pub(crate) fn op_to_float(args: &[DataValue]) -> Result<DataValue> {
Ok(match &args[0] {

@ -22,6 +22,7 @@ impl Display for NullableColType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.coltype {
ColType::Any => f.write_str("Any")?,
ColType::Bool => f.write_str("Bool")?,
ColType::Int => f.write_str("Int")?,
ColType::Float => f.write_str("Float")?,
ColType::String => f.write_str("String")?,
@ -57,6 +58,7 @@ impl Display for NullableColType {
#[derive(Debug, Clone, Eq, PartialEq, serde_derive::Deserialize, serde_derive::Serialize)]
pub(crate) enum ColType {
Any,
Bool,
Int,
Float,
String,
@ -160,6 +162,7 @@ impl NullableColType {
Ok(match &self.coltype {
ColType::Any => data,
ColType::Bool => DataValue::Bool(data.get_bool().ok_or_else(make_err)?),
ColType::Int => DataValue::from(data.get_int().ok_or_else(make_err)?),
ColType::Float => DataValue::from(data.get_float().ok_or_else(make_err)?),
ColType::String => {

@ -112,6 +112,7 @@ pub(crate) fn parse_nullable_type(pair: Pair<'_>) -> Result<NullableColType> {
fn parse_type_inner(pair: Pair<'_>) -> Result<ColType> {
Ok(match pair.as_rule() {
Rule::any_type => ColType::Any,
Rule::bool_type => ColType::Bool,
Rule::int_type => ColType::Int,
Rule::float_type => ColType::Float,
Rule::string_type => ColType::String,

Loading…
Cancel
Save