xiedeyantu commented on code in PR #4598: URL: https://github.com/apache/calcite/pull/4598#discussion_r2460055473
########## core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; + +import com.google.common.collect.ImmutableSet; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle. Review Comment: Javadoc should typically describe what this rule does, specifically the transformation from which type of SQL to which other type of SQL, or from which type of Plan to which other type of Plan. ########## core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; + +import com.google.common.collect.ImmutableSet; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle. + */ [email protected] +public class AggregateExtractLiteralAggRule + extends RelRule<AggregateExtractLiteralAggRule.Config> + implements TransformationRule { + + protected AggregateExtractLiteralAggRule(Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + final LogicalAggregate aggregate = call.rel(0); + + final List<AggregateCall> aggCalls = aggregate.getAggCallList(); + if (aggCalls.isEmpty()) { + return; + } + + // Collect indices of LITERAL_AGG calls. + final List<Integer> literalAggIndices = new ArrayList<>(); + for (int i = 0; i < aggCalls.size(); i++) { + final AggregateCall ac = aggCalls.get(i); + if (ac.getAggregation().getName().equals("LITERAL_AGG")) { Review Comment: Would it be better to use `ac.getAggregation().getKind() == SqlKind.LITERAL_AGG`? ########## core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; + +import com.google.common.collect.ImmutableSet; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle. + */ [email protected] +public class AggregateExtractLiteralAggRule + extends RelRule<AggregateExtractLiteralAggRule.Config> + implements TransformationRule { + + protected AggregateExtractLiteralAggRule(Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + final LogicalAggregate aggregate = call.rel(0); Review Comment: Is better to use Aggregate? ########## core/src/main/java/org/apache/calcite/rel/rules/AggregateExtractLiteralAggRule.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; + +import com.google.common.collect.ImmutableSet; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * AggregateExtractLiteralAggRule gets rid of the LITERAL_AGG into most databases can handle. + */ [email protected] +public class AggregateExtractLiteralAggRule + extends RelRule<AggregateExtractLiteralAggRule.Config> + implements TransformationRule { + + protected AggregateExtractLiteralAggRule(Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + final LogicalAggregate aggregate = call.rel(0); + + final List<AggregateCall> aggCalls = aggregate.getAggCallList(); + if (aggCalls.isEmpty()) { + return; + } + + // Collect indices of LITERAL_AGG calls. + final List<Integer> literalAggIndices = new ArrayList<>(); + for (int i = 0; i < aggCalls.size(); i++) { + final AggregateCall ac = aggCalls.get(i); + if (ac.getAggregation().getName().equals("LITERAL_AGG")) { + literalAggIndices.add(i); + } + } + + if (literalAggIndices.isEmpty()) { + // nothing to do + return; + } + + // Build new AggregateCall list without LITERAL_AGG entries. + final List<AggregateCall> newAggCalls = new ArrayList<>(); + final Map<Integer, Integer> oldAggIndexToNewAggIndex = new HashMap<>(); + int newAggPos = 0; + for (int i = 0; i < aggCalls.size(); i++) { + if (!literalAggIndices.contains(i)) { + newAggCalls.add(aggCalls.get(i)); + oldAggIndexToNewAggIndex.put(i, newAggPos++); + } + } + + // Create new Aggregate preserving groupSet/groupSets/hints. + final LogicalAggregate newAggregate = Review Comment: I noticed that the operators in this rule are all created using the create method(Aggregate and Project). Could they be changed to use RelBuilder? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
