Skip to content

[CALCITE-6534] Adjust type when pulling up Calc in JoinUnifyRule#3921

Merged
mihaibudiu merged 1 commit intoapache:mainfrom
suibianwanwank:6528-null-if
Aug 21, 2024
Merged

[CALCITE-6534] Adjust type when pulling up Calc in JoinUnifyRule#3921
mihaibudiu merged 1 commit intoapache:mainfrom
suibianwanwank:6528-null-if

Conversation

@suibianwanwank
Copy link
Contributor

@suibianwanwank suibianwanwank commented Aug 18, 2024

This PR addresses both [CALCITE-6534] and [CALCITE-6528] and is based on [CALCITE-6501]. For the problem of nullablility due to Join null-side, if RexNode satisfies null-if-null, we will adjust the nullability, if not, we should reject this rewrite regardless of the nullablility.

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 personally I am not very familiar with this part of the codebase so I am not confident enough to approve. Based on the tests, this seems to work very well, though.
The code looks generally fine, I left most comments that could improve readability.

@Test void testLeftProjectOnRightJoinToJoinFailed() {
* Assertion Error in JoinUnifyRule Due to Type Mismatch</a>.
*
* <p>Originally, this test was noMat due to a type mismatch.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this kind of comment is useful long term. It is useful for the reviewers of this PR, but not for maintaining the code. This comment would be better as a github comment rather than a code comment. Same for the subsequent comments.

private static boolean allProjectsNullable(List<RexNode> projects) {
return projects.stream()
.allMatch(project -> project.getType().isNullable());
/** Cast RexNode to the given type if only nullability differs. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I would also add "Otherwise throw". The current comment implies that in all other cases the type is left unchanged.

return true;
}

/** Determines if all projects are strong and the condition is always true. */
Copy link
Contributor

Choose a reason for hiding this comment

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

not clear what the "projects" are and what the condition is given single input parameter. Maybe you can add @param to describe where inputExplained comes from.

if (rightProjects != null && joinType.generatesNullsOn(1)
&& !allProjectsNullable(rightProjects)) {
return false;
private static List<RexNode> shiftAndAdjustProjectExpr(MutableJoin query, MutableJoin target,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think JavaDoc at least for some parameters could also help.
In particular, what is the target?

}

// In cases such as JoinOnLeftCalcToJoinUnifyRule and JoinOnCalcsToJoinUnifyRule rules,
// the left calc need to be pulled up the target, initialize the converter.
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be "where the left calc needs to be pulled up"?

@suibianwanwank
Copy link
Contributor Author

@mihaibudiu Thanks for the review, this function both shift and type adjustments, and I think it's really not easy to understand, so I've optimized those comments and added more of them.

* {@link JoinOnCalcsToJoinUnifyRule} <br/>
*
* @param query MutableRel of query
* @param target MutableRel of target
Copy link
Contributor

Choose a reason for hiding this comment

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

This information can be already obtained from the parameter declaration, the question I had is "what is the target?" Is the target the place where you are building the modified query?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The naming here comes from UnifyRuleCall, UnifyRule is bottom-down matching, target means the RelNode of the mv that is trying to match.
If you're interested in this part, Refinement for Substitution-Based Materialized View Matching is a great resource

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally that google doc would be posted somewhere publicly and linked from the code here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, it can be done in other issue

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 19, 2024
@mihaibudiu
Copy link
Contributor

You can squash the commits for merging

@suibianwanwank
Copy link
Contributor Author

You can squash the commits for merging

Done✅

@sonarqubecloud
Copy link

@mihaibudiu mihaibudiu merged commit 906a17f into apache:main Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants