Skip to content

Commit

Permalink
[BUG] GROUPING func and ORDER BY are used at the same time to report …
Browse files Browse the repository at this point in the history
…NullPointerException (#6266)

fix #6265

The reason for the error is that the `Grouping Func Exprs` is substituted twice. In the first substitution, `VirtualSlotRef` is used to replace the original `SlotRef`, and in the second substitution, `VirtualSlotRef` is reported in the `getTable()` Times Null pointer. IN
```
} else if (((SlotRef) child).getDesc().getParent().getTable().getType()
```
For the first substitution, the List of executable exprs in select clause has been substituted.
```
groupingInfo = new GroupingInfo(analyzer, groupByClause.getGroupingType());
            groupingInfo.substituteGroupingFn(resultExprs, analyzer);
```
In the second substitution, actually only need to substitute the unique expr in Ordering exprs.
```
createSortInfo(analyzer);
        if (sortInfo != null && CollectionUtils.isNotEmpty(sortInfo.getOrderingExprs())) {
            if (groupingInfo != null) {
                groupingInfo.substituteGroupingFn(sortInfo.getOrderingExprs(), analyzer);
            }
        }
```
change into:
```
createSortInfo(analyzer);
        if (sortInfo != null && CollectionUtils.isNotEmpty(sortInfo.getOrderingExprs())) {
            if (groupingInfo != null) {
                List<Expr> orderingExprNotInSelect = sortInfo.getOrderingExprs().stream()
                        .filter(item -> !resultExprs.contains(item)).collect(Collectors.toList());
                groupingInfo.substituteGroupingFn(orderingExprNotInSelect, analyzer);
            }
        }
```
  • Loading branch information
Xinyi Zou authored Jul 21, 2021
1 parent 327e31c commit 7771233
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* Representation of a single select block, including GROUP BY, ORDER BY and HAVING
Expand Down Expand Up @@ -508,7 +509,12 @@ public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
createSortInfo(analyzer);
if (sortInfo != null && CollectionUtils.isNotEmpty(sortInfo.getOrderingExprs())) {
if (groupingInfo != null) {
groupingInfo.substituteGroupingFn(sortInfo.getOrderingExprs(), analyzer);
// List of executable exprs in select clause has been substituted, only the unique expr in Ordering
// exprs needs to be substituted.
// Otherwise, if substitute twice for `Grouping Func Expr`, a null pointer will be reported.
List<Expr> orderingExprNotInSelect = sortInfo.getOrderingExprs().stream()
.filter(item -> !resultExprs.contains(item)).collect(Collectors.toList());
groupingInfo.substituteGroupingFn(orderingExprNotInSelect, analyzer);
}
}
analyzeAggregation(analyzer);
Expand Down

0 comments on commit 7771233

Please sign in to comment.