This is an automated email from the ASF dual-hosted git repository.
krisztiankasa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new 9e4a0e7195c HIVE-27484: Limit pushdown with offset generate wrong
results (okumin, reviewed by Krisztian Kasa, Attila Turoczy)
9e4a0e7195c is described below
commit 9e4a0e7195ca5447d5369bd53a352423868ffc9d
Author: okumin <[email protected]>
AuthorDate: Fri Jul 21 17:59:19 2023 +0900
HIVE-27484: Limit pushdown with offset generate wrong results (okumin,
reviewed by Krisztian Kasa, Attila Turoczy)
---
.../calcite/rules/HiveSortJoinReduceRule.java | 27 ++--
.../clientpositive/llap/limit_join_transpose.q.out | 146 +++++++++++----------
2 files changed, 93 insertions(+), 80 deletions(-)
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortJoinReduceRule.java
b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortJoinReduceRule.java
index 1911ca6fa59..4910e0e6d95 100644
---
a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortJoinReduceRule.java
+++
b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortJoinReduceRule.java
@@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hive.ql.optimizer.calcite.rules;
+import java.math.BigDecimal;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.RelCollation;
@@ -26,7 +27,9 @@ import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
import org.apache.hadoop.hive.ql.optimizer.calcite.HiveCalciteUtil;
import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveJoin;
import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit;
@@ -94,14 +97,13 @@ public class HiveSortJoinReduceRule extends RelOptRule {
}
// Finally, if we do not reduce the input size, we bail out
- final int offset = sortLimit.offset == null ? 0 :
RexLiteral.intValue(sortLimit.offset);
- final RelMetadataQuery mq = call.getMetadataQuery();
- if (offset + RexLiteral.intValue(sortLimit.fetch)
- >= mq.getRowCount(reducedInput)) {
+ final long offset = sortLimit.offset == null ? 0 :
RexLiteral.intValue(sortLimit.offset);
+ final long offsetPlusLimit = offset + RexLiteral.intValue(sortLimit.fetch);
+ if (offsetPlusLimit > Integer.MAX_VALUE) {
return false;
}
-
- return true;
+ final RelMetadataQuery mq = call.getMetadataQuery();
+ return offsetPlusLimit < mq.getRowCount(reducedInput);
}
@Override
@@ -111,10 +113,19 @@ public class HiveSortJoinReduceRule extends RelOptRule {
RelNode inputLeft = join.getLeft();
RelNode inputRight = join.getRight();
+ final RexBuilder rexBuilder = sortLimit.getCluster().getRexBuilder();
+ final RexNode inputOffset =
rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
+ final int offset = sortLimit.offset == null ? 0 :
RexLiteral.intValue(sortLimit.offset);
+ final int limit = RexLiteral.intValue(sortLimit.fetch);
+ // LIMIT l OFFSET o without ORDER BY is semantically identical to LIMIT l
when o + l <= # of total rows.
+ // However, in a distributed environment, each task processes only a
subset of input data and is not aware of the
+ // final number of total rows. So, this rule pushes o + l to the upstream
node.
+ final RexNode inputLimit =
rexBuilder.makeExactLiteral(BigDecimal.valueOf(offset + limit));
+
// We create a new sort operator on the corresponding input
if (join.getJoinType() == JoinRelType.LEFT) {
inputLeft = sortLimit.copy(sortLimit.getTraitSet(), inputLeft,
- sortLimit.getCollation(), sortLimit.offset, sortLimit.fetch);
+ sortLimit.getCollation(), inputOffset, inputLimit);
((HiveSortLimit) inputLeft).setRuleCreated(true);
} else {
// Adjust right collation
@@ -123,7 +134,7 @@ public class HiveSortJoinReduceRule extends RelOptRule {
RelCollations.shift(sortLimit.getCollation(),
-join.getLeft().getRowType().getFieldCount()));
inputRight =
sortLimit.copy(sortLimit.getTraitSet().replace(rightCollation), inputRight,
- rightCollation, sortLimit.offset, sortLimit.fetch);
+ rightCollation, inputOffset, inputLimit);
((HiveSortLimit) inputRight).setRuleCreated(true);
}
// We copy the join and the top sort operator
diff --git a/ql/src/test/results/clientpositive/llap/limit_join_transpose.q.out
b/ql/src/test/results/clientpositive/llap/limit_join_transpose.q.out
index f61068d68a3..9f9e734e3b1 100644
--- a/ql/src/test/results/clientpositive/llap/limit_join_transpose.q.out
+++ b/ql/src/test/results/clientpositive/llap/limit_join_transpose.q.out
@@ -1250,17 +1250,16 @@ STAGE PLANS:
alias: src1
Statistics: Num rows: 500 Data size: 89000 Basic stats:
COMPLETE Column stats: COMPLETE
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Select Operator
expressions: key (type: string), value (type: string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Reduce Output Operator
null sort order:
sort order:
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
TopN Hash Memory Usage: 0.1
value expressions: _col0 (type: string), _col1 (type:
string)
Filter Operator
@@ -1283,20 +1282,23 @@ STAGE PLANS:
Execution mode: llap
Reduce Operator Tree:
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
Select Operator
expressions: VALUE._col0 (type: string), VALUE._col1 (type:
string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
- Reduce Output Operator
- key expressions: _col0 (type: string)
- null sort order: z
- sort order: +
- Map-reduce partition columns: _col0 (type: string)
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Limit
+ Number of rows: 1
+ Offset of rows: 1
Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
- value expressions: _col1 (type: string)
+ Reduce Output Operator
+ key expressions: _col0 (type: string)
+ null sort order: z
+ sort order: +
+ Map-reduce partition columns: _col0 (type: string)
+ Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ value expressions: _col1 (type: string)
Reducer 3
Execution mode: llap
Reduce Operator Tree:
@@ -1339,6 +1341,7 @@ limit 1 offset 1
POSTHOOK: type: QUERY
POSTHOOK: Input: default@src
#### A masked pattern was here ####
+86 val_86 86 val_86
PREHOOK: query: explain
select *
from src src1 right outer join (
@@ -1381,17 +1384,16 @@ STAGE PLANS:
alias: src2
Statistics: Num rows: 500 Data size: 89000 Basic stats:
COMPLETE Column stats: COMPLETE
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Select Operator
expressions: key (type: string), value (type: string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Reduce Output Operator
null sort order:
sort order:
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
TopN Hash Memory Usage: 0.1
value expressions: _col0 (type: string), _col1 (type:
string)
Filter Operator
@@ -1428,23 +1430,26 @@ STAGE PLANS:
Execution mode: llap
Reduce Operator Tree:
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
- Limit
- Number of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
- Select Operator
- expressions: VALUE._col0 (type: string), VALUE._col1
(type: string)
- outputColumnNames: _col0, _col1
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Select Operator
+ expressions: VALUE._col0 (type: string), VALUE._col1 (type:
string)
+ outputColumnNames: _col0, _col1
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Limit
+ Number of rows: 1
+ Offset of rows: 1
Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
- Reduce Output Operator
- key expressions: _col1 (type: string)
- null sort order: z
- sort order: +
- Map-reduce partition columns: _col1 (type: string)
+ Limit
+ Number of rows: 1
Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
- value expressions: _col0 (type: string)
+ Reduce Output Operator
+ key expressions: _col1 (type: string)
+ null sort order: z
+ sort order: +
+ Map-reduce partition columns: _col1 (type: string)
+ Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ value expressions: _col0 (type: string)
Reducer 3
Execution mode: llap
Reduce Operator Tree:
@@ -1518,6 +1523,7 @@ limit 1 offset 1
POSTHOOK: type: QUERY
POSTHOOK: Input: default@src
#### A masked pattern was here ####
+86 val_86 86 val_86 86 val_86
PREHOOK: query: explain
select *
from src src1 right outer join (
@@ -1561,17 +1567,16 @@ STAGE PLANS:
alias: src2
Statistics: Num rows: 500 Data size: 89000 Basic stats:
COMPLETE Column stats: COMPLETE
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Select Operator
expressions: key (type: string), value (type: string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
Reduce Output Operator
null sort order:
sort order:
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
TopN Hash Memory Usage: 0.1
value expressions: _col0 (type: string), _col1 (type:
string)
Filter Operator
@@ -1608,19 +1613,18 @@ STAGE PLANS:
Execution mode: llap
Reduce Operator Tree:
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
Select Operator
expressions: VALUE._col0 (type: string), VALUE._col1 (type:
string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
Reduce Output Operator
key expressions: _col1 (type: string)
null sort order: z
sort order: +
Map-reduce partition columns: _col1 (type: string)
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col0 (type: string)
Reducer 3
Execution mode: llap
@@ -1632,33 +1636,32 @@ STAGE PLANS:
0 _col1 (type: string)
1 _col1 (type: string)
outputColumnNames: _col0, _col1, _col2, _col3
- Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 1602 Basic stats: COMPLETE
Column stats: COMPLETE
Limit
- Number of rows: 1
- Statistics: Num rows: 1 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
Reduce Output Operator
null sort order:
sort order:
- Statistics: Num rows: 1 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 712 Basic stats:
COMPLETE Column stats: COMPLETE
TopN Hash Memory Usage: 0.1
value expressions: _col0 (type: string), _col1 (type:
string), _col2 (type: string), _col3 (type: string)
Reducer 4
Execution mode: llap
Reduce Operator Tree:
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
Select Operator
expressions: VALUE._col0 (type: string), VALUE._col1 (type:
string), VALUE._col2 (type: string), VALUE._col3 (type: string)
outputColumnNames: _col0, _col1, _col2, _col3
- Statistics: Num rows: 1 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
Reduce Output Operator
key expressions: _col0 (type: string)
null sort order: z
sort order: +
Map-reduce partition columns: _col0 (type: string)
- Statistics: Num rows: 1 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 712 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col1 (type: string), _col2 (type:
string), _col3 (type: string)
Reducer 5
Execution mode: llap
@@ -1670,7 +1673,7 @@ STAGE PLANS:
0 _col0 (type: string)
1 _col0 (type: string)
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5
- Statistics: Num rows: 2 Data size: 1068 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 2492 Basic stats: COMPLETE
Column stats: COMPLETE
Limit
Number of rows: 1
Offset of rows: 1
@@ -1709,6 +1712,7 @@ limit 1 offset 1
POSTHOOK: type: QUERY
POSTHOOK: Input: default@src
#### A masked pattern was here ####
+238 val_238 238 val_238 238 val_238
PREHOOK: query: explain
select *
from src src1 right outer join (
@@ -1807,15 +1811,14 @@ STAGE PLANS:
outputColumnNames: _col0, _col1
Statistics: Num rows: 500 Data size: 89000 Basic stats:
COMPLETE Column stats: COMPLETE
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
Reduce Output Operator
key expressions: _col1 (type: string)
null sort order: z
sort order: +
Map-reduce partition columns: _col1 (type: string)
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col0 (type: string)
Reducer 3
Execution mode: llap
@@ -1827,22 +1830,22 @@ STAGE PLANS:
0 _col0 (type: string)
1 _col1 (type: string)
outputColumnNames: _col1, _col2
- Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 890 Basic stats: COMPLETE
Column stats: COMPLETE
Top N Key Operator
sort order: +
keys: _col1 (type: string)
null sort order: z
- Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 890 Basic stats: COMPLETE
Column stats: COMPLETE
top n: 2
Select Operator
expressions: _col1 (type: string), _col2 (type: string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 890 Basic stats:
COMPLETE Column stats: COMPLETE
Reduce Output Operator
key expressions: _col0 (type: string)
null sort order: z
sort order: +
- Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 890 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col1 (type: string)
Reducer 4
Execution mode: llap
@@ -1850,17 +1853,16 @@ STAGE PLANS:
Select Operator
expressions: KEY.reducesinkkey0 (type: string), VALUE._col0
(type: string)
outputColumnNames: _col0, _col1
- Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 890 Basic stats: COMPLETE
Column stats: COMPLETE
Limit
- Number of rows: 1
- Offset of rows: 1
- Statistics: Num rows: 1 Data size: 178 Basic stats: COMPLETE
Column stats: COMPLETE
+ Number of rows: 2
+ Statistics: Num rows: 2 Data size: 356 Basic stats: COMPLETE
Column stats: COMPLETE
Reduce Output Operator
key expressions: _col0 (type: string)
null sort order: z
sort order: +
Map-reduce partition columns: _col0 (type: string)
- Statistics: Num rows: 1 Data size: 178 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 2 Data size: 356 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col1 (type: string)
Reducer 5
Execution mode: llap
@@ -1872,18 +1874,18 @@ STAGE PLANS:
0 _col0 (type: string)
1 _col0 (type: string)
outputColumnNames: _col0, _col1, _col2, _col3
- Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 1602 Basic stats: COMPLETE
Column stats: COMPLETE
Top N Key Operator
sort order: +
keys: _col2 (type: string)
null sort order: z
- Statistics: Num rows: 2 Data size: 712 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 1602 Basic stats:
COMPLETE Column stats: COMPLETE
top n: 2
Reduce Output Operator
key expressions: _col2 (type: string)
null sort order: z
sort order: +
- Statistics: Num rows: 2 Data size: 712 Basic stats:
COMPLETE Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 1602 Basic stats:
COMPLETE Column stats: COMPLETE
value expressions: _col0 (type: string), _col1 (type:
string), _col3 (type: string)
Reducer 6
Execution mode: llap
@@ -1891,7 +1893,7 @@ STAGE PLANS:
Select Operator
expressions: VALUE._col0 (type: string), VALUE._col1 (type:
string), KEY.reducesinkkey0 (type: string), VALUE._col2 (type: string)
outputColumnNames: _col0, _col1, _col2, _col3
- Statistics: Num rows: 2 Data size: 534 Basic stats: COMPLETE
Column stats: COMPLETE
+ Statistics: Num rows: 5 Data size: 1246 Basic stats: COMPLETE
Column stats: COMPLETE
Limit
Number of rows: 1
Offset of rows: 1