Skip to content

Commit

Permalink
Merge pull request #195 from Qrlew/implement_sign
Browse files Browse the repository at this point in the history
Implement sign
  • Loading branch information
ngrislain authored Nov 20, 2023
2 parents c322287 + 05fd357 commit 0ad6eae
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.5.2] - 2023-11-19
## Added
- `CEIL`, `ROUND`, `FLOOR`and `TRUNC` funtions [#192](https://github.com/Qrlew/qrlew/issues/192)
- `SIGN` funtion [#194](https://github.com/Qrlew/qrlew/issues/194)
- SD is acceptable as DP compilation

## [0.5.1] - 2023-11-19
Expand Down
28 changes: 28 additions & 0 deletions src/data_type/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,14 @@ pub fn trunc() -> impl Function {
)
}

// Sign function
pub fn sign() -> impl Function {
PartitionnedMonotonic::univariate(
data_type::Float::default(),
|a| if a == 0. {0} else if a < 0. {-1} else {1}
)
}

/*
Aggregation functions
*/
Expand Down Expand Up @@ -3557,4 +3565,24 @@ mod tests {
println!("im({}) = {}", set, im);
assert!(im == DataType::boolean_value(true));
}

#[test]
fn test_sign() {
println!("\nTest sign");
let fun = sign();
println!("type = {}", fun);
println!("domain = {}", fun.domain());
println!("co_domain = {}", fun.co_domain());
println!("data_type = {}", fun.data_type());

let set = DataType::float_interval(-5., 5.);
let im = fun.super_image(&set).unwrap();
println!("im({}) = {}", set, im);
assert!(im == DataType::integer_interval(-1, 1));

let set = DataType::float_value(0.);
let im = fun.super_image(&set).unwrap();
println!("im({}) = {}", set, im);
assert!(im == DataType::integer_value(0));
}
}
6 changes: 5 additions & 1 deletion src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum Function {
BitwiseXor,
InList,
Coalesce,
Sign,
// Functions
Exp,
Ln,
Expand Down Expand Up @@ -132,6 +133,7 @@ impl Function {
| Function::Floor
| Function::CastAsDate
| Function::CastAsTime
| Function::Sign
// Binary Functions
| Function::Pow
| Function::Position
Expand Down Expand Up @@ -198,7 +200,8 @@ impl Function {
| Function::CastAsDate
| Function::CastAsTime
| Function::Ceil
| Function::Floor => Arity::Unary,
| Function::Floor
| Function::Sign => Arity::Unary,
// Binary Function
Function::Pow
| Function::Position
Expand Down Expand Up @@ -289,6 +292,7 @@ impl fmt::Display for Function {
Function::Floor => "floor",
Function::CastAsDate => "cast_as_date",
Function::CastAsTime => "cast_as_time",
Function::Sign => "sign",
// Binary Functions
Function::Pow => "pow",
Function::Position => "position",
Expand Down
4 changes: 2 additions & 2 deletions src/expr/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ macro_rules! function_implementations {
}

// All functions:
// Unary: Opposite, Not, Exp, Ln, Abs, Sin, Cos, CharLength, Lower, Upper, Md5, Ceil, Floor
// Unary: Opposite, Not, Exp, Ln, Abs, Sin, Cos, CharLength, Lower, Upper, Md5, Ceil, Floor, Sign
// Binary: Plus, Minus, Multiply, Divide, Modulo, StringConcat, Gt, Lt, GtEq, LtEq, Eq, NotEq, And, Or, Xor, BitwiseOr, BitwiseAnd, BitwiseXor, Position, Concat, Greatest, Least, Round, Trunc
// Ternary: Case, Position
// Nary: Concat
function_implementations!(
[Opposite, Not, Exp, Ln, Log, Abs, Sin, Cos, Sqrt, Md5, Ceil, Floor],
[Opposite, Not, Exp, Ln, Log, Abs, Sin, Cos, Sqrt, Md5, Ceil, Floor, Sign],
[
Plus,
Minus,
Expand Down
51 changes: 50 additions & 1 deletion src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ impl_unary_function_constructors!(
CastAsDate,
CastAsTime,
Ceil,
Floor
Floor,
Sign
); // TODO Complete that

/// Implement binary function constructors
Expand Down Expand Up @@ -2998,4 +2999,52 @@ mod tests {
DataType::boolean_value(false)
);
}

#[test]
fn test_sign() {
println!("sign");
let expression = Expr::sign(
Expr::col("col1".to_string())
);
println!("expression = {}", expression);
println!("expression domain = {}", expression.domain());
println!("expression co domain = {}", expression.co_domain());
println!("expression data type = {}", expression.data_type());

let set = DataType::structured([
("col1", DataType::float_interval(-10., 1.)),
]);
println!(
"expression super image = {}",
expression.super_image(&set).unwrap()
);
assert_eq!(
expression.super_image(&set).unwrap(),
DataType::integer_interval(-1, 1)
);

let set = DataType::structured([
("col1", DataType::integer_min(-0)),
]);
println!(
"expression super image = {}",
expression.super_image(&set).unwrap()
);
assert_eq!(
expression.super_image(&set).unwrap(),
DataType::integer_interval(0, 1)
);

let set = DataType::structured([
("col1", DataType::float_min(1.)),
]);
println!(
"expression super image = {}",
expression.super_image(&set).unwrap()
);
assert_eq!(
expression.super_image(&set).unwrap(),
DataType::integer_value(1)
);
}
}
14 changes: 13 additions & 1 deletion src/expr/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ impl<'a> expr::Visitor<'a, ast::Expr> for FromExprVisitor {
| expr::function::Function::Substr
| expr::function::Function::SubstrWithSize
| expr::function::Function::Ceil
| expr::function::Function::Floor => ast::Expr::Function(ast::Function {
| expr::function::Function::Floor
| expr::function::Function::Sign => ast::Expr::Function(ast::Function {
name: ast::ObjectName(vec![ast::Ident::new(function.to_string())]),
args: arguments
.into_iter()
Expand Down Expand Up @@ -651,4 +652,15 @@ mod tests {
println!("ast::expr = {gen_expr}");
assert_eq!(ast_expr.to_string().to_lowercase(), gen_expr.to_string().to_lowercase());
}

#[test]
fn test_sign() {
let str_expr = "sign(a)";
let ast_expr: ast::Expr = parse_expr(str_expr).unwrap();
let expr = Expr::try_from(&ast_expr).unwrap();
println!("expr = {}", expr);
let gen_expr = ast::Expr::from(&expr);
println!("ast::expr = {gen_expr}");
assert_eq!(ast_expr, gen_expr);
}
}
1 change: 1 addition & 0 deletions src/sql/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ impl<'a> Visitor<'a, Result<Expr>> for TryIntoExprVisitor<'a> {
precision,
)
}
"sign" => Expr::sign(flat_args[0].clone()),
// Aggregates
"min" => Expr::min(flat_args[0].clone()),
"max" => Expr::max(flat_args[0].clone()),
Expand Down

0 comments on commit 0ad6eae

Please sign in to comment.