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.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 io.trino.sql.tree.DefaultExpressionTraversalVisitor;
import io.trino.sql.tree.DefaultTraversalVisitor;
import io.trino.sql.tree.DereferenceExpression;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.Identifier;
import io.trino.sql.tree.LambdaExpression;
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.SubqueryExpression;
import io.trino.sql.tree.SymbolReference;
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;
import static java.util.Objects.requireNonNull;
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, 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.getOrderBy()));
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();
}
// to extract qualified name with prefix
public static Set extractNames(Expression expression, Set> columnReferences)
{
ImmutableSet.Builder builder = ImmutableSet.builder();
new QualifiedNameBuilderVisitor(columnReferences, true).process(expression, builder);
return builder.build();
}
public static Set extractNamesNoSubqueries(Expression expression, Set> columnReferences)
{
ImmutableSet.Builder builder = ImmutableSet.builder();
new QualifiedNameBuilderVisitor(columnReferences, false).process(expression, builder);
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 DefaultExpressionTraversalVisitor>
{
@Override
protected Void visitSymbolReference(SymbolReference node, ImmutableList.Builder builder)
{
builder.add(Symbol.from(node));
return null;
}
@Override
protected Void visitLambdaExpression(LambdaExpression node, ImmutableList.Builder context)
{
// Symbols in lambda expression are bound to lambda arguments, so no need to extract them
return null;
}
}
private static class QualifiedNameBuilderVisitor
extends DefaultTraversalVisitor>
{
private final Set> columnReferences;
private final boolean recurseIntoSubqueries;
private QualifiedNameBuilderVisitor(Set> columnReferences, boolean recurseIntoSubqueries)
{
this.columnReferences = requireNonNull(columnReferences, "columnReferences is null");
this.recurseIntoSubqueries = recurseIntoSubqueries;
}
@Override
protected Void visitDereferenceExpression(DereferenceExpression node, ImmutableSet.Builder builder)
{
if (columnReferences.contains(NodeRef.of(node))) {
builder.add(DereferenceExpression.getQualifiedName(node));
}
else {
process(node.getBase(), builder);
}
return null;
}
@Override
protected Void visitIdentifier(Identifier node, ImmutableSet.Builder builder)
{
builder.add(QualifiedName.of(node.getValue()));
return null;
}
@Override
protected Void visitSubqueryExpression(SubqueryExpression node, ImmutableSet.Builder context)
{
if (!recurseIntoSubqueries) {
return null;
}
return super.visitSubqueryExpression(node, context);
}
}
}