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 is_inf, is_nan, log for floats #1344

Merged
merged 2 commits into from
Jan 31, 2024
Merged

[SQL] Add is_inf, is_nan, log for floats #1344

merged 2 commits into from
Jan 31, 2024

Conversation

abhizer
Copy link
Contributor

@abhizer abhizer commented Jan 26, 2024

  • [SQL] Add import float8 tests from Postgres
  • bugfix: Implement ln for double

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

Fixes: #1328
Fixes: #1326
Fixes: #1351
Fixes: #1348
Fixes: #1363

Comment on lines 689 to 718
case "log10":
case "ln":
{
// Cast to Double if the type isn't Decimal
DBSPExpression arg = ops.get(0);
if (!arg.type.is(DBSPTypeDecimal.class)) {
ops.set(0, arg.cast(new DBSPTypeDouble(node, arg.getType().mayBeNull)));
}
return this.compilePolymorphicFunction(call, node, type, ops, 1);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See: #1328

Comment on lines 699 to 726
case "log":
{
// Turn the arguments into Double
for (int i = 0; i < ops.size(); i++) {
DBSPExpression arg = ops.get(i);
ops.set(i, arg.cast(new DBSPTypeDouble(node, arg.getType().mayBeNull)));
}
return this.compilePolymorphicFunction(call, node, type, ops, 1, 2);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We only support log for double as rust_decimal doesn't have a native decimal support for it.

<tr>
<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>IS_INF(value)</code></td>
<td>Returns true if the value is infinite.</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

there are multiple infinities and NaNs in FP, but maybe we don't have to say that.

/*
-- power -> ensure both parameters have the same type
-- truncate
SELECT f.f1, trunc(f.f1) AS trunc_f1 --> not defined for double
Copy link
Collaborator

Choose a reason for hiding this comment

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

trunc should work for double. it it doesn't, please file an issue

NaN | NaN | NaN
(22 rows)

-- ignored some of the tests after the update query
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you have an update query you can probably create a separate table with just the updated versions of the data

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 move these tests to a second file which initializes the table with the updated contents.

(5 rows)
*/
// calcite returns: 1008618.49
@Test @Ignore("fails for calcite optimized version")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure this is a bug really in Calcite. We should investigate. Please file an issue for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add the issue to the ignore comment so we can fix it later

sql-to-dbsp-compiler/lib/sqllib/src/lib.rs Outdated Show resolved Hide resolved
sql-to-dbsp-compiler/lib/sqllib/src/lib.rs Show resolved Hide resolved
@abhizer
Copy link
Contributor Author

abhizer commented Jan 30, 2024

Fixes: #1351
Fixes: #1348

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.

This looks very good.

</tr>
<tr>
<td><code>LOG(value, [, value2])</code></td>
<td>The logarithm of value to base value2, or base e if value2 is not present. Returns `-inf` for value 0. Produces a runtime error for negative numbers.</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

for value2=0.
negative values for either value or value2
Let's call "value2" "base" instead

</tr>
<tr>
<td><code>IS_NAN(value)</code></td>
<td>Returns true if the value is NaN. Note that two NaN values aren't equal.</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

they may be equal.
But there are multiple kinds of NaN, which are not equal to each other.

</tr>
<tr>
<td><code>IS_INF(value)</code></td>
<td>Returns true if the value is infinite. Note that two infinite values aren't necessarily equal.</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

there is + and - infinity.
I think there is one of each, though, and equality works for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh.
I thought like in maths, some infinities can be larger than others. But that doesn't seem to be true here.
I'll update it.

pub fn sqrt_decimal(left: Decimal) -> F64 {
F64::from(left.sqrt().unwrap().to_f64().unwrap())
}

some_polymorphic_function1!(sqrt, decimal, Decimal, F64);

pub fn sqrt_d(left: F64) -> F64 {
F64::from(left.into_inner().sqrt())
let left = left.into_inner();
if left < 0.0 || left.is_nan() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

usually is_nan produces is_nan as a result. Is this check necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it doesn't seem necessary. Removed.

* Tests manually adapted from
* https://github.com/postgres/postgres/blob/master/src/test/regress/expected/float8.out
*/
@SuppressWarnings("JavadocLinkAsPlainText")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you make the comment /* instead of /** you can remove this warning.

}

/*
-- check edge cases for exp
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is wrong with these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't support those functions yet.

NaN | NaN | NaN
(22 rows)

-- ignored some of the tests after the update query
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 move these tests to a second file which initializes the table with the updated contents.

(5 rows)
*/
// calcite returns: 1008618.49
@Test @Ignore("fails for calcite optimized version")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add the issue to the ignore comment so we can fix it later

}

// Calcite optimized version returns 0
@Test @Ignore("https://github.com/feldera/feldera/issues/1348")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we fix this test?
I think we agreed that log in base 0 does return 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is fixed.

@abhizer
Copy link
Contributor Author

abhizer commented Jan 31, 2024

Fixes: #1363

* [SQL] Add import float8 tests from Postgres
* [SQL] bugfix: Implement ln for double
* [SQL] add more methods for polymorphic function power()
* [SQL] Define is_nan, is_inf for both real & double
* [SQL] Implement truncate, round, sqrt for double

Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
}

/*
-- check edge cases for exp --> we don't support exp yet
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have added EXP to the list of functions to implement #1265

@mihaibudiu mihaibudiu merged commit ac51bb4 into main Jan 31, 2024
5 checks passed
@mihaibudiu mihaibudiu deleted the fp_log_and_tests branch January 31, 2024 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants