Skip to content

Commit

Permalink
Allow PPD when subject is not a column with grouping sets present (Zh…
Browse files Browse the repository at this point in the history
…ihua Deng, reviewed by Jesus Camacho Rodriguez)

Closes #1255
  • Loading branch information
dengzhhu653 committed Jul 17, 2020
1 parent 58d55e0 commit 44aa72f
Show file tree
Hide file tree
Showing 3 changed files with 877 additions and 23 deletions.
44 changes: 22 additions & 22 deletions ql/src/java/org/apache/hadoop/hive/ql/ppd/OpProcFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -792,40 +792,40 @@ public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
return null;
}

private void getGBYKeyPosFromExpr(ExprNodeDesc expr, List<ExprNodeDesc> groupByKeys,
List<Integer> gbyKeyPos) {
for (int i = 0; i < groupByKeys.size(); i++) {
if (groupByKeys.get(i).isSame(expr)) {
gbyKeyPos.add(i);
return;
}
}
if (expr.getChildren() != null) {
for (int i = 0; i < expr.getChildren().size(); i++) {
getGBYKeyPosFromExpr(expr.getChildren().get(i), groupByKeys, gbyKeyPos);
}
}
}

private boolean canPredPushdown(ExprNodeDesc expr, List<ExprNodeDesc> groupByKeys,
FastBitSet[] bitSets, int groupingSetPosition) {
List<ExprNodeDesc> columns = new ArrayList<ExprNodeDesc>();
extractCols(expr, columns);
for (ExprNodeDesc col : columns) {
int index = groupByKeys.indexOf(col);
assert index >= 0;
List<Integer> gbyKeyPos = new ArrayList<Integer>();
getGBYKeyPosFromExpr(expr, groupByKeys, gbyKeyPos);
// gbyKeysInExpr can be empty, maybe the expr is a boolean constant, let the expr push down
for (Integer pos : gbyKeyPos) {
for (FastBitSet bitset : bitSets) {
int keyPos = bitset.nextClearBit(0);
while (keyPos < groupingSetPosition && keyPos != index) {
while (keyPos < groupingSetPosition && keyPos != pos) {
keyPos = bitset.nextClearBit(keyPos + 1);
}
// If the column has not be found in grouping sets, the expr should not be pushed down
if (keyPos != index) {
// If the gbyKey has not be found in grouping sets, the expr should not be pushed down
if (keyPos != pos) {
return false;
}
}
}
return true;
}

// Extract columns from expression
private void extractCols(ExprNodeDesc expr, List<ExprNodeDesc> columns) {
if (expr instanceof ExprNodeColumnDesc) {
columns.add(expr);
}

if (expr instanceof ExprNodeGenericFuncDesc) {
List<ExprNodeDesc> children = expr.getChildren();
for (int i = 0; i < children.size(); ++i) {
extractCols(children.get(i), columns);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,56 @@ SELECT * FROM (
SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((a), (a, b))
) t WHERE b IS NULL;
) t WHERE b IS NULL;

EXPLAIN EXTENDED SELECT * FROM (
SELECT upper(a) x, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((a), (a, b))
) t WHERE x in ("AAA", "BBB");

SELECT * FROM (
SELECT upper(a) x, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((a), (a, b))
) t WHERE x in ('AAA', 'BBB');

EXPLAIN EXTENDED SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((a), (a, b))
HAVING upper(a) = 'AAA' AND 1 != 1;

SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((a), (a, b))
HAVING upper(a) = 'AAA' AND 1 != 1;

EXPLAIN EXTENDED SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((), (a), (a, b))
HAVING upper(a) = 'AAA' AND sum(s) > 100;

SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((), (a), (a, b))
HAVING upper(a) = 'AAA' AND sum(s) > 100;

EXPLAIN EXTENDED SELECT upper(a), b, sum(s)
FROM T1
GROUP BY upper(a), b GROUPING SETS ((upper(a)), (upper(a), b))
HAVING upper(a) = 'AAA' AND sum(s) > 100;

SELECT upper(a), b, sum(s)
FROM T1
GROUP BY upper(a), b GROUPING SETS ((upper(a)), (upper(a), b))
HAVING upper(a) = 'AAA' AND sum(s) > 100;

EXPLAIN EXTENDED SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((b), (a, b))
HAVING sum(s) > 100 and a IS NOT NULL AND upper(b) = 'BBB';

SELECT a, b, sum(s)
FROM T1
GROUP BY a, b GROUPING SETS ((b), (a, b))
HAVING sum(s) > 100 and a IS NOT NULL AND upper(b) = 'BBB';
Loading

0 comments on commit 44aa72f

Please sign in to comment.