Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL] Add trigonometric functions sin() and cos() #1118

Merged
merged 3 commits into from
Dec 8, 2023
Merged

[SQL] Add trigonometric functions sin() and cos() #1118

merged 3 commits into from
Dec 8, 2023

Conversation

abhizer
Copy link
Contributor

@abhizer abhizer commented Dec 7, 2023

  • Also changes the precision to which floats display

Fixes: #1094

Is this a user-visible change (yes/no): yes

* Also changes the precision to which floats display

Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
@abhizer abhizer added enhancement New feature or request SQL compiler Related to the SQL compiler User-facing For PRs that lead to Feldera-user visible changes labels Dec 7, 2023
Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
@abhizer abhizer added this to the December 19, 2024 milestone Dec 7, 2023
Copy link
Collaborator

@mihaibudiu mihaibudiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use 15 digits of precision for doubles and 6 for floats when converting to strings.
The formatter will also do some rounding itself, I hope the results will come out right.

@@ -84,4 +84,12 @@ REAL '1.23' -- string style
<td><code>LOG10(value)</code></td>
<td>The logarithm base 10 of value. Produces a runtime error for values less than or equal to zero.</td>
</tr>
<tr>
<td><code>SIN(value)</code></td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to document the fact that the only type supported is double, and everything else is cast to double, and also that the result is always double. We will have to revisit the other functions, like ln too. Maybe you can file an issue about that.

@@ -361,7 +364,9 @@ macro_rules! float {
impl Display for $outer {
#[rustfmt::skip]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0.0, f)
let num = format!("{1:.0$}", FLOAT_DISPLAY_PRECISION, &self.0.0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this in the cast to string method, which is in sqllib/src/casts.rs

case "cos":
{
// Turn the argument into Double
ops.set(0, ops.get(0).cast(new DBSPTypeDouble(node, false)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The false here is not correct, it indicates that the value cannot be null.
You should instead get the current type of the operand and copy its mayBeNull value.
This also proves that there is no test for NULL as an argument, so add that too.

@Test
public void testSin() {
this.qs(
"SELECT sin(0);\n" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these values are decimal values.
You probably should write the same test with double values as arguments.
And replace 3.14 with pi when we have implemented that.
Maybe you can push another commit in this PR for PI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also document where the tests are coming from. For example "tested using Postgres".

@@ -889,6 +889,24 @@ some_polymorphic_function1!(sign, f, F32, F32);
some_polymorphic_function1!(sign, d, F64, F64);
some_polymorphic_function1!(sign, decimal, Decimal, Decimal);

///////////////////// sin //////////////////////
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this many comments are overkill.
Just add one comment for all trigonometric functions

* Accept null argument in sin and cos
* Limit max precision to 6 and 15 decimal places for float and double
respectively when casting to string
* Update the tests including a test for null argument
* Update the documentation of the functions

Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
@Test
public void testSinDouble() {
this.qs(
"SELECT sin(CAST(0 AS DOUBLE));\n" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works, but you can get a double by writing e0 at the end. 3.14e0 is a double, while 3.14 is a decimal.

@mihaibudiu mihaibudiu merged commit 1691a34 into main Dec 8, 2023
5 checks passed
@mihaibudiu mihaibudiu deleted the sin branch December 8, 2023 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request SQL compiler Related to the SQL compiler User-facing For PRs that lead to Feldera-user visible changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sin is not supported
2 participants