gortiz commented on code in PR #15027: URL: https://github.com/apache/pinot/pull/15027#discussion_r1952517625
########## pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotLeftJoinToNotInClauseRule.java: ########## @@ -0,0 +1,133 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.pinot.calcite.rel.rules; + +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.List; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.logical.LogicalFilter; +import org.apache.calcite.rel.logical.LogicalJoin; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rel.logical.LogicalValues; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.tools.RelBuilderFactory; +import org.apache.pinot.calcite.sql.fun.PinotOperatorTable; + + +/** + * Converts a left-join with literal only right side to a NOT IN clause. + * + * This is required because Calcite compiles NOT IN clause into a left-join, which is not efficient. + * + * Example query plan when reaching this rule: + * LogicalProject(col1=[$0]) + * LogicalFilter(condition=[IS NOT TRUE($3)]) + * LogicalJoin(condition=[=($1, $2)], joinType=[left]) + * LogicalProject(col1=[$0], col20=[$1]) + * LogicalTableScan(table=[[default, a]]) + * LogicalProject(ROW_VALUE=[$0], $f1=[true]) + * LogicalValues(tuples=[[{ _UTF-8'a1' }, { _UTF-8'a2' }, { _UTF-8'a3' }]]) + */ +public class PinotLeftJoinToNotInClauseRule extends RelOptRule { + public static final PinotLeftJoinToNotInClauseRule INSTANCE = + new PinotLeftJoinToNotInClauseRule(PinotRuleUtils.PINOT_REL_FACTORY); + + public PinotLeftJoinToNotInClauseRule(RelBuilderFactory factory) { + super(operand(LogicalProject.class, operand(LogicalFilter.class, + operand(LogicalJoin.class, operand(RelNode.class, any()), + operand(LogicalProject.class, operand(LogicalValues.class, any()))))), factory, null); + } + + @Override + public boolean matches(RelOptRuleCall call) { + // Match LogicalFilter with single IS NOT TRUE condition + LogicalFilter filter = call.rel(1); + RexNode filterCondition = filter.getCondition(); + if (filterCondition.getKind() != SqlKind.IS_NOT_TRUE) { + return false; + } + // Match LogicalJoin with left join type and single EQUALS condition + LogicalJoin join = call.rel(2); + if (join.getJoinType() != JoinRelType.LEFT) { Review Comment: Could we also add INNER here as well? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org