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

HIVE-28675: Maximize the removal of redundant columns from GROUP BY clauses #5586

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zabetak
Copy link
Member

@zabetak zabetak commented Dec 20, 2024

What changes were proposed in this pull request?

Enhance HiveRelFieldTrimmer to remove the maximum number of redundant columns from the GROUP BY clause.

Why are the changes needed?

  1. Generate more efficient plans by pruning as many columns as possible (less CPU/IO/network cost).
  2. Avoid missing optimization opportunities by examining all candidates.

For more see HIVE-28675.

Does this PR introduce any user-facing change?

More efficient query plans.

Is the change a dependency upgrade?

No

How was this patch tested?

mvn test -Dtest=TestMiniLlapLocalCliDriver.java -Dqfile="cbo_groupby_remove_key.q"

@zabetak zabetak marked this pull request as ready for review December 31, 2024 16:09
if (aggregate.getGroupSet().contains(key)) {
groupByUniqueKey = key;
break;
ImmutableBitSet removableCols = originalGroupSet.except(key).except(fieldsUsed);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to compute except(fieldsUsed) outside the loop? I believe changing the order of excepts will still yield the same resulting set.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we can do this, but just want to be careful that if there is no uniquekey match then we should not remove any columns at all(should skip the except in the return statement below in that case)

Copy link
Contributor

Choose a reason for hiding this comment

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

Just realized that we only update columnsToRemove when there is atleast one match. So I think yes we can move the except(fieldsUsed) outside the for loop.
Also is it efficient to loop the fieldsUsed(and then check if there is a field that is a unique key and part of aggregate keys to retain only fieldsUsed) vs looping uniquekeys? Mostly depends on size of fieldsUsed vs uniqueKeys. I will probably leave it upto you to decide on that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is possible to compute except(fieldsUsed) outside the loop. I applied the suggestion in e89386d.

Copy link
Member Author

Choose a reason for hiding this comment

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

Comparing the efficiency of the iteration between fieldsUsed and uniqueKeys is not possible because the semantics are different. The uniqueKeys variable is a set of sets (Set<ImmutableBitSet>) while fieldsUsed is a single set (ImmutableBitSet). Note that a key is not necessarily a single column but a set of columns (composite key).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for your explanation. Makes sense to me.

Copy link

sonarqubecloud bot commented Jan 3, 2025

Copy link
Contributor

@soumyakanti3578 soumyakanti3578 left a comment

Choose a reason for hiding this comment

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

LGTM!

@ramesh0201
Copy link
Contributor

ramesh0201 commented Jan 3, 2025

LGTM +1. Just left a minor comment for my understanding, please feel free to merge if this question is irrelevant. :)

EXPLAIN CBO SELECT passport, COUNT(1) FROM passenger GROUP BY id, fname, lname, passport;
EXPLAIN CBO SELECT fname, COUNT(1) FROM passenger GROUP BY id, fname, lname, passport;
EXPLAIN CBO SELECT lname, COUNT(1) FROM passenger GROUP BY id, fname, lname, passport;
EXPLAIN CBO SELECT fname, lname, COUNT(1) FROM passenger GROUP BY id, fname, lname, passport;
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, is having group by fname,lname always a better plan? -- even if there is a different aggregate function?

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.

4 participants