diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java index ae4e077b97ef..703098cb3457 100644 --- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java +++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java @@ -2535,13 +2535,14 @@ private AggregateCall removeRedundantDistinct( ImmutableBitSet groupSet, RelNode r) { if (aggregateCall.isDistinct() && !config.redundantDistinct()) { - final RelMetadataQuery mq = r.getCluster().getMetadataQuery(); + final RelNode relNode = requireNonNull(r, "RelNode"); + final RelMetadataQuery mq = relNode.getCluster().getMetadataQuery(); final List argList = aggregateCall.getArgList(); final ImmutableBitSet distinctArg = ImmutableBitSet.builder() .addAll(argList) .build(); final ImmutableBitSet columns = groupSet.union(distinctArg); - final boolean alreadyUnique = mq.areColumnsUnique(r, columns); + final boolean alreadyUnique = mq.areColumnsUnique(relNode, columns); if (alreadyUnique) { return aggregateCall.withDistinct(false); }