Jackie-Jiang commented on a change in pull request #6811: URL: https://github.com/apache/incubator-pinot/pull/6811#discussion_r620787772
########## 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()))) { Review comment: Only optimize children for `AND` and `OR`, no need to do recursion for other operators ########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java ########## @@ -428,6 +438,25 @@ public BrokerResponse handleRequest(JsonNode request, @Nullable RequesterIdentit _brokerMetrics.addMeteredTableValue(rawTableName, BrokerMeter.BROKER_RESPONSES_WITH_NUM_GROUPS_LIMIT_REACHED, 1); } + logBrokerResponse(requestStatistics, requestId, query, compilationStartTimeNs, brokerRequest, + numUnavailableSegments, serverStats, brokerResponse, executionEndTimeNs); + return brokerResponse; + } + + /** + * Given a {@link BrokerRequest}, this function will determine if we can return a response without server-side query + * evaluation. This happens when the optimizer determines that the entire WHERE clause evaluates to false. + */ + private boolean isResponsePossible(BrokerRequest brokerRequest) { Review comment: Suggest renaming to `isAlwaysFalse` ########## File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java ########## @@ -31,12 +31,14 @@ import org.apache.pinot.core.query.optimizer.filter.FlattenAndOrFilterOptimizer; import org.apache.pinot.core.query.optimizer.filter.MergeEqInFilterOptimizer; import org.apache.pinot.core.query.optimizer.filter.MergeRangeFilterOptimizer; +import org.apache.pinot.core.query.optimizer.filter.NumericalFilterOptimizer; import org.apache.pinot.spi.data.Schema; public class QueryOptimizer { private static final List<FilterOptimizer> FILTER_OPTIMIZERS = Arrays - .asList(new FlattenAndOrFilterOptimizer(), new MergeEqInFilterOptimizer(), new MergeRangeFilterOptimizer()); + .asList(new FlattenAndOrFilterOptimizer(), new MergeEqInFilterOptimizer(), new MergeRangeFilterOptimizer(), + new NumericalFilterOptimizer()); Review comment: It is not about performance, but correctness. Currently `MergeRangeFilterOptimizer` assumes the numeric format is correct. If we put `NumericalFilterOptimizer` before `MergeRangeFilterOptimizer`, `int_col > 1.5 AND int_col < 2.5` will work. ########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java ########## @@ -428,6 +438,25 @@ public BrokerResponse handleRequest(JsonNode request, @Nullable RequesterIdentit _brokerMetrics.addMeteredTableValue(rawTableName, BrokerMeter.BROKER_RESPONSES_WITH_NUM_GROUPS_LIMIT_REACHED, 1); } + logBrokerResponse(requestStatistics, requestId, query, compilationStartTimeNs, brokerRequest, + numUnavailableSegments, serverStats, brokerResponse, executionEndTimeNs); + return brokerResponse; + } + + /** + * Given a {@link BrokerRequest}, this function will determine if we can return a response without server-side query + * evaluation. This happens when the optimizer determines that the entire WHERE clause evaluates to false. + */ + private boolean isResponsePossible(BrokerRequest brokerRequest) { + return brokerRequest == null || brokerRequest.getPinotQuery() == null || ( Review comment: (Critical) `brokerRequest.getPinotQuery()` will always be `null` for PQL query ########## File path: pinot-common/src/main/java/org/apache/pinot/common/utils/request/RequestUtils.java ########## @@ -151,7 +157,10 @@ public static Expression getLiteralExpression(byte[] value) { } public static Expression getLiteralExpression(Object object) { - if (object instanceof Integer || object instanceof Long) { + if (object instanceof Integer) { Review comment: I don't quite follow here. The test failure should not be related to this change but the new extra `NumericalFilterOptimizer`. It is okay to fix the test, but we should not modify the production code because of how tests are written. ########## 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); Review comment: We might want to simply return `null` when filter evaluates to `true` to denote that the filter can be dropped ########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java ########## @@ -345,6 +346,15 @@ public BrokerResponse handleRequest(JsonNode request, @Nullable RequesterIdentit requestStatistics.setFanoutType(RequestStatistics.FanoutType.REALTIME); } + // Check if response can be send without server query evaluation. + if (isResponsePossible(offlineBrokerRequest) && isResponsePossible(realtimeBrokerRequest)) { Review comment: Set `offlineBrokerRequest` or `realtimeBrokerRequest` to `null` if only one of them is always false -- 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