Jackie-Jiang commented on a change in pull request #6811: URL: https://github.com/apache/incubator-pinot/pull/6811#discussion_r620794795
########## File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/NumericalFilterOptimizer.java ########## @@ -0,0 +1,273 @@ +/** + * 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.core.query.optimizer.filter; + +import java.math.BigDecimal; +import java.util.List; +import javax.annotation.Nullable; +import org.apache.pinot.common.request.Expression; +import org.apache.pinot.common.request.ExpressionType; +import org.apache.pinot.common.request.Function; +import org.apache.pinot.common.request.Literal; +import org.apache.pinot.common.utils.request.FilterQueryTree; +import org.apache.pinot.common.utils.request.RequestUtils; +import org.apache.pinot.pql.parsers.pql2.ast.FilterKind; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.Schema; + + +/** + * Numerical expressions of form "column = literal" or "column != literal" can compare a column of one datatype + * (say INT) with a literal of different datatype (say DOUBLE). These expressions can not be evaluated on the Server. + * Hence, we rewrite such expressions into an equivalent expression whose LHS and RHS are of the same datatype. + * + * Simple predicate examples: + * 1) WHERE "intColumn = 5.0" gets rewritten to "WHERE intColumn = 5" + * 2) WHERE "intColumn != 5.0" gets rewritten to "WHERE intColumn != 5" + * 3) WHERE "intColumn = 5.5" gets rewritten to "WHERE false" because INT values can not match 5.5. + * 4) WHERE "intColumn = 3000000000 gets rewritten to "WHERE false" because INT values can not match 3000000000. + * 5) WHERE "intColumn != 3000000000 gets rewritten to "WHERE true" becuase INT values always not equal to 3000000000. + * + * Compound predicate examples: + * 6) WHERE "intColumn1 = 5.5 AND intColumn2 = intColumn3" + * rewrite to "WHERE false AND intColumn2 = intColumn3" + * rewrite to "WHERE intColumn2 = intColumn3" + * 7) WHERE "intColumn1 != 5.5 OR intColumn2 = 5000000000" (5000000000 is out of bounds for integer column) + * rewrite to "WHERE true OR false" + * rewrite to "WHERE true" + * rewrite to query without any WHERE clause. + * + * When entire predicate gets rewritten to false (Example 3 above), the query will not return any data. Hence, it is + * better for the Broker itself to return an empty response rather than sending the query to servers for further + * evaluation. + */ +public class NumericalFilterOptimizer implements FilterOptimizer { + + private static final Expression TRUE = RequestUtils.getLiteralExpression(true); + private static final Expression FALSE = RequestUtils.getLiteralExpression(false); + + @Override + public FilterQueryTree optimize(FilterQueryTree filterQueryTree, @Nullable Schema schema) { + // Don't do anything here since this is for PQL queries which we no longer support. + return filterQueryTree; + } + + @Override + public Expression optimize(Expression expression, @Nullable Schema schema) { + ExpressionType type = expression.getType(); + if (type != ExpressionType.FUNCTION) { + // Not a function, so we have nothing to rewrite. + return expression; + } + + Function function = expression.getFunctionCall(); + List<Expression> operands = function.getOperands(); + String operator = function.getOperator(); + if (!(operator.equals(FilterKind.EQUALS.name()) || operator.equals(FilterKind.NOT_EQUALS.name()))) { + // This is not an EQUALS or NOT_EQUALS function, but one of its operands may be an EQUALS function so + // recursively traverse the expression tree to see if we find an EQUALS function to rewrite. + operands.forEach(operand -> optimize(operand, schema)); + + // We have rewritten the child operands, so rewrite the parent if needed. + return optimizeCurrent(expression); + } + + // If we are here, then this expression must have EQUALS operator. Verify that LHS is a numeric column and RHS is + // a numeric literal before proceeding further. + Expression lhs = operands.get(0), rhs = operands.get(1); + if (!(isNumericColumn(lhs, schema) && isNumericLiteral(rhs))) { Review comment: We should always convert integral rhs `DOUBLE` value to `LONG` even if lhs is an expression -- 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. 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