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-4702] Error when executing query with GROUP BY constant via …
…JDBC adapter Add new method in SqlDialect controlling whether GROUP BY using literals is supported. Note that the whole Postgres family returns false by precaution; some literals may be supported by Postgres or some derivation of it. We agreed the extra complexity needed handle those special cases was not worth it so we decided to return false for all kinds of literals. Introduce a new rule to rewrite the GROUP BY using an inner join with a dummy table for those dialects that do not support literals. Add a rule based transformation step at the beginning of the rel to SQL conversion and ensure callers are passing from there. This allows to keep the aggregate constant transformation in a single place. Add tests with GROUP BY and different types of literals. Close apache#2482
- Loading branch information
1 parent
bb89b92
commit 466fb42
Showing
11 changed files
with
334 additions
and
4 deletions.
There are no files selected for viewing
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
142 changes: 142 additions & 0 deletions
142
core/src/main/java/org/apache/calcite/rel/rules/AggregateProjectConstantToDummyJoinRule.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,142 @@ | ||
/* | ||
* 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.JoinRelType; | ||
import org.apache.calcite.rel.core.Project; | ||
import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
import org.apache.calcite.rex.RexBuilder; | ||
import org.apache.calcite.rex.RexLiteral; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.tools.RelBuilder; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import org.immutables.value.Value; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Planner rule that recognizes a {@link org.apache.calcite.rel.core.Aggregate} | ||
* on top of a {@link org.apache.calcite.rel.core.Project} where the aggregate's group set | ||
* contains literals (true, false, DATE, chars, etc), and removes the literals from the | ||
* group keys by joining with a dummy table of literals. | ||
* | ||
* <pre>{@code | ||
* select avg(sal) | ||
* from emp | ||
* group by true, DATE '2022-01-01'; | ||
* }</pre> | ||
* becomes | ||
* <pre>{@code | ||
* select avg(sal) | ||
* from emp, (select true x, DATE '2022-01-01' d) dummy | ||
* group by dummy.x, dummy.d; | ||
* }</pre> | ||
*/ | ||
@Value.Enclosing | ||
public final class AggregateProjectConstantToDummyJoinRule | ||
extends RelRule<AggregateProjectConstantToDummyJoinRule.Config> { | ||
|
||
/** Creates an AggregateProjectConstantToDummyJoinRule. */ | ||
private AggregateProjectConstantToDummyJoinRule(Config config) { | ||
super(config); | ||
} | ||
|
||
@Override public boolean matches(RelOptRuleCall call) { | ||
final Aggregate aggregate = call.rel(0); | ||
final Project project = call.rel(1); | ||
|
||
for (int groupKey: aggregate.getGroupSet().asList()) { | ||
if (groupKey >= aggregate.getRowType().getFieldCount()) { | ||
continue; | ||
} | ||
RexNode groupKeyProject = project.getProjects().get(groupKey); | ||
if (groupKeyProject instanceof RexLiteral) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override public void onMatch(RelOptRuleCall call) { | ||
final Aggregate aggregate = call.rel(0); | ||
final Project project = call.rel(1); | ||
|
||
RelBuilder builder = call.builder(); | ||
RexBuilder rexBuilder = builder.getRexBuilder(); | ||
|
||
builder.push(project.getInput()); | ||
int offset = project.getInput().getRowType().getFieldCount(); | ||
|
||
RelDataTypeFactory.Builder valuesType = rexBuilder.getTypeFactory().builder(); | ||
List<RexLiteral> literals = new ArrayList<>(); | ||
List<RexNode> projects = project.getProjects(); | ||
for (int i = 0; i < projects.size(); i++) { | ||
RexNode node = projects.get(i); | ||
if (node instanceof RexLiteral) { | ||
literals.add((RexLiteral) node); | ||
valuesType.add(project.getRowType().getFieldList().get(i)); | ||
} | ||
} | ||
builder.values(ImmutableList.of(literals), valuesType.build()); | ||
|
||
builder.join(JoinRelType.INNER, rexBuilder.makeLiteral(true)); | ||
|
||
List<RexNode> newProjects = new ArrayList<>(); | ||
int literalCounter = 0; | ||
for (RexNode exp : project.getProjects()) { | ||
if (exp instanceof RexLiteral) { | ||
newProjects.add(builder.field(offset + literalCounter++)); | ||
} else { | ||
newProjects.add(exp); | ||
} | ||
} | ||
|
||
builder.project(newProjects); | ||
builder.aggregate( | ||
builder.groupKey( | ||
aggregate.getGroupSet(), aggregate.getGroupSets()), aggregate.getAggCallList() | ||
); | ||
|
||
call.transformTo(builder.build()); | ||
} | ||
|
||
/** Rule configuration. */ | ||
@Value.Immutable | ||
public interface Config extends RelRule.Config { | ||
Config DEFAULT = ImmutableAggregateProjectConstantToDummyJoinRule.Config.of() | ||
.withOperandFor(Aggregate.class, Project.class); | ||
|
||
@Override default AggregateProjectConstantToDummyJoinRule toRule() { | ||
return new AggregateProjectConstantToDummyJoinRule(this); | ||
} | ||
|
||
default Config withOperandFor(Class<? extends Aggregate> aggregateClass, | ||
Class<? extends Project> projectClass) { | ||
return withOperandSupplier(b0 -> | ||
b0.operand(aggregateClass).oneInput(b1 -> | ||
b1.operand(projectClass).anyInputs())) | ||
.as(Config.class); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.