You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2020/09/24 06:11:38 UTC

[GitHub] [incubator-pinot] Jackie-Jiang opened a new pull request #6056: Add FilterOptimizer which supports optimizing both PQL and SQL query filter

Jackie-Jiang opened a new pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056


   ## Description
   Add `FilterOptimizer` which supports optimizing both PQL and SQL query filter.
   `FilterOptimizer` will replace `FilterQueryTreeOptimizer` which only works on PQL query filter
   In order to fully support SQL (#4219), the query optimizer should perform the same optimization to SQL queries as PQL queries.
   
   Add `FlattenAndOrFilterOptimizer` to replace `FlattenNestedPredicatesFilterQueryTreeOptimizer`, and removes the limitation of flatten depth
   Add `MergeEqInFilterOptimizer` to replace `MultipleOrEqualitiesToInClauseFilterQueryTreeOptimizer`
   Add `MergeRangeFilterOptimizer` to replace `RangeMergeOptimizer`, and supports merging range for all single-value columns (based on schema)
   
   This PR only adds the new code. The following PR will wire the new code and remove the old code.


----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] Jackie-Jiang commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496333858



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/FlattenAndOrFilterOptimizer.java
##########
@@ -0,0 +1,88 @@
+/**
+ * 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.ArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterOperator;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.utils.request.FilterQueryTree;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * The {@code FlattenAndOrFilterOptimizer} flattens the nested AND/OR filters. For example, AND(a, AND(b, c)) can
+ * be flattened to AND(a, b, c).
+ */
+public class FlattenAndOrFilterOptimizer implements FilterOptimizer {

Review comment:
       The method signature also changed (new interface introduced, `FilterQueryOptimizerRequest` no longer needed). IMO, trying to enhance the existing one will make it very hard to review (will also probably end up with the same code).
   Another benefit of adding new code instead of enhancing the existing one is to break the change into multiple smaller PRs for easier review. The first one only adds the new implementation and the tests, and the next one wire it in.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] siddharthteotia commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496310303



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/FlattenAndOrFilterOptimizer.java
##########
@@ -0,0 +1,88 @@
+/**
+ * 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.ArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterOperator;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.utils.request.FilterQueryTree;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * The {@code FlattenAndOrFilterOptimizer} flattens the nested AND/OR filters. For example, AND(a, AND(b, c)) can
+ * be flattened to AND(a, b, c).
+ */
+public class FlattenAndOrFilterOptimizer implements FilterOptimizer {

Review comment:
       Can we enhance the existing one instead of deleting it? I believe everything is same except for removing flatten depth restriction and optimize interface for FilterExpression




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] siddharthteotia commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496309019



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+import java.util.Arrays;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterQuery;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.core.query.optimizer.filter.FilterOptimizer;
+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.spi.data.Schema;
+
+
+public class QueryOptimizer {
+  private static final List<FilterOptimizer> FILTER_OPTIMIZERS = Arrays
+      .asList(new FlattenAndOrFilterOptimizer(), new MergeEqInFilterOptimizer(), new MergeRangeFilterOptimizer());
+
+  /**
+   * Optimizes the given PQL query.
+   */
+  public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) {
+    FilterQuery filterQuery = brokerRequest.getFilterQuery();
+    if (filterQuery != null) {
+      FilterQueryTree filterQueryTree =
+          RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap());
+      for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+        filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema);
+      }
+      RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest);
+    }
+  }
+
+  /**
+   * Optimizes the given SQL query.
+   */
+  public void optimize(PinotQuery pinotQuery, @Nullable Schema schema) {

Review comment:
       Why PinotQuery based signature for SQL? That is the end-state after getting rid of BrokerRequest right?
   
   But for now, the optimizer will get BrokerRequest for both PQL (through PQL compiler) and for SQL (through calcite compiler -> PinotQuery -> converter -> BrokerRequest). The javadoc seems misleading




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] siddharthteotia commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496309019



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+import java.util.Arrays;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterQuery;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.core.query.optimizer.filter.FilterOptimizer;
+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.spi.data.Schema;
+
+
+public class QueryOptimizer {
+  private static final List<FilterOptimizer> FILTER_OPTIMIZERS = Arrays
+      .asList(new FlattenAndOrFilterOptimizer(), new MergeEqInFilterOptimizer(), new MergeRangeFilterOptimizer());
+
+  /**
+   * Optimizes the given PQL query.
+   */
+  public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) {
+    FilterQuery filterQuery = brokerRequest.getFilterQuery();
+    if (filterQuery != null) {
+      FilterQueryTree filterQueryTree =
+          RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap());
+      for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+        filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema);
+      }
+      RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest);
+    }
+  }
+
+  /**
+   * Optimizes the given SQL query.
+   */
+  public void optimize(PinotQuery pinotQuery, @Nullable Schema schema) {

Review comment:
       Why PinotQuery based signature for SQL? That is the end-state after getting rid of BrokerRequest right?
   
   But for now, the optimizer will get BrokerRequest for both PQL (through PQL compiler) and for SQL (through calcite compiler -> PinotQuery -> converter -> BrokerRequest). 




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] Jackie-Jiang commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496328436



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+import java.util.Arrays;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterQuery;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.core.query.optimizer.filter.FilterOptimizer;
+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.spi.data.Schema;
+
+
+public class QueryOptimizer {
+  private static final List<FilterOptimizer> FILTER_OPTIMIZERS = Arrays
+      .asList(new FlattenAndOrFilterOptimizer(), new MergeEqInFilterOptimizer(), new MergeRangeFilterOptimizer());
+
+  /**
+   * Optimizes the given PQL query.
+   */
+  public void optimize(BrokerRequest brokerRequest, @Nullable Schema schema) {
+    FilterQuery filterQuery = brokerRequest.getFilterQuery();
+    if (filterQuery != null) {
+      FilterQueryTree filterQueryTree =
+          RequestUtils.buildFilterQuery(filterQuery.getId(), brokerRequest.getFilterSubQueryMap().getFilterQueryMap());
+      for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+        filterQueryTree = filterOptimizer.optimize(filterQueryTree, schema);
+      }
+      RequestUtils.generateFilterFromTree(filterQueryTree, brokerRequest);
+    }
+  }
+
+  /**
+   * Optimizes the given SQL query.
+   */
+  public void optimize(PinotQuery pinotQuery, @Nullable Schema schema) {

Review comment:
       The purpose for this PR is to decouple the SQL and PQL query logic, and we only use the `PinotQuery` for SQL query, and only use the `BrokerRequest` for PQL query.
   To optimize a SQL query, we should only call `optimize` with `PinotQuery`, but not with `BrokerRequest`.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] Jackie-Jiang merged pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang merged pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056


   


----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [incubator-pinot] siddharthteotia commented on a change in pull request #6056: Add FilterOptimizer which supports optimizing query filter from both BrokerRequest and PinotQuery

Posted by GitBox <gi...@apache.org>.
siddharthteotia commented on a change in pull request #6056:
URL: https://github.com/apache/incubator-pinot/pull/6056#discussion_r496310465



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/MergeEqInFilterOptimizer.java
##########
@@ -0,0 +1,263 @@
+/**
+ * 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.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.FilterOperator;
+import org.apache.pinot.common.request.Function;
+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.Schema;
+
+
+/**
+ * The {@code MergeEqualInFilterOptimizer} merges EQ and IN predicates on the same column joined by OR, and performs the

Review comment:
       Same as before. We should try to enhance the same code. 




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org