Repository: phoenix Updated Branches: refs/heads/master cab39d721 -> 3bfa38dd9
PHOENIX-2444 Expansion of derived table query works incorrectly with aggregate over order by Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/7b2e1b14 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/7b2e1b14 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/7b2e1b14 Branch: refs/heads/master Commit: 7b2e1b1493c1fe2bbcf8d2c8b5de6ba1447f7ec3 Parents: 1b5e7fd Author: maryannxue <wei....@intel.com> Authored: Wed Nov 25 14:00:40 2015 -0500 Committer: maryannxue <wei....@intel.com> Committed: Wed Nov 25 14:00:40 2015 -0500 ---------------------------------------------------------------------- .../apache/phoenix/end2end/DerivedTableIT.java | 25 ++++++++++++++++ .../phoenix/compile/SubselectRewriter.java | 31 +++++++++++--------- 2 files changed, 42 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/7b2e1b14/phoenix-core/src/it/java/org/apache/phoenix/end2end/DerivedTableIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/DerivedTableIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/DerivedTableIT.java index 7b21f37..95a7fed 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/DerivedTableIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/DerivedTableIT.java @@ -332,6 +332,22 @@ public class DerivedTableIT extends BaseClientManagedTimeIT { rs = conn.createStatement().executeQuery("EXPLAIN " + query); assertEquals(plans[1], QueryUtil.getExplainPlan(rs)); + + // (orderby) groupby + query = "SELECT a_string, count(*) FROM (SELECT * FROM aTable order by a_integer) AS t where a_byte != 8 group by a_string"; + statement = conn.prepareStatement(query); + rs = statement.executeQuery(); + assertTrue (rs.next()); + assertEquals(A_VALUE,rs.getString(1)); + assertEquals(4,rs.getInt(2)); + assertTrue (rs.next()); + assertEquals(B_VALUE,rs.getString(1)); + assertEquals(3,rs.getInt(2)); + assertTrue (rs.next()); + assertEquals(C_VALUE,rs.getString(1)); + assertEquals(1,rs.getInt(2)); + + assertFalse(rs.next()); } finally { conn.close(); } @@ -678,6 +694,15 @@ public class DerivedTableIT extends BaseClientManagedTimeIT { assertEquals(8,rs.getInt(1)); assertFalse(rs.next()); + + // count (orderby) + query = "SELECT count(a_byte) FROM (SELECT * FROM aTable order by a_integer) AS t where a_byte != 8"; + statement = conn.prepareStatement(query); + rs = statement.executeQuery(); + assertTrue (rs.next()); + assertEquals(8,rs.getInt(1)); + + assertFalse(rs.next()); } finally { conn.close(); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/7b2e1b14/phoenix-core/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java index 5a91a17..853d772 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java @@ -127,6 +127,22 @@ public class SubselectRewriter extends ParseNodeRewriter { } } + if (select.isDistinct()) { + if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) { + return select; + } + isDistinctRewrite = true; + orderByRewrite = null; + } + + if (select.isAggregate()) { + if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) { + return select; + } + isAggregateRewrite = true; + orderByRewrite = null; + } + List<ParseNode> groupBy = select.getGroupBy(); if (!groupBy.isEmpty()) { if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) { @@ -139,6 +155,7 @@ public class SubselectRewriter extends ParseNodeRewriter { if (select.getHaving() != null) { havingRewrite = select.getHaving().accept(this); } + orderByRewrite = null; } List<AliasedNode> selectNodes = select.getSelect(); @@ -190,20 +207,6 @@ public class SubselectRewriter extends ParseNodeRewriter { hintRewrite = hintRewrite == null ? hint : HintNode.combine(hint, hintRewrite); } - if (select.isDistinct()) { - if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) { - return select; - } - isDistinctRewrite = true; - } - - if (select.isAggregate()) { - if (subselect.getLimit() != null || subselect.isAggregate() || subselect.isDistinct()) { - return select; - } - isAggregateRewrite = true; - } - return NODE_FACTORY.select(subselect.getFrom(), hintRewrite, isDistinctRewrite, selectNodesRewrite, whereRewrite, groupByRewrite, havingRewrite, orderByRewrite, limitRewrite, select.getBindCount(), isAggregateRewrite, select.hasSequence(), select.getSelects(), select.getUdfParseNodes()); }