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-6451] Improve Nullability Derivation for Intersect and Minus #3845

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vbarua
Copy link

@vbarua vbarua commented Jul 5, 2024

Issue: CALCITE-6451

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testUnionTypeDerivation(boolean all) {
final RelBuilder builder = RelBuilder.create(config().build());
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure if this is the best place for these tests. I'm open to suggestions about where they should go.

Copy link
Contributor

Choose a reason for hiding this comment

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

@vbarua vbarua marked this pull request as draft July 5, 2024 22:05
@vbarua vbarua force-pushed the vbarua/CALCITE-6451/nullability-derivation-for-minus-and-intersect branch from 3259727 to c28c090 Compare July 5, 2024 22:20
@vbarua
Copy link
Author

vbarua commented Jul 6, 2024

This interacts interestingly/poorly with the IntersectToDistinctRule, which rewrites Intersects to Unions using something like:

LogicalIntersect(all=[false])
  LogicalProject(a=[$0], b=[$1], c=[$2])
    LogicalTableScan(table=[[CATALOG, TEMP, FOO]])
  LogicalProject(a=[$0], b=[$1], c=[$2])
    LogicalTableScan(table=[[CATALOG, TEMP, BAR]])

which becomes

LogicalProject(a=[$0], b=[$1], c=[$2])
  LogicalFilter(condition=[=($9, 2)])
    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
      LogicalUnion(all=[true])
        LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
          LogicalProject(a=[$0], b=[$1], c=[$2])
            LogicalTableScan(table=[[CATALOG, TEMP, FOO]])
        LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
          LogicalProject(a=[$0], b=[$1], c=[$2])
            LogicalTableScan(table=[[CATALOG, TEMP, BAR]])

What's interesting about this is that while it returns the same results, it's actually somewhat lossy with regards to the nullability information because we can't infer as tight nullability bounds for the second form as we can the first. That is to say, if FOO.a is nullable but BAR.a is not nullable, then with the first form we can say that in the output a will not be nullable but this is not the case with the second form.

One way I could see to work around this would be to improve the IntersectToDistinctRule by including filtering information when possible. That is, when a column is not nullable in all Intersect branches, we add an IS NOT NULL filter to exclude nulls:

LogicalProject(a=[$0], b=[$1], c=[$2])
  LogicalFilter(condition=[=($9, 2)])
    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
      LogicalUnion(all=[true])
        LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
          LogicalFilter(condition=[IS NOT NULL($0)] <-- Filter here excludes NULL columns
            LogicalProject(a=[$0], b=[$1], c=[$2])
              LogicalTableScan(table=[[CATALOG, TEMP, FOO]])
        LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()])
          LogicalProject(a=[$0], b=[$1], c=[$2])
            LogicalTableScan(table=[[CATALOG, TEMP, BAR]])

However that might also require additional work because I don't believe that Calcite can use the presence of an IS NOT NULL filter to change the output nullability.

The reason this is a problem now is that when the IntersectToDisctinctRule is applied that types of the RelNode going in and RelNode coming out no longer match.

For Minus, the output column nullability is that of the primary input

For Intersect, an output column is nullable if and only if it is
nullable in all of the inputs
@vbarua vbarua force-pushed the vbarua/CALCITE-6451/nullability-derivation-for-minus-and-intersect branch from c28c090 to 6238792 Compare July 6, 2024 00:05
@vbarua vbarua marked this pull request as ready for review July 6, 2024 00:05
Copy link
Contributor

@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.

I will edit the PR title to match the JIRA ticket title.
You will have to do the same for the commit message.

@mihaibudiu mihaibudiu changed the title [CALCITE-6451] Refine nullability of outputs for MINUS and INTERSECT [CALCITE-6451] Improve Nullability Derivation for Intersect and Minus Jul 8, 2024
@mihaibudiu
Copy link
Contributor

I think you will need to fix the IntersectToDistinct rule before we can merge this PR.

Updates the IntersectToDistinctRule to:
* Filter inputs before aggregation to exclude nulls when possible
* Force the correct nullabilities for output columns
@vbarua
Copy link
Author

vbarua commented Jul 20, 2024

I've pushed some changes that address the issue with the IntersectToDistinctRule. Unfortunately, it looks there's a different issue as well. The test in:

# INTERSECT
select deptno from "scott".emp
intersect
select deptno from "scott".dept;
+--------+
| DEPTNO |
+--------+
| 10 |
| 20 |
| 30 |
+--------+
(3 rows)

is failing now because the output type of the relational tree no longer matches the type of the validated SQL tree. I suspect I need to update the SQL validation logic as well to address that. I will continue to look at this when I have a chance.

@mihaibudiu
Copy link
Contributor

I will convert this to a draft, since it's not yet ready

@mihaibudiu mihaibudiu marked this pull request as draft August 13, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants