Skip to content

[CALCITE-3868] Remove redundant ruleSet and ruleNames in VolcanoPlanner#1869

Merged
hsyuan merged 1 commit intoapache:masterfrom
hsyuan:remove_ruleset
Apr 21, 2020
Merged

[CALCITE-3868] Remove redundant ruleSet and ruleNames in VolcanoPlanner#1869
hsyuan merged 1 commit intoapache:masterfrom
hsyuan:remove_ruleset

Conversation

@hsyuan
Copy link
Copy Markdown
Member

@hsyuan hsyuan commented Mar 23, 2020

Copy link
Copy Markdown
Contributor

@chunweilei chunweilei left a comment

Choose a reason for hiding this comment

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

Nice catch~

: "Rule's description should not contain '$': "
+ description;
assert !INTEGER_PATTERN.matcher(description).matches()
: "Rule's description should not be an integer: "
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How should we validate the rule description pattern now ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When creating the rule

Copy link
Copy Markdown
Member

@zabetak zabetak left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @hsyuan ! I left some small comments. Apart from that, I think the history.md should be updated with a small note about the change in these public APIs.

* are unique.
*/
private final Map<String, RelOptRule> mapDescToRule = new HashMap<>();
protected final Map<String, RelOptRule> mapDescToRule = new HashMap<>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With this change the map becomes the main data structure holding the rules so I think the Javadoc should be updated to reflect this. Increasing the visibility of the field makes it public API; it might be better to keep this private and rely on public/protected methods to recover the necessary info.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we may need to change it to LinkedHashMap to be consistent with the requirement of HepPlanner.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It turns out we still need it to be protected to avoid copying.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that in most cases the number of rules is not very big so I was thinking that copying vs. mapDescToRule.values() is not going to have significant performance overhead in the planning phase, thus, I tend to prefer better encapsulation. Having that said, I do not have any concrete measures to support my claims (just instinct that could be wrong) :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I agree the rules is not very big. But in onNewClass, every time we add a new logical/physical operator we have to copy it. It will copy N times depends on how many operators you have. Though not a significant overhead, but I think we still need to avoid multiple times copy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with any option so I am leaving the final decision up to you :)

// Check that there isn't a rule with the same description,
// also validating description string.

protected boolean mapRuleDescription(RelOptRule rule) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The method now basically registers the rule to the planner so it might be better to rename this entirely to addRule.

* @return the rule that is removed, or null if no rule is removed
*/
protected void unmapRuleDescription(RelOptRule rule) {
protected RelOptRule unmapRuleDescription(RelOptRule rule) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rename this to removeRule and update the Javadoc?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

will do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The parameter of this method is weird, should be string description of the rule, like removeRule(String desc) ?

this.provenanceMap.clear();
}

public List<RelOptRule> getRules() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be a good idea to move the method to AbstractRelOptPlanner? This would allow us to keep mapDescToRule private and replace calls to mapDescToRule.values() with calls to this method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes.

return ImmutableList.copyOf(mapDescToRule.values());
}

public boolean addRule(RelOptRule rule) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we choose to add this method to the superclass (instead of having mapRuleDescription) then here we can ovverride.

Copy link
Copy Markdown
Member Author

@hsyuan hsyuan Mar 25, 2020

Choose a reason for hiding this comment

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

@zabetak I have addressed your comments. Thanks.

if (instruction.ruleSet == null) {
instruction.ruleSet = new LinkedHashSet<>();
for (RelOptRule rule : allRules) {
for (RelOptRule rule : mapDescToRule.values()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we already know the size of mapDescToRule, we can create a set with exact size, which will eliminate capacity expansion overhead and space waste when creating. Even though this is trivial update, I think it is always a good manner to create collection in such way if possible.

instruction.ruleSet = Sets.newHashSetWithExpectedSize(mapDescToRule.size());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, will do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It turns out the size instruction.ruleSet is undetermined, we don't know the exact size. So I will leave as it is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree, there is "if" check to determine what to be added. I didn't find that out clearly, sorry about the false alert.

@hsyuan hsyuan added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Apr 12, 2020
@hsyuan hsyuan merged commit 90f3e98 into apache:master Apr 21, 2020
@hsyuan hsyuan deleted the remove_ruleset branch April 21, 2020 09:54
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. slow-tests-needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants