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

Fix exception during plan duplicate columns in order by clause #3018

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -628,20 +628,24 @@ private PlanBuilder sort(PlanBuilder subPlan, List<SortItem> orderBy, Optional<S
Iterator<SortItem> sortItems = orderBy.iterator();

ImmutableList.Builder<Symbol> orderBySymbols = ImmutableList.builder();
ImmutableMap.Builder<Symbol, SortOrder> orderings = ImmutableMap.builder();
Map<Symbol, SortOrder> orderings = new HashMap<Symbol, SortOrder>();
for (FieldOrExpression fieldOrExpression : orderByExpressions) {
Symbol symbol = subPlan.translate(fieldOrExpression);
orderBySymbols.add(symbol);
if (orderings.containsKey(symbol)) {
sortItems.next();
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of calling next() in the two branches (which makes the code a little more error prone), assign sortItems.next() to a variable before the if block and use it below.

Also, invert the if condition. I think the code will read more natural:

for (...) {
    ...
    if (!orderings.contains(...)) {
       ... add to map, etc
    }
}

continue;
}

orderBySymbols.add(symbol);
orderings.put(symbol, toSortOrder(sortItems.next()));
}

PlanNode planNode;
if (limit.isPresent()) {
planNode = new TopNNode(idAllocator.getNextId(), subPlan.getRoot(), Long.parseLong(limit.get()), orderBySymbols.build(), orderings.build(), false);
planNode = new TopNNode(idAllocator.getNextId(), subPlan.getRoot(), Long.parseLong(limit.get()), orderBySymbols.build(), orderings, false);
}
else {
planNode = new SortNode(idAllocator.getNextId(), subPlan.getRoot(), orderBySymbols.build(), orderings.build());
planNode = new SortNode(idAllocator.getNextId(), subPlan.getRoot(), orderBySymbols.build(), orderings);
}

return new PlanBuilder(subPlan.getTranslations(), planNode, subPlan.getSampleWeight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,13 @@ public void testOrderByMultipleFields()
assertQueryOrdered("SELECT custkey, orderstatus FROM orders ORDER BY custkey DESC, orderstatus");
}

@Test
public void testOrderByDuplicateFields()
throws Exception
{
assertQueryOrdered("SELECT custkey, custkey FROM orders ORDER BY custkey, custkey");
Copy link
Contributor

Choose a reason for hiding this comment

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

Add the test I mentioned in my previous comments to ensure the sort order for fields that appear multiple times is handled correctly. E.g.,

SELECT custkey, custkey FROM orders ORDER BY custkey ASC, custkey DESC

}

@Test
public void testOrderByWithNulls()
throws Exception
Expand Down