Repository: phoenix Updated Branches: refs/heads/4.8-HBase-0.98 bc9c42111 -> 0b737ebb8
PHOENIX-3054 Counting zero null rows returns an empty result set Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/0b737ebb Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/0b737ebb Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/0b737ebb Branch: refs/heads/4.8-HBase-0.98 Commit: 0b737ebb89846e44bc3271df5ec4da28f06e4278 Parents: bc9c421 Author: Ankit Singhal <ankitsingha...@gmail.com> Authored: Tue Sep 6 11:10:06 2016 +0530 Committer: Ankit Singhal <ankitsingha...@gmail.com> Committed: Tue Sep 6 11:10:06 2016 +0530 ---------------------------------------------------------------------- .../phoenix/end2end/AggregateQueryIT.java | 36 ++++++++++++++++++++ .../apache/phoenix/execute/BaseQueryPlan.java | 7 +++- .../iterate/MergeSortResultIterator.java | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/0b737ebb/phoenix-core/src/it/java/org/apache/phoenix/end2end/AggregateQueryIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AggregateQueryIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AggregateQueryIT.java index f23b5eb..01cc2c1 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AggregateQueryIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AggregateQueryIT.java @@ -191,6 +191,42 @@ public class AggregateQueryIT extends BaseQueryIT { conn.close(); } } + + + @Test + public void testCountWithNoScanRanges() throws Exception { + String query = "SELECT count(1) FROM aTable WHERE organization_id = 'not_existing_organization_id'"; + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 2 + Connection conn = DriverManager.getConnection(getUrl(), props); + try { + PreparedStatement statement = conn.prepareStatement(query); + ResultSet rs = statement.executeQuery(); + assertTrue(rs.next()); + assertEquals(0, rs.getLong(1)); + assertFalse(rs.next()); + query = "SELECT count(1) FROM aTable WHERE organization_id = 'not_existing_organization_id' having count(*)>0"; + rs = conn.prepareStatement(query).executeQuery(); + assertFalse(rs.next()); + query = "SELECT count(1) FROM aTable WHERE organization_id = 'not_existing_organization_id' limit 1 offset 1"; + rs = conn.prepareStatement(query).executeQuery(); + assertFalse(rs.next()); + query = "SELECT count(1),123 FROM aTable WHERE organization_id = 'not_existing_organization_id'"; + rs = conn.prepareStatement(query).executeQuery(); + assertTrue(rs.next()); + assertEquals(0, rs.getLong(1)); + assertEquals("123", rs.getString(2)); + assertFalse(rs.next()); + query = "SELECT count(1),sum(x_decimal) FROM aTable WHERE organization_id = 'not_existing_organization_id'"; + rs = conn.prepareStatement(query).executeQuery(); + assertTrue(rs.next()); + assertEquals(0, rs.getLong(1)); + assertEquals(null, rs.getBigDecimal(2)); + assertFalse(rs.next()); + } finally { + conn.close(); + } + } @Test public void testCountIsNotNull() throws Exception { http://git-wip-us.apache.org/repos/asf/phoenix/blob/0b737ebb/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java b/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java index ff44d2e..74ac269 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/execute/BaseQueryPlan.java @@ -217,7 +217,12 @@ public abstract class BaseQueryPlan implements QueryPlan { } public final ResultIterator iterator(final List<? extends SQLCloseable> dependencies, ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { - if (context.getScanRanges() == ScanRanges.NOTHING) { + /* + * For aggregate queries, we still need to let the AggregationPlan to + * proceed so that we can give proper aggregates even if there are no + * row to be scanned. + */ + if (context.getScanRanges() == ScanRanges.NOTHING && !getStatement().isAggregate()) { return ResultIterator.EMPTY_ITERATOR; } http://git-wip-us.apache.org/repos/asf/phoenix/blob/0b737ebb/phoenix-core/src/main/java/org/apache/phoenix/iterate/MergeSortResultIterator.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/iterate/MergeSortResultIterator.java b/phoenix-core/src/main/java/org/apache/phoenix/iterate/MergeSortResultIterator.java index 961e766..d094bec 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/iterate/MergeSortResultIterator.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/iterate/MergeSortResultIterator.java @@ -70,7 +70,7 @@ public abstract class MergeSortResultIterator implements PeekingResultIterator { private PriorityQueue<MaterializedComparableResultIterator> getMinHeap() throws SQLException { if (minHeap == null) { List<PeekingResultIterator> iterators = resultIterators.getIterators(); - minHeap = new PriorityQueue<MaterializedComparableResultIterator>(iterators.size()); + minHeap = new PriorityQueue<MaterializedComparableResultIterator>(Math.max(1, iterators.size())); for (PeekingResultIterator itr : iterators) { if (itr.peek() == null) { itr.close();