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

Custom Calcite Rule to remove redundant references #16402

Merged
merged 18 commits into from May 14, 2024

Conversation

sreemanamala
Copy link
Contributor

@sreemanamala sreemanamala commented May 6, 2024

Description

Custom calcite rule mimicking AggregateProjectMergeRule to extend support to expressions.
The current calcite rule return null in such cases.
In addition, this removes the redundant references.


Key changed/added classes in this PR
  • DruidAggregateRemoveRedundancyRule
  • CalciteRulesManager

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@sreemanamala sreemanamala changed the title Custom Aggregate Project Merge Rule capable of merging expressions Custom Calcite Rule to remove redundant references May 7, 2024
AggregatorFactory factory = new GroupingAggregatorFactory(name, arguments);
AggregatorFactory factory = new GroupingAggregatorFactory(
name,
arguments.stream().distinct().collect(Collectors.toList())
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't this will mask the newly added check in GroupingAggregatorFactory ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made this a soft exception and removed this distinct masking here

LogicalProject(t=[TIME_FLOOR($0, 'PT1H')], $f1=[CASE(=($1, '#it.wikipedia'), $16, null:VARCHAR)], user=[$16], $f3=[IS TRUE(=($1, '#it.wikipedia'))])
LogicalTableScan(table=[[druid, wikipedia]])

!logicalPlan
Copy link
Member

Choose a reason for hiding this comment

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

its very sad to see that we can't capture the last valid logical plan from which the resulting query was generated...unfortunately we can't do much as this is how the old planner works...

if we would be able to show that - then we could just remove the native plan which is much harder to read

*/
@Value.Enclosing
public class DruidAggregateRemoveRedundancyRule
extends RelRule<DruidAggregateRemoveRedundancyRule.Config>
Copy link
Member

Choose a reason for hiding this comment

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

you could possibly extend RelOptRule instead; that doesn't need this config stuff;

or optionally move that immutables generated stuff inside this class somewhere - so its co-located

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made use of RelOptRule and got rid of the Immutable class

@cryptoe cryptoe modified the milestone: 30.0.0 May 9, 2024

private DruidAggregateRemoveRedundancyRule()
{
super(operand(Aggregate.class, operand(Project.class, any())));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
RelOptRule.operand
should be avoided because it has been deprecated.

private DruidAggregateRemoveRedundancyRule()
{
super(operand(Aggregate.class, operand(Project.class, any())));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
RelOptRule.operand
should be avoided because it has been deprecated.

private DruidAggregateRemoveRedundancyRule()
{
super(operand(Aggregate.class, operand(Project.class, any())));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
RelOptRule.any
should be avoided because it has been deprecated.
@@ -12941,8 +12941,7 @@ public void testRepeatedIdenticalVirtualExpressionGrouping()
.setVirtualColumns(expressionVirtualColumn("v0", "1", ColumnType.LONG))
.setDimensions(
dimensions(
new DefaultDimensionSpec("v0", "d0", ColumnType.LONG),
new DefaultDimensionSpec("v0", "d1", ColumnType.LONG)
new DefaultDimensionSpec("v0", "d0", ColumnType.LONG)
Copy link
Member

Choose a reason for hiding this comment

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

I was looking at this for a while - but actually its ok; since the case is reduced to TRUE because dim1 = NULL may never be TRUE

try {
factory = new GroupingAggregatorFactory(name, arguments);
}
catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

pass by comment, is this the correct way to handle any errors coming out of the agg factory?
A couple of things that I find ambiguous :

  1. Grouping Aggregation [%s] is not supported seems like misleading message incase there's an actual bug in the agg factory
  2. CannotBuildQueryException only takes in the message of the exception, which would make things harder to debug without a stack if there's an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rohangarg Thanks for the comment.
I have updated the exception message to remove not supported. I think the exception here would only be caused by the Precondition checks and that could be easily understood by the exception message
Made the exception look similar to the one we have in EarliestLatestBySqlAggregator and return null.
anyways, In the end DruidQuery would throw CannotBuildQueryException. So it made sense to not throw in the aggregator class.

Does this look good to you now?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the fixups :)
A couple of things I would check are :

  1. How does the whole user message look when the exception is thrown - does that make sense?
  2. Are all the exceptions thrown by the constructor distinguishable via the message - so that we aren't left with ambiguity when an exception arrives?

If both those things look fine, then it is good to go from my side too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. These exceptions which would be thrown here would result in not being able to plan the query in the current path and look for other ways. The custom rule which is introduced in this PR will make sure that the duplicate grouping would be removed and plan the query properly. In my understanding, the user would face this exception if the query can not be planned at all. As part of this testcase which I have added, Initially this would fail in plan and then turns towards the rule, gets converted and generated the final plan.
  2. By setting the planning error with the exception being received, this should be able to distinguish between the different exceptions based on the message

Copy link
Member

Choose a reason for hiding this comment

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

Ah, for 1, I was actually mentioning the meaningfulness of planning error message which would get surfaced if a query can't get planned. It does look reasonable to me.
For 2, thanks for the clarification.
LGTM

@kgyrtkirk kgyrtkirk merged commit b8dd747 into apache:master May 14, 2024
87 checks passed
@kgyrtkirk kgyrtkirk added the Bug label May 14, 2024
@sreemanamala sreemanamala deleted the grouping-sets branch May 20, 2024 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants