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

[CALCITE-6702] Strong Policy for the POWER Function is wrong #4059

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ninidhiaeddine
Copy link

CALCITE-6702: Strong Policy for the SqlStdOperatorTable.POWER Function is wrongly assigned AS_IS when it should be ANY.

Context:
In various SQL db implementations, the POWER(a, b) function returns NULL when either or both operands are null.

SELECT POWER(NULL, 2) AS "ALIAS1"; -- => Returns NULL
SELECT POWER(2, NULL) AS "ALIAS2"; -- => Returns NULL
SELECT POWER(NULL ,NULL) AS "ALIAS3"; -- => Returns NULL

These are all the cases I could think of:
image

This means that the correct null policy for the POWER function is ANY instead of AS_IS, since this expression is NULL if and only if at least one of its arguments is NULL.

@ninidhiaeddine ninidhiaeddine changed the title CALCITE-6702: Strong Policy for the POWER Function is wrong [CALCITE-6702]: Strong Policy for the POWER Function is wrong Nov 22, 2024
@ninidhiaeddine ninidhiaeddine changed the title [CALCITE-6702]: Strong Policy for the POWER Function is wrong [CALCITE-6702] Strong Policy for the POWER Function is wrong Nov 22, 2024
@NobiGo
Copy link
Contributor

NobiGo commented Nov 22, 2024

@ninidhiaeddine We need a unit test for this change.

@ninidhiaeddine
Copy link
Author

@ninidhiaeddine We need a unit test for this change.

I was thinking to add a unit test for this change, but I didn't find any existing unit tests that are checking the returned Strong.Policy for a given SqlOperator.

The only unit tests that I found to be remotely related are: SqlOperatorTest#testPowerFun() and SqlOperatorTest#testPowerDecimalFunc() which check that POWER(2, NULL) and POWER(NULL, 2) both return NULL. However, none of these methods make any call to Strong.policy(arg).

Any ideas on what to test here exactly? Please let me know.

@caicancai
Copy link
Member

caicancai commented Nov 22, 2024

@ninidhiaeddine We need a unit test for this change.

I was thinking to add a unit test for this change, but I didn't find any existing unit tests that are checking the returned Strong.Policy for a given SqlOperator.

The only unit tests that I found to be remotely related are: SqlOperatorTest#testPowerFun() and SqlOperatorTest#testPowerDecimalFunc() which check that POWER(2, NULL) and POWER(NULL, 2) both return NULL. However, none of these methods make any call to Strong.policy(arg).

Any ideas on what to test here exactly? Please let me know.

@ninidhiaeddine Hi. You can add tests in sqloperatortest

@ninidhiaeddine
Copy link
Author

ninidhiaeddine commented Nov 22, 2024

@ninidhiaeddine Hi. You can add tests in sqloperatortest

Could you please clarify?
The only relevant check I can do is Assert.assertEquals(Strong.Policy.ANY, Strong.policy(SqlStdOperatorTable.POWER) but it would be out of context as none of the existing test methods in this class currently validate the Strong.Policy of the SqlOperators. What do you think?

@mihaibudiu
Copy link
Contributor

The Strong class is used during optimizations.
The JavaDoc for Strong says "Utilities for strong predicates."
However, POWER is not a predicate.
So I don't know whether this change has any effect.
It is not easy to audit all the places where Strong is used in the codebase to determine whether it will have any effect on any program. Most places I could find only simplify predicates.
I don't know if this has any effect on the *_REDUCE_EXPRESSIONS set of optimization rules, where perhaps POWER with a constant null argument could be simplified to NULL at compile-time.
Do you have an example which is affected by this issue?

@mihaibudiu
Copy link
Contributor

BTW: there is another class NullPolicy which seems to overlap in functionality with Strong. That class is used during code-generation for the Enum interpreter.

@mihaibudiu
Copy link
Contributor

The only place where the Policy is actually used is in RexSimplify, where it is used to check whether some expressions are always null. So perhaps you can can write a SqlToRelConverter test that checks to see whether the call to POWER(null, x) is replaced in the program with a constant NULL.

@mihaibudiu
Copy link
Contributor

Since Strong provides only a conservative approximation, AS_IS is not wrong, it may be just a missed optimization opportunity.

@ninidhiaeddine
Copy link
Author

ninidhiaeddine commented Nov 26, 2024

The Strong class is used during optimizations. The JavaDoc for Strong says "Utilities for strong predicates." However, POWER is not a predicate. So I don't know whether this change has any effect. It is not easy to audit all the places where Strong is used in the codebase to determine whether it will have any effect on any program. Most places I could find only simplify predicates. I don't know if this has any effect on the *_REDUCE_EXPRESSIONS set of optimization rules, where perhaps POWER with a constant null argument could be simplified to NULL at compile-time. Do you have an example which is affected by this issue?

Thank you for your reply @mihaibudiu . Let's move the discussion to the Jira issue if you don't mind, since @julianhyde is probably not seeing the discussion here. I wrote my reply there.
Once we agree on the changes, we will continue here

@ninidhiaeddine ninidhiaeddine marked this pull request as draft November 28, 2024 14:31
Copy link

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 90 days if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions bot added the stale label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants