Skip to content

Conversation

@Indhumathi27
Copy link
Contributor

@Indhumathi27 Indhumathi27 commented Nov 18, 2025

What changes were proposed in this pull request?

This PR updates TopNKeyProcessor to skip creating TopNKeyOperator when ReduceSinkDesc.topN is already set by LIMIT pushdown for ORDER BY LIMIT case. This prevents TopNKey from overriding pushdown and ensures the map-side LIMIT optimization is applied correctly.

Why are the changes needed?

Currently, when a query includes ORDER BY + LIMIT, LIMIT pushdown is generated during planning but is effectively overridden by the subsequent TopNKey rewrite. As a result, TopNKey operator receives full input rather than a reduced data set, leading to worse performance (e.g., 16M rows forwarded to reducer instead of a few). In cases where global ordering uses a single reducer, LIMIT pushdown is sufficient and far more efficient. This fix prevents unnecessary TopNKey creation so that pushdown can reduce shuffle and significantly improve execution time.

Test reports:

For query: select * from table order by h limit 100;
Total num of rows: 67764224

with fix:
Screenshot 2025-12-01 at 11 45 37 PM

without fix:
Screenshot 2025-12-01 at 11 49 39 PM

Does this PR introduce any user-facing change?

No

How was this patch tested?

Manual testing + existing testcases

@Indhumathi27 Indhumathi27 changed the title Draft: HIVE-29322: Avoid TopNKeyOperator When Map-Side LIMIT Pushdown Provides Better Pruning Nov 27, 2025
@Indhumathi27
Copy link
Contributor Author

@kasakrisz @deniskuzZ Can you help to review this PR. Thanks

@Indhumathi27 Indhumathi27 force-pushed the disable_topn branch 4 times, most recently from db493f2 to a003d03 Compare December 1, 2025 18:13
@Indhumathi27 Indhumathi27 changed the title HIVE-29322: Avoid TopNKeyOperator When Map-Side LIMIT Pushdown Provides Better Pruning Dec 1, 2025
@Indhumathi27
Copy link
Contributor Author

@ayushtkn @zabetak @kasakrisz @deniskuzZ Can you help to review the PR. thanks

// Skip the current optimization when a simple global ORDER BY...LIMIT is present
// (topN > -1 and hasOnlyOrderByLimit()).
// This plan structure is handled more efficiently by the specialized 'TopN In Reducer' optimization.
if (reduceSinkDesc.getTopN() > -1 && reduceSinkDesc.hasOnlyOrderByLimit()) {
Copy link
Member

@deniskuzZ deniskuzZ Dec 6, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also curious about the two questions in the JIRA ticket. I don't have an instant answer on whether it is optimal to reuse Top-N for the global sort. However, in my impression, it is at least suboptimal and might not be so bad.
Disclaimer: My next response might not be timely because I have to visit a hospital every day.

@zabetak
Copy link
Member

zabetak commented Dec 8, 2025

I left some questions under the JIRA ticket to better understand the issue that we are trying to solve here.

@Indhumathi27
Copy link
Contributor Author

@zabetak @okumin Added the analysis in the jira. Please check

Copy link
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.

I went over the plan changes and they look reasonable based on the goal of this PR. In terms of changes in the optimizer I left a few comments that could make the code a bit easier to follow.

sort order: ++
Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE Column stats: COMPLETE
value expressions: _col1 (type: string)
TopN Hash Memory Usage: 0.1
Copy link
Member

Choose a reason for hiding this comment

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

When the plan contains a Limit operator all subsequent TopN optimizations in the same mapper/reducer seem redundant. It's out of scope of the current PR but it may be worth logging a separate JIRA ticket as a potential improvement.

private transient boolean hasOrderBy = false;

// used to decide whether topn key optimisation can be applied
private transient boolean hasOnlyOrderByLimit = false;
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need this new indicator here? IT seems to be a hint about the structure of the plan rather than a property/quality of the ReduceSink operator.

Comment on lines +126 to +127
if (operator instanceof GroupByOperator || operator instanceof JoinOperator) {
hasOnlyOrderByLimit = false;
Copy link
Member

Choose a reason for hiding this comment

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

The decision to introduce or not a TopNKeyOperator is a responsibility of the the TopNKeyProcessor so if we need to make decisions based on the structure of the plan it makes more sense to do put the GBY/JOIN checks there.

// Skip the current optimization when a simple global ORDER BY...LIMIT is present
// (topN > -1 and hasOnlyOrderByLimit()).
// This plan structure is handled more efficiently by the specialized 'TopN In Reducer' optimization.
if (reduceSinkDesc.getTopN() > -1 && reduceSinkDesc.hasOnlyOrderByLimit()) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems that we don't want to introduce a TopNKeyOperator if the path between TS and RS does not contain a JOIN or GBY operator. We can add such checks at some point here or maybe better modify the respective RuleRegExp to only trigger when there is a GBY or JOIN in the path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants