Return-Path: X-Original-To: apmail-drill-commits-archive@www.apache.org Delivered-To: apmail-drill-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BE70918FF1 for ; Thu, 25 Jun 2015 12:53:21 +0000 (UTC) Received: (qmail 11594 invoked by uid 500); 25 Jun 2015 12:53:21 -0000 Delivered-To: apmail-drill-commits-archive@drill.apache.org Received: (qmail 11569 invoked by uid 500); 25 Jun 2015 12:53:21 -0000 Mailing-List: contact commits-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: commits@drill.apache.org Delivered-To: mailing list commits@drill.apache.org Received: (qmail 11560 invoked by uid 99); 25 Jun 2015 12:53:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Jun 2015 12:53:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 802BCE3677; Thu, 25 Jun 2015 12:53:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jni@apache.org To: commits@drill.apache.org Message-Id: <5179750ced984d75b2a56623d94b0b29@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: drill git commit: DRILL-3210 : Expand star column properly when it is used together with window function in projection list of SQL query. Date: Thu, 25 Jun 2015 12:53:21 +0000 (UTC) Repository: drill Updated Branches: refs/heads/master 3f0d9221d -> 58c3c4c69 DRILL-3210 : Expand star column properly when it is used together with window function in projection list of SQL query. Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/58c3c4c6 Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/58c3c4c6 Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/58c3c4c6 Branch: refs/heads/master Commit: 58c3c4c69748bdf2423c1b4f543407faef50a767 Parents: 3f0d922 Author: Jinfeng Ni Authored: Wed Jun 24 13:16:27 2015 -0700 Committer: Jinfeng Ni Committed: Wed Jun 24 23:28:56 2015 -0700 ---------------------------------------------------------------------- .../drill/exec/planner/physical/WindowPrel.java | 30 +++++++++++++++- .../org/apache/drill/TestExampleQueries.java | 37 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/58c3c4c6/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java index 122dee8..170438e 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/WindowPrel.java @@ -20,6 +20,8 @@ package org.apache.drill.exec.planner.physical; import com.google.common.collect.Lists; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rel.type.RelRecordType; import org.apache.drill.common.expression.ExpressionPosition; import org.apache.drill.common.expression.FieldReference; import org.apache.drill.common.expression.FunctionCall; @@ -42,9 +44,11 @@ import org.apache.calcite.rex.RexLiteral; import org.apache.calcite.util.BitSets; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; import static com.google.common.base.Preconditions.checkState; @@ -60,7 +64,8 @@ public class WindowPrel extends DrillWindowRelBase implements Prel { @Override public RelNode copy(RelTraitSet traitSet, List inputs) { - return new WindowPrel(getCluster(), traitSet, sole(inputs), constants, getRowType(), groups.get(0)); + final RelDataType copiedRowType = deriveCopiedRowTypeFromInput(sole(inputs)); + return new WindowPrel(getCluster(), traitSet, sole(inputs), constants, copiedRowType, groups.get(0)); } @Override @@ -145,4 +150,27 @@ public class WindowPrel extends DrillWindowRelBase implements Prel { public Iterator iterator() { return PrelUtil.iter(getInput()); } + + /** + * Derive rowType for the copied WindowPrel based on input. + * When copy() is called, the input might be different from the current one's input. + * We have to use the new input's field in the copied WindowPrel. + */ + private RelDataType deriveCopiedRowTypeFromInput(final RelNode input) { + final RelDataType inputRowType = input.getRowType(); + final RelDataType windowRowType = this.getRowType(); + + final List fieldList = new ArrayList<>(inputRowType.getFieldList()); + final int inputFieldCount = inputRowType.getFieldCount(); + final int windowFieldCount = windowRowType.getFieldCount(); + + for (int i = inputFieldCount; i < windowFieldCount; i++) { + fieldList.add(windowRowType.getFieldList().get(i)); + } + + final RelDataType rowType = this.getCluster().getRexBuilder().getTypeFactory().createStructType(fieldList); + + return rowType; + } + } http://git-wip-us.apache.org/repos/asf/drill/blob/58c3c4c6/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java b/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java index 6d03d81..e8af325 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java +++ b/exec/java-exec/src/test/java/org/apache/drill/TestExampleQueries.java @@ -1054,4 +1054,41 @@ public class TestExampleQueries extends BaseTestQuery { } + @Test // DRILL-3210 + public void testWindowFunAndStarCol() throws Exception { + // SingleTableQuery : star + window function + final String query = + " select * , sum(n_nationkey) over (partition by n_regionkey) as sumwin " + + " from cp.`tpch/nation.parquet`"; + final String baseQuery = + " select n_nationkey, n_name, n_regionkey, n_comment, " + + " sum(n_nationkey) over (partition by n_regionkey) as sumwin " + + " from cp.`tpch/nation.parquet`"; + testBuilder() + .sqlQuery(query) + .unOrdered() + .sqlBaselineQuery(baseQuery) + .build() + .run();; + + // JoinQuery: star + window function + final String joinQuery = + " select *, sum(n.n_nationkey) over (partition by r.r_regionkey order by r.r_name) as sumwin" + + " from cp.`tpch/nation.parquet` n, cp.`tpch/region.parquet` r " + + " where n.n_regionkey = r.r_regionkey"; + final String joinBaseQuery = + " select n.n_nationkey, n.n_name, n.n_regionkey, n.n_comment, r.r_regionkey, r.r_name, r.r_comment, " + + " sum(n.n_nationkey) over (partition by r.r_regionkey order by r.r_name) as sumwin " + + " from cp.`tpch/nation.parquet` n, cp.`tpch/region.parquet` r " + + " where n.n_regionkey = r.r_regionkey"; + + testBuilder() + .sqlQuery(joinQuery) + .unOrdered() + .sqlBaselineQuery(joinBaseQuery) + .build() + .run(); + + } + }