forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6214] Remove
DISTINCT
in COUNT
if field is unique
- Loading branch information
1 parent
e2c84a6
commit 725ce51
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
core/src/main/java/org/apache/calcite/rel/rules/AggregateRemoveDistinctRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.rel.rules; | ||
|
||
import org.apache.calcite.plan.RelOptRuleCall; | ||
import org.apache.calcite.plan.RelRule; | ||
import org.apache.calcite.rel.core.Aggregate; | ||
import org.apache.calcite.rel.core.AggregateCall; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.util.ImmutableBitSet; | ||
|
||
import org.immutables.value.Value; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Planner rule that removes a distinct in count for {@link Aggregate}. | ||
* if relational expression is already distinct. | ||
* (such as {@code COUNT(DISTINCT x)}) | ||
* | ||
* @see CoreRules#AGGREGATE_REMOVE_DISTINCT | ||
*/ | ||
@Value.Enclosing | ||
public class AggregateRemoveDistinctRule | ||
extends RelRule<AggregateRemoveDistinctRule.Config> | ||
implements SubstitutionRule { | ||
|
||
/** | ||
* Creates an AggregateRemoveDistinctRule. | ||
*/ | ||
protected AggregateRemoveDistinctRule(Config config) { | ||
super(config); | ||
} | ||
|
||
@Override public void onMatch(RelOptRuleCall call) { | ||
final Aggregate topAggregate = call.rel(0); | ||
final Aggregate bottomAggregate = call.rel(1); | ||
// Input must be distinct one column | ||
final ImmutableBitSet groupSet = bottomAggregate.getGroupSet(); | ||
if (groupSet.cardinality() != 1) { | ||
return; | ||
} | ||
// Remove `DISTINCT` for `COUNT(DISTINCT $0)` | ||
boolean removed = false; | ||
final List<AggregateCall> newAggregateCallList = new ArrayList<>(); | ||
for (AggregateCall aggregateCall : topAggregate.getAggCallList()) { | ||
if (aggregateCall.isDistinct() | ||
&& aggregateCall.getAggregation().getKind() == SqlKind.COUNT | ||
&& aggregateCall.getArgList().size() == 1 | ||
&& aggregateCall.getArgList().get(0) < groupSet.cardinality()) { | ||
// Arg0 must be unique key | ||
final AggregateCall newAggregateCall = aggregateCall.withDistinct(false); | ||
newAggregateCallList.add(newAggregateCall); | ||
removed = true; | ||
} else { | ||
newAggregateCallList.add(aggregateCall); | ||
} | ||
} | ||
// Create a new Aggregate if we can remove `DISTINCT` | ||
if (removed) { | ||
final Aggregate newAggregate = | ||
topAggregate.copy(topAggregate.getTraitSet(), bottomAggregate, | ||
topAggregate.getGroupSet(), | ||
topAggregate.getGroupSets(), | ||
newAggregateCallList); | ||
call.transformTo(newAggregate); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Rule configuration. | ||
*/ | ||
@Value.Immutable | ||
public interface Config extends RelRule.Config { | ||
AggregateRemoveDistinctRule.Config DEFAULT = ImmutableAggregateRemoveDistinctRule | ||
.Config.of() | ||
.withOperandSupplier(b0 -> | ||
b0.operand(Aggregate.class) | ||
.oneInput(b1 -> | ||
b1.operand(Aggregate.class) | ||
.predicate(Aggregate::isSimple) | ||
.anyInputs())) | ||
.as(AggregateRemoveDistinctRule.Config.class); | ||
|
||
@Override default AggregateRemoveDistinctRule toRule() { | ||
return new AggregateRemoveDistinctRule(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters