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

io.trino.sql.analyzer.NamesExtractor Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * 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.analyzer;

import com.google.common.collect.ImmutableSet;
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.NodeRef;
import io.trino.sql.tree.QualifiedName;
import io.trino.sql.tree.SubqueryExpression;

import java.util.Set;

import static java.util.Objects.requireNonNull;

public final class NamesExtractor
{
    private NamesExtractor() {}

    // 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();
    }

    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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy