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

io.prestosql.sql.planner.iterative.rule.ImplementExceptAsUnion Maven / Gradle / Ivy

There is a newer version: 350
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.prestosql.sql.planner.iterative.rule;

import com.google.common.collect.ImmutableList;
import io.prestosql.matching.Captures;
import io.prestosql.matching.Pattern;
import io.prestosql.metadata.Metadata;
import io.prestosql.sql.planner.iterative.Rule;
import io.prestosql.sql.planner.plan.Assignments;
import io.prestosql.sql.planner.plan.ExceptNode;
import io.prestosql.sql.planner.plan.FilterNode;
import io.prestosql.sql.planner.plan.ProjectNode;
import io.prestosql.sql.tree.Expression;
import io.prestosql.sql.tree.NotExpression;

import java.util.List;

import static com.google.common.collect.Iterables.getFirst;
import static io.prestosql.sql.ExpressionUtils.and;
import static io.prestosql.sql.planner.plan.Patterns.except;
import static java.util.Objects.requireNonNull;

/**
 * Converts EXCEPT queries into UNION ALL..GROUP BY...WHERE
 * E.g.:
 * 
 *     SELECT a FROM foo
 *     EXCEPT
 *     SELECT x FROM bar
 * 
* => *
 *     SELECT a
 *     FROM
 *     (
 *         SELECT a,
 *         COUNT(foo_marker) AS foo_count,
 *         COUNT(bar_marker) AS bar_count
 *         FROM
 *         (
 *             SELECT a, true as foo_marker, null as bar_marker
 *             FROM foo
 *             UNION ALL
 *             SELECT x, null as foo_marker, true as bar_marker
 *             FROM bar
 *         ) T1
 *     GROUP BY a
 *     ) T2
 *     WHERE foo_count >= 1 AND bar_count = 0;
 * 
*/ public class ImplementExceptAsUnion implements Rule { private static final Pattern PATTERN = except(); private final Metadata metadata; public ImplementExceptAsUnion(Metadata metadata) { this.metadata = requireNonNull(metadata, "metadata is null"); } @Override public Pattern getPattern() { return PATTERN; } @Override public Result apply(ExceptNode node, Captures captures, Context context) { SetOperationNodeTranslator translator = new SetOperationNodeTranslator(metadata, context.getSymbolAllocator(), context.getIdAllocator()); SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlan(node); ImmutableList.Builder predicatesBuilder = ImmutableList.builder(); List presentExpression = result.getPresentExpressions(); predicatesBuilder.add(getFirst(presentExpression, null)); for (int i = 1; i < presentExpression.size(); i++) { predicatesBuilder.add(new NotExpression(presentExpression.get(i))); } return Result.ofPlanNode( new ProjectNode( context.getIdAllocator().getNextId(), new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), and(predicatesBuilder.build())), Assignments.identity(node.getOutputSymbols()))); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy