amrishlal commented on a change in pull request #6811: URL: https://github.com/apache/incubator-pinot/pull/6811#discussion_r620895878
########## 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: Renamed to `isFilterAlwaysFalse` and also added 'isFilterAlwaysTrue' to take care of all cases including setting `offlineBrokerRequest` or `realtimeBrokerRequest` to null if needed and dropping filter if filter will always evaluate to TRUE. ########## 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: Renamed to `isFilterAlwaysFalse` and also added 'isFilterAlwaysTrue' to take care of all cases including setting `offlineBrokerRequest` or `realtimeBrokerRequest` to null if needed and dropping filter if filter will always evaluate to TRUE. ########## 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: Added check for making sure we are doing this only for SQL queries. ########## 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: `null` often gets very overloaded in Java with all sorts of meaning in different contexts, so I prefer to avoid it :-) TRUE/FALSE usage here seems to convey the meaning behind predicate evaluation results quite well. ########## 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: Fixed. ########## 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: As we discussed offline earlier, for now we are only rewriting when LHS is column and RHS is a literal as there is quite a bit of complexity involved in determining LHS datatype when LHS is an generic expression. It should be possible, but its probably better to get simpler cases out of the way before stepping into more complex cases. -- 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