jadami10 commented on code in PR #10444:
URL: https://github.com/apache/pinot/pull/10444#discussion_r1145467030
##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/FlattenAndOrFilterOptimizer.java:
##########
@@ -40,6 +40,9 @@ public Expression optimize(Expression filterExpression,
@Nullable Schema schema)
private Expression optimize(Expression filterExpression) {
Function function = filterExpression.getFunctionCall();
+ if (function == null) {
Review Comment:
both `where true` and `where 1=1` fail here with NPE.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java:
##########
@@ -44,7 +45,7 @@ public class QueryOptimizer {
// values to the proper format so that they can be properly parsed
private static final List<FilterOptimizer> FILTER_OPTIMIZERS =
Arrays.asList(new FlattenAndOrFilterOptimizer(), new
MergeEqInFilterOptimizer(), new NumericalFilterOptimizer(),
- new TimePredicateFilterOptimizer(), new MergeRangeFilterOptimizer());
+ new TimePredicateFilterOptimizer(), new MergeRangeFilterOptimizer(),
new IdenticalPredicateFilterOptimizer());
Review Comment:
good call. it actually caught another null point exception moving it earlier
##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/BaseAndOrBooleanFilterOptimizer.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.sql.FilterKind;
+
+/**
+ * This base class acts as a helper for any optimizer that is effectively
removing filter conditions.
+ * It provides TRUE/FALSE literal classes that can be used to replace filter
expressions that are always true/false.
+ * It provides an optimization implementation for AND/OR/NOT expressions.
+ */
+public abstract class BaseAndOrBooleanFilterOptimizer implements
FilterOptimizer {
+
+ protected static final Expression TRUE =
RequestUtils.getLiteralExpression(true);
Review Comment:
good eye. I was working on a new laptop and hadn't set that up.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/BaseAndOrBooleanFilterOptimizer.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.sql.FilterKind;
+
+/**
+ * This base class acts as a helper for any optimizer that is effectively
removing filter conditions.
+ * It provides TRUE/FALSE literal classes that can be used to replace filter
expressions that are always true/false.
+ * It provides an optimization implementation for AND/OR/NOT expressions.
+ */
+public abstract class BaseAndOrBooleanFilterOptimizer implements
FilterOptimizer {
+
+ protected static final Expression TRUE =
RequestUtils.getLiteralExpression(true);
+ protected static final Expression FALSE =
RequestUtils.getLiteralExpression(false);
+
+ @Override
+ public abstract Expression optimize(Expression filterExpression, @Nullable
Schema schema);
+
+ /**
+ * If any of the operands of AND function is "false", then the AND
function itself is false and can be replaced with
+ * "false" literal. Otherwise, remove all the "true" operands of the AND
function. Similarly, if any of the operands
+ * of OR function is "true", then the OR function itself is true and can
be replaced with "true" literal. Otherwise,
+ * remove all the "false" operands of the OR function.
+ */
+ protected Expression optimizeCurrent(Expression expression) {
+ Function function = expression.getFunctionCall();
+ String operator = function.getOperator();
+ List<Expression> operands = function.getOperands();
+ if (operator.equals(FilterKind.AND.name())) {
+ // If any of the literal operands are always false, then replace
AND function with FALSE.
+ for (Expression operand : operands) {
+ if (isAlwaysFalse(operand)) {
+ return FALSE;
+ }
+ }
+
+ // Remove all Literal operands that are always true.
+ operands.removeIf(this::isAlwaysTrue);
+ if (operands.isEmpty()) {
+ return TRUE;
+ }
+ } else if (operator.equals(FilterKind.OR.name())) {
+ // If any of the literal operands are always true, then replace OR
function with TRUE
+ for (Expression operand : operands) {
+ if (isAlwaysTrue(operand)) {
+ return TRUE;
+ }
+ }
+
+ // Remove all Literal operands that are always false.
+ operands.removeIf(this::isAlwaysFalse);
+ if (operands.isEmpty()) {
+ return FALSE;
+ }
+ } else if (operator.equals(FilterKind.NOT.name())) {
+ assert operands.size() == 1;
+ Expression operand = operands.get(0);
+ if (isAlwaysTrue(operand)) {
+ return FALSE;
+ }
+ if (isAlwaysFalse(operand)) {
+ return TRUE;
+ }
+ }
+ return expression;
+ }
+
+ protected boolean isAlwaysFalse(Expression operand) {
Review Comment:
I've reworked the interface to be a little clearer. The base class handles
the DFS, and users just implement the base case. Let me know if this looks
better.
##########
pinot-controller/src/main/java/org/apache/pinot/controller/recommender/io/InputManager.java:
##########
@@ -170,6 +170,8 @@ private void validateQueries() {
for (String queryString : _queryWeightMap.keySet()) {
try {
PinotQuery pinotQuery =
CalciteSqlParser.compileToPinotQuery(queryString);
+ // TODO: should we catch and ignore any errors here. If we error on
query optimization,
Review Comment:
ya, I was thinking the same about the fact that it might be only semi
optimized when this fails. I've updated the comment to reflect this state
##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/FlattenAndOrFilterOptimizer.java:
##########
@@ -50,7 +53,7 @@ private Expression optimize(Expression filterExpression) {
for (Expression child : children) {
Expression optimizedChild = optimize(child);
Function childFunction = optimizedChild.getFunctionCall();
- if (childFunction.getOperator().equals(operator)) {
+ if (childFunction != null &&
childFunction.getOperator().equals(operator)) {
Review Comment:
It also fails on `where 1=1`, so the only way to eliminate those is to run
some other optimizer first. that seems like it might be more complicated.
Running this one first seems to simplify a lot of things
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]