All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.rel.rules.AggregateJoinJoinRemoveRule Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * 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 com.hazelcast.org.apache.calcite.rel.rules;

import com.hazelcast.org.apache.calcite.plan.RelOptRuleCall;
import com.hazelcast.org.apache.calcite.plan.RelOptUtil;
import com.hazelcast.org.apache.calcite.plan.RelRule;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.Aggregate;
import com.hazelcast.org.apache.calcite.rel.core.AggregateCall;
import com.hazelcast.org.apache.calcite.rel.core.Join;
import com.hazelcast.org.apache.calcite.rel.core.JoinRelType;
import com.hazelcast.org.apache.calcite.rel.logical.LogicalAggregate;
import com.hazelcast.org.apache.calcite.rel.logical.LogicalJoin;
import com.hazelcast.org.apache.calcite.rex.RexNode;
import com.hazelcast.org.apache.calcite.rex.RexUtil;
import com.hazelcast.org.apache.calcite.tools.RelBuilder;
import com.hazelcast.org.apache.calcite.tools.RelBuilderFactory;
import com.hazelcast.org.apache.calcite.util.ImmutableBitSet;
import com.hazelcast.org.apache.calcite.util.mapping.Mappings;

import com.hazelcast.com.google.common.collect.ImmutableList;

import org.immutables.value.Value;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Planner rule that matches an {@link com.hazelcast.org.apache.calcite.rel.core.Aggregate}
 * on a {@link com.hazelcast.org.apache.calcite.rel.core.Join} and removes the left input
 * of the join provided that the left input is also a left join if possible.
 *
 * 

For instance, * *

*
select distinct s.product_id, pc.product_id
 * from sales as s
 * left join product as p
 *   on s.product_id = p.product_id
 * left join product_class pc
 *   on s.product_id = pc.product_id
* *

becomes * *

*
select distinct s.product_id, pc.product_id
 * from sales as s
 * left join product_class pc
 *   on s.product_id = pc.product_id
* * @see CoreRules#AGGREGATE_JOIN_JOIN_REMOVE */ @Value.Enclosing public class AggregateJoinJoinRemoveRule extends RelRule implements TransformationRule { /** Creates an AggregateJoinJoinRemoveRule. */ protected AggregateJoinJoinRemoveRule(Config config) { super(config); } @Deprecated // to be removed before 2.0 public AggregateJoinJoinRemoveRule( Class aggregateClass, Class joinClass, RelBuilderFactory relBuilderFactory) { this(Config.DEFAULT .withRelBuilderFactory(relBuilderFactory) .as(Config.class) .withOperandFor(aggregateClass, joinClass)); } @Override public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final Join topJoin = call.rel(1); final Join bottomJoin = call.rel(2); int leftBottomChildSize = bottomJoin.getLeft().getRowType() .getFieldCount(); // Check whether the aggregate uses columns in the right input of // bottom join. final Set allFields = RelOptUtil.getAllFields(aggregate); if (allFields.stream().anyMatch(i -> i >= leftBottomChildSize && i < bottomJoin.getRowType().getFieldCount())) { return; } if (aggregate.getAggCallList().stream().anyMatch(aggregateCall -> !aggregateCall.isDistinct())) { return; } // Check whether the top join uses columns in the right input of bottom join. final List leftKeys = new ArrayList<>(); RelOptUtil.splitJoinCondition(topJoin.getLeft(), topJoin.getRight(), topJoin.getCondition(), leftKeys, new ArrayList<>(), new ArrayList<>()); if (leftKeys.stream().anyMatch(s -> s >= leftBottomChildSize)) { return; } // Check whether left join keys in top join and bottom join are equal. final List leftChildKeys = new ArrayList<>(); RelOptUtil.splitJoinCondition(bottomJoin.getLeft(), bottomJoin.getRight(), bottomJoin.getCondition(), leftChildKeys, new ArrayList<>(), new ArrayList<>()); if (!leftKeys.equals(leftChildKeys)) { return; } int offset = bottomJoin.getRight().getRowType().getFieldCount(); final RelBuilder relBuilder = call.builder(); RexNode condition = RexUtil.shift(topJoin.getCondition(), leftBottomChildSize, -offset); RelNode join = relBuilder.push(bottomJoin.getLeft()) .push(topJoin.getRight()) .join(topJoin.getJoinType(), condition) .build(); final Map map = new HashMap<>(); allFields.forEach( index -> map.put(index, index < leftBottomChildSize ? index : index - offset)); final ImmutableBitSet groupSet = aggregate.getGroupSet().permute(map); final ImmutableList.Builder aggCalls = ImmutableList.builder(); final int sourceCount = aggregate.getInput().getRowType().getFieldCount(); final Mappings.TargetMapping targetMapping = Mappings.target(map, sourceCount, sourceCount); aggregate.getAggCallList().forEach( aggregateCall -> aggCalls.add(aggregateCall.transform(targetMapping))); RelNode newAggregate = relBuilder.push(join) .aggregate(relBuilder.groupKey(groupSet), aggCalls.build()) .build(); call.transformTo(newAggregate); } /** Rule configuration. */ @Value.Immutable public interface Config extends RelRule.Config { Config DEFAULT = ImmutableAggregateJoinJoinRemoveRule.Config.of() .withOperandFor(LogicalAggregate.class, LogicalJoin.class); @Override default AggregateJoinJoinRemoveRule toRule() { return new AggregateJoinJoinRemoveRule(this); } /** Defines an operand tree for the given classes. */ default Config withOperandFor(Class aggregateClass, Class joinClass) { return withOperandSupplier(b0 -> b0.operand(aggregateClass) .oneInput(b1 -> b1.operand(joinClass) .predicate(join -> join.getJoinType() == JoinRelType.LEFT) .inputs(b2 -> b2.operand(joinClass) .predicate(join -> join.getJoinType() == JoinRelType.LEFT) .anyInputs()))).as(Config.class); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy