io.trino.sql.planner.SymbolsExtractor Maven / Gradle / Ivy
/*
* Licensed 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 io.trino.sql.planner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.trino.sql.ir.DefaultTraversalVisitor;
import io.trino.sql.ir.Expression;
import io.trino.sql.ir.Lambda;
import io.trino.sql.ir.Reference;
import io.trino.sql.planner.iterative.Lookup;
import io.trino.sql.planner.plan.AggregationNode.Aggregation;
import io.trino.sql.planner.plan.PlanNode;
import io.trino.sql.planner.plan.WindowNode;
import java.util.List;
import java.util.Set;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.sql.planner.ExpressionExtractor.extractExpressions;
import static io.trino.sql.planner.ExpressionExtractor.extractExpressionsNonRecursive;
import static io.trino.sql.planner.iterative.Lookup.noLookup;
import static io.trino.sql.planner.optimizations.PlanNodeSearcher.searchFrom;
public final class SymbolsExtractor
{
private SymbolsExtractor() {}
public static Set extractUnique(PlanNode node)
{
ImmutableSet.Builder uniqueSymbols = ImmutableSet.builder();
extractExpressions(node).forEach(expression -> uniqueSymbols.addAll(extractUnique(expression)));
return uniqueSymbols.build();
}
public static Set extractUniqueNonRecursive(PlanNode node)
{
ImmutableSet.Builder uniqueSymbols = ImmutableSet.builder();
extractExpressionsNonRecursive(node).forEach(expression -> uniqueSymbols.addAll(extractUnique(expression)));
return uniqueSymbols.build();
}
public static Set extractUnique(PlanNode node, Lookup lookup)
{
ImmutableSet.Builder uniqueSymbols = ImmutableSet.builder();
extractExpressions(node, lookup).forEach(expression -> uniqueSymbols.addAll(extractUnique(expression)));
return uniqueSymbols.build();
}
public static Set extractUnique(Expression expression)
{
return ImmutableSet.copyOf(extractAll(expression));
}
public static Set extractUnique(Iterable extends Expression> expressions)
{
ImmutableSet.Builder unique = ImmutableSet.builder();
for (Expression expression : expressions) {
unique.addAll(extractAll(expression));
}
return unique.build();
}
public static Set extractUnique(Aggregation aggregation)
{
return ImmutableSet.copyOf(extractAll(aggregation));
}
public static Set extractUnique(WindowNode.Function function)
{
return ImmutableSet.copyOf(extractAll(function));
}
public static List extractAll(Expression expression)
{
ImmutableList.Builder builder = ImmutableList.builder();
new SymbolBuilderVisitor().process(expression, new Context(ImmutableSet.of(), builder));
return builder.build();
}
public static List extractAll(Aggregation aggregation)
{
ImmutableList.Builder builder = ImmutableList.builder();
for (Expression argument : aggregation.getArguments()) {
builder.addAll(extractAll(argument));
}
aggregation.getFilter().ifPresent(builder::add);
aggregation.getOrderingScheme().ifPresent(orderBy -> builder.addAll(orderBy.orderBy()));
aggregation.getMask().ifPresent(builder::add);
return builder.build();
}
public static List extractAll(WindowNode.Function function)
{
ImmutableList.Builder builder = ImmutableList.builder();
for (Expression argument : function.getArguments()) {
builder.addAll(extractAll(argument));
}
function.getFrame().getEndValue().ifPresent(builder::add);
function.getFrame().getSortKeyCoercedForFrameEndComparison().ifPresent(builder::add);
function.getFrame().getStartValue().ifPresent(builder::add);
function.getFrame().getSortKeyCoercedForFrameStartComparison().ifPresent(builder::add);
return builder.build();
}
public static Set extractOutputSymbols(PlanNode planNode)
{
return extractOutputSymbols(planNode, noLookup());
}
public static Set extractOutputSymbols(PlanNode planNode, Lookup lookup)
{
return searchFrom(planNode, lookup)
.findAll()
.stream()
.flatMap(node -> node.getOutputSymbols().stream())
.collect(toImmutableSet());
}
private static class SymbolBuilderVisitor
extends DefaultTraversalVisitor
{
@Override
protected Void visitReference(Reference node, Context context)
{
Symbol symbol = Symbol.from(node);
if (!context.lambdaArguments().contains(symbol)) {
context.builder().add(symbol);
}
return null;
}
@Override
protected Void visitLambda(Lambda node, Context context)
{
Context lambdaContext = new Context(
ImmutableSet.builder()
.addAll(context.lambdaArguments())
.addAll(node.arguments())
.build(),
context.builder());
process(node.body(), lambdaContext);
return null;
}
}
private record Context(Set lambdaArguments, ImmutableList.Builder builder) {}
}