From 54311736cc83e77ac90ff7f362911ce8129e772d Mon Sep 17 00:00:00 2001 From: Ashutosh Chauhan Date: Fri, 19 Aug 2016 16:13:49 -0700 Subject: [PATCH] HIVE-14590 : CBO (Calcite Return Path) Incorrect result set when limit is present in one of the union branches --- .../optimizer/calcite/translator/HiveOpConverter.java | 7 +++++++ ql/src/test/queries/clientpositive/union_null.q | 3 +++ ql/src/test/results/clientpositive/union_null.q.out | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java index 8d56595..972de07 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/HiveOpConverter.java @@ -581,6 +581,13 @@ OpAttr visit(HiveUnion unionRel) throws SemanticException { Operator unionOp = OperatorFactory.getAndMakeChild( semanticAnalyzer.getOpContext(), unionDesc, new RowSchema(cinfoLst), children); + for (RelNode input : unionRel.getInputs()) { + // disable global limit if any union branch has limit in it + if (input instanceof HiveSortLimit) { + this.semanticAnalyzer.getQB().getParseInfo().setOuterQueryLimit(-1); + break; + } + } if (LOG.isDebugEnabled()) { LOG.debug("Generated " + unionOp + " with row schema: [" + unionOp.getSchema() + "]"); } diff --git a/ql/src/test/queries/clientpositive/union_null.q b/ql/src/test/queries/clientpositive/union_null.q index a17325c..23da07a 100644 --- a/ql/src/test/queries/clientpositive/union_null.q +++ b/ql/src/test/queries/clientpositive/union_null.q @@ -2,6 +2,9 @@ -- HIVE-2901 select x from (select * from (select value as x from src order by x limit 5)a union all select * from (select NULL as x from src limit 5)b )a; +set hive.cbo.returnpath.hiveop=true; +select x from (select * from (select value as x from src order by x limit 5)a union all select * from (select NULL as x from src limit 5)b )a; +set hive.cbo.returnpath.hiveop=false; -- HIVE-4837 select * from (select * from (select null as N from src1 group by key)a UNION ALL select * from (select null as N from src1 group by key)b ) a; diff --git a/ql/src/test/results/clientpositive/union_null.q.out b/ql/src/test/results/clientpositive/union_null.q.out index 32cdf65..a3407d4 100644 --- a/ql/src/test/results/clientpositive/union_null.q.out +++ b/ql/src/test/results/clientpositive/union_null.q.out @@ -22,6 +22,24 @@ NULL NULL NULL NULL +PREHOOK: query: select x from (select * from (select value as x from src order by x limit 5)a union all select * from (select NULL as x from src limit 5)b )a +PREHOOK: type: QUERY +PREHOOK: Input: default@src +#### A masked pattern was here #### +POSTHOOK: query: select x from (select * from (select value as x from src order by x limit 5)a union all select * from (select NULL as x from src limit 5)b )a +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +#### A masked pattern was here #### +NULL +NULL +NULL +NULL +NULL +val_0 +val_0 +val_0 +val_10 +val_100 PREHOOK: query: -- HIVE-4837 select * from (select * from (select null as N from src1 group by key)a UNION ALL select * from (select null as N from src1 group by key)b ) a PREHOOK: type: QUERY -- 1.7.12.4 (Apple Git-37)