-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
import com.google.common.collect.Iterables; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
|
@@ -629,8 +630,15 @@ private PlanBuilder sort(PlanBuilder subPlan, List<SortItem> orderBy, Optional<S | |
|
||
ImmutableList.Builder<Symbol> orderBySymbols = ImmutableList.builder(); | ||
ImmutableMap.Builder<Symbol, SortOrder> orderings = ImmutableMap.builder(); | ||
Set<Symbol> orderBySymbolSet = new HashSet<Symbol>(); | ||
for (FieldOrExpression fieldOrExpression : orderByExpressions) { | ||
Symbol symbol = subPlan.translate(fieldOrExpression); | ||
|
||
if (orderBySymbolSet.contains(symbol)) { | ||
sortItems.next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also, invert the
|
||
continue; | ||
} | ||
orderBySymbolSet.add(symbol); | ||
orderBySymbols.add(symbol); | ||
|
||
orderings.put(symbol, toSortOrder(sortItems.next())); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.,
|
||
} | ||
|
||
@Test | ||
public void testOrderByWithNulls() | ||
throws Exception | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of keeping track of the symbols, I'd change the code to build a HashMap for
orderings
instead of Guava's ImmutableMap.builder.One thing to be careful with is that if the sort order for a given column is different, the first one should win. For instance, in:
SELECT a, a FROM t ORDER BY a ASC, a DESC
DESC
for the second occurrence is effectively a no-op.I'd add a test for this while you're at it.