Skip to content

Commit

Permalink
Fix ORDER BY with LIMIT 0
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Feb 11, 2015
1 parent 0ebc021 commit 6742c65
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public TopNOperator(
this.operatorContext = checkNotNull(operatorContext, "operatorContext is null");
this.types = checkNotNull(types, "types is null");

checkArgument(n > 0, "n must be greater than zero");
checkArgument(n >= 0, "n must be positive");
this.n = n;

this.sortTypes = checkNotNull(sortTypes, "sortTypes is null");
Expand All @@ -141,6 +141,10 @@ public TopNOperator(
this.memoryManager = new TopNMemoryManager(checkNotNull(operatorContext, "operatorContext is null"));

this.pageBuilder = new PageBuilder(types);

if (n == 0) {
finishing = true;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TopNNode(@JsonProperty("id") PlanNodeId id,
super(id);

Preconditions.checkNotNull(source, "source is null");
Preconditions.checkArgument(count > 0, "count must be positive");
Preconditions.checkArgument(count >= 0, "count must be positive");
Preconditions.checkNotNull(orderBy, "orderBy is null");
Preconditions.checkArgument(!orderBy.isEmpty(), "orderBy is empty");
Preconditions.checkArgument(orderings.size() == orderBy.size(), "orderBy and orderings sizes don't match");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static com.facebook.presto.testing.MaterializedResult.resultBuilder;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.testng.Assert.assertEquals;

@Test(singleThreaded = true)
public class TestTopNOperator
Expand Down Expand Up @@ -162,4 +163,32 @@ public void testReverseOrder()

assertOperatorEquals(operator, input, expected);
}

@Test
public void testLimitZero()
throws Exception
{
List<Page> input = rowPagesBuilder(BIGINT).row(1).build();

TopNOperatorFactory factory = new TopNOperatorFactory(
0,
ImmutableList.of(BIGINT),
0,
ImmutableList.of(0),
ImmutableList.of(DESC_NULLS_LAST),
false);

Operator operator = factory.createOperator(driverContext);

MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT).build();

// assertOperatorEquals assumes operators do not start in finished state
assertEquals(operator.isFinished(), true);
assertEquals(operator.needsInput(), false);
assertEquals(operator.getOutput(), null);

List<Page> pages = OperatorAssertion.toPages(operator, input.iterator());
MaterializedResult actual = OperatorAssertion.toMaterializedResult(operator.getOperatorContext().getSession(), operator.getTypes(), pages);
assertEquals(actual, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ public void testLimitZero()
assertQuery("SELECT custkey, totalprice FROM orders LIMIT 0");
}

@Test
public void testOrderByLimitZero()
throws Exception
{
assertQuery("SELECT custkey, totalprice FROM orders ORDER BY orderkey LIMIT 0");
}

@Test
public void testRepeatedAggregations()
throws Exception
Expand Down

0 comments on commit 6742c65

Please sign in to comment.