Copilot commented on code in PR #2670: URL: https://github.com/apache/phoenix/pull/2670#discussion_r4530644673
########## phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/UncoveredIndexDataRowFilter.java: ########## @@ -0,0 +1,411 @@ +/* + * 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.phoenix.coprocessor; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.execute.TupleProjector; +import org.apache.phoenix.expression.AndExpression; +import org.apache.phoenix.expression.ArithmeticExpression; +import org.apache.phoenix.expression.BaseTerminalExpression; +import org.apache.phoenix.expression.CaseExpression; +import org.apache.phoenix.expression.CoerceExpression; +import org.apache.phoenix.expression.ComparisonExpression; +import org.apache.phoenix.expression.Determinism; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.expression.InListExpression; +import org.apache.phoenix.expression.IsNullExpression; +import org.apache.phoenix.expression.LikeExpression; +import org.apache.phoenix.expression.LiteralExpression; +import org.apache.phoenix.expression.NotExpression; +import org.apache.phoenix.expression.OrExpression; +import org.apache.phoenix.expression.ProjectedColumnExpression; +import org.apache.phoenix.expression.function.BsonValueFunction; +import org.apache.phoenix.expression.function.CoalesceFunction; +import org.apache.phoenix.expression.function.ScalarFunction; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.join.HashJoinInfo; +import org.apache.phoenix.schema.KeyValueSchema; +import org.apache.phoenix.schema.SortOrder; +import org.apache.phoenix.schema.ValueSchema.Field; +import org.apache.phoenix.schema.tuple.ResultTuple; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PBoolean; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.util.ByteUtil; +import org.apache.phoenix.util.ScanUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; +import org.apache.phoenix.thirdparty.com.google.common.collect.ImmutableList; + +/** + * An early filter for uncovered index scans. The scanner applies it to each data row before it + * joins the data row to the index row. The filter holds the conjuncts of the post-join WHERE that + * read only data columns. It reads those columns from the data row directly. A rejected row then + * skips the join. The full post-join WHERE still runs on each accepted row, so the filter only + * removes rows that the post-join WHERE also removes. + */ +public final class UncoveredIndexDataRowFilter { + private static final Logger LOGGER = LoggerFactory.getLogger(UncoveredIndexDataRowFilter.class); + + /** + * The bit set of a value schema has a different format above this count of nullable fields. + */ + private static final int MAX_SHORT_BIT_SET_FIELDS = 16; + + private final Expression expression; + private final ImmutableBytesWritable ptr = new ImmutableBytesWritable(); + + @VisibleForTesting + UncoveredIndexDataRowFilter(Expression expression) { + this.expression = expression; + } + + /** + * Builds the early filter from the post-join WHERE of an uncovered index scan. + * @param scan the index scan + * @param extraWhere the post-join WHERE, or null + * @param tupleProjector the projector that builds the joined row from the data row, or null + * @return the filter, or null when no conjunct is safe to apply early + */ + public static UncoveredIndexDataRowFilter create(Scan scan, Expression extraWhere, + TupleProjector tupleProjector) { + // Analyze scans skip the post-join WHERE. A hash join reads the joined row in a different + // layout. In these cases the filter must not change the rows. + if ( + extraWhere == null || tupleProjector == null || ScanUtil.isAnalyzeTable(scan) + || HashJoinInfo.hasHashJoin(scan) + ) { + return null; + } + List<Expression> pushed = new ArrayList<>(); + for (Expression conjunct : getConjuncts(extraWhere)) { + Expression rewritten = rewriteConjunct(conjunct, tupleProjector); + if (rewritten != null) { + pushed.add(rewritten); + } Review Comment: Skipping unrewritable conjuncts can hide errors: for `10 / TO_NUMBER(IX) = 1 AND I = 5`, `IX='0', I=6` is discarded instead of raising division by zero. Rejecting a NULL pushed conjunct can also hide an error later in the original AND, which does not short-circuit on NULL. Conservatively disable the early filter if any conjunct cannot be rewritten, and add regressions for both orderings. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
