|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule; |
| 21 | + |
| 22 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNode; |
| 23 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.Assignments; |
| 24 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.Symbol; |
| 25 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Rule; |
| 26 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.ProjectNode; |
| 27 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.UnionNode; |
| 28 | +import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Expression; |
| 29 | +import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SymbolReference; |
| 30 | +import org.apache.iotdb.db.queryengine.plan.relational.utils.matching.Capture; |
| 31 | +import org.apache.iotdb.db.queryengine.plan.relational.utils.matching.Captures; |
| 32 | +import org.apache.iotdb.db.queryengine.plan.relational.utils.matching.Pattern; |
| 33 | + |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | +import com.google.common.collect.ImmutableListMultimap; |
| 36 | +import org.apache.tsfile.read.common.type.Type; |
| 37 | + |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.ExpressionSymbolInliner.inlineSymbols; |
| 43 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.node.Patterns.project; |
| 44 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.node.Patterns.source; |
| 45 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.node.Patterns.union; |
| 46 | +import static org.apache.iotdb.db.queryengine.plan.relational.utils.matching.Capture.newCapture; |
| 47 | + |
| 48 | +public class PushProjectionThroughUnion implements Rule<ProjectNode> { |
| 49 | + private static final Capture<UnionNode> CHILD = newCapture(); |
| 50 | + |
| 51 | + private static final Pattern<ProjectNode> PATTERN = |
| 52 | + project() |
| 53 | + .matching(PushProjectionThroughUnion::nonTrivialProjection) |
| 54 | + .with(source().matching(union().capturedAs(CHILD))); |
| 55 | + |
| 56 | + @Override |
| 57 | + public Pattern<ProjectNode> getPattern() { |
| 58 | + return PATTERN; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Result apply(ProjectNode parent, Captures captures, Context context) { |
| 63 | + UnionNode child = captures.get(CHILD); |
| 64 | + |
| 65 | + // OutputLayout of the resultant Union, will be same as the layout of the Project |
| 66 | + List<Symbol> outputLayout = parent.getOutputSymbols(); |
| 67 | + |
| 68 | + // Mapping from the output symbol to ordered list of symbols from each of the children |
| 69 | + ImmutableListMultimap.Builder<Symbol, Symbol> mappings = ImmutableListMultimap.builder(); |
| 70 | + |
| 71 | + // sources for the resultant UnionNode |
| 72 | + ImmutableList.Builder<PlanNode> outputSources = ImmutableList.builder(); |
| 73 | + |
| 74 | + for (int i = 0; i < child.getChildren().size(); i++) { |
| 75 | + Map<Symbol, SymbolReference> outputToInput = |
| 76 | + child.sourceSymbolMap(i); // Map: output of union -> input of this child to the union |
| 77 | + Assignments.Builder assignments = |
| 78 | + Assignments.builder(); // assignments for the new ProjectNode |
| 79 | + |
| 80 | + // mapping from current ProjectNode to new ProjectNode, used to identify the output layout |
| 81 | + Map<Symbol, Symbol> projectSymbolMapping = new HashMap<>(); |
| 82 | + |
| 83 | + // Translate the assignments in the ProjectNode using symbols of the source of the UnionNode |
| 84 | + for (Map.Entry<Symbol, Expression> entry : parent.getAssignments().entrySet()) { |
| 85 | + Expression translatedExpression = inlineSymbols(outputToInput, entry.getValue()); |
| 86 | + Type type = context.getSymbolAllocator().getTypes().getTableModelType(entry.getKey()); |
| 87 | + Symbol symbol = context.getSymbolAllocator().newSymbol(translatedExpression, type); |
| 88 | + assignments.put(symbol, translatedExpression); |
| 89 | + projectSymbolMapping.put(entry.getKey(), symbol); |
| 90 | + } |
| 91 | + outputSources.add( |
| 92 | + new ProjectNode( |
| 93 | + context.getIdAllocator().genPlanNodeId(), |
| 94 | + child.getChildren().get(i), |
| 95 | + assignments.build())); |
| 96 | + outputLayout.forEach(symbol -> mappings.put(symbol, projectSymbolMapping.get(symbol))); |
| 97 | + } |
| 98 | + |
| 99 | + return Result.ofPlanNode( |
| 100 | + new UnionNode( |
| 101 | + parent.getPlanNodeId(), |
| 102 | + outputSources.build(), |
| 103 | + mappings.build(), |
| 104 | + ImmutableList.copyOf(mappings.build().keySet()))); |
| 105 | + } |
| 106 | + |
| 107 | + private static boolean nonTrivialProjection(ProjectNode project) { |
| 108 | + return !project.getAssignments().getExpressions().stream() |
| 109 | + .allMatch(SymbolReference.class::isInstance); |
| 110 | + } |
| 111 | +} |
0 commit comments