From be0f0532d67dee2957db5ae3e6a6e2b07c27f320 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Tue, 16 Jan 2024 19:59:21 +0200 Subject: [PATCH] feat: Make `SING` function accept Int or Float type --- crates/gitql-ast/src/function.rs | 10 ++++++++-- docs/function/functions.md | 31 ++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/crates/gitql-ast/src/function.rs b/crates/gitql-ast/src/function.rs index 1d275e3f..cf064522 100644 --- a/crates/gitql-ast/src/function.rs +++ b/crates/gitql-ast/src/function.rs @@ -413,7 +413,7 @@ lazy_static! { map.insert( "sign", Prototype { - parameters: vec![DataType::Float], + parameters: vec![DataType::Variant(vec![DataType::Integer, DataType::Float])], result: DataType::Integer, }, ); @@ -821,7 +821,13 @@ fn numeric_atn2(inputs: &[Value]) -> Value { } fn numeric_sign(inputs: &[Value]) -> Value { - let float_value = inputs[0].as_float(); + let value = &inputs[0]; + if value.data_type().is_int() { + let int_value = value.as_int(); + return Value::Integer(int_value.signum()); + } + + let float_value = value.as_float(); if float_value == 0.0 { Value::Integer(0) } else if float_value > 0.0 { diff --git a/docs/function/functions.md b/docs/function/functions.md index 5c3bc24f..93658f56 100644 --- a/docs/function/functions.md +++ b/docs/function/functions.md @@ -87,21 +87,21 @@ SELECT HOUR(NOW()) ### Numeric Functions -| Name | Parameters | Return | Description | -| ------ | ------------ | ------- | ---------------------------------------------------------------------------- | -| PI | | Float | Return the value of PI. | -| FLOOR | Float | Integer | Returns the largest integer value that is smaller than or equal to a number. | -| ROUND | Float | Integer | Returns the nearest integer value. | -| SQUARE | Integer | Integer | Returns the square of an integer value. | -| ABS | Integer | Integer | Returns the absolute value of an integer value. | -| SIN | Float | Float | Returns the sine of a number. | -| ASIN | Float | Float | Returns the arc sine of a number. | -| COS | FLOAT | FLOAT | Returns the cosine of a number. | -| ACOS | FLOAT | FLOAT | Returns the arc cosine of a number. | -| TAN | FLOAT | FLOAT | Returns the tangent of a number. | -| ATAN | FLOAT | FLOAT | Returns the arc tangent of a number. | -| ATN2 | FLOAT, FLOAT | FLOAT | Returns the arc tangent of two values. | -| SIGN | FLOAT | Integer | Returns the sign of a number. | +| Name | Parameters | Return | Description | +| ------ | ---------------- | ------- | ---------------------------------------------------------------------------- | +| PI | | Float | Return the value of PI. | +| FLOOR | Float | Integer | Returns the largest integer value that is smaller than or equal to a number. | +| ROUND | Float | Integer | Returns the nearest integer value. | +| SQUARE | Integer | Integer | Returns the square of an integer value. | +| ABS | Integer | Integer | Returns the absolute value of an integer value. | +| SIN | Float | Float | Returns the sine of a number. | +| ASIN | Float | Float | Returns the arc sine of a number. | +| COS | FLOAT | FLOAT | Returns the cosine of a number. | +| ACOS | FLOAT | FLOAT | Returns the arc cosine of a number. | +| TAN | FLOAT | FLOAT | Returns the tangent of a number. | +| ATAN | FLOAT | FLOAT | Returns the arc tangent of a number. | +| ATN2 | FLOAT, FLOAT | FLOAT | Returns the arc tangent of two values. | +| SIGN | Integer or FLOAT | Integer | Returns the sign of a number. | ### Numeric functions samples @@ -112,6 +112,7 @@ SELECT ROUND(1.5) SELECT SQUARE(64) SELECT ABS(-1) SELECT SIN(2.0) +SELECT SIN(2) SELECT ATN2(0.50, 1.0) ```