jasperjiaguo commented on a change in pull request #5774:
URL: https://github.com/apache/incubator-pinot/pull/5774#discussion_r466079072



##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/recommender/rules/impl/BloomFilterRule.java
##########
@@ -0,0 +1,142 @@
+/**
+ * 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.controller.recommender.rules.impl;
+
+import com.google.common.util.concurrent.AtomicDouble;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.controller.recommender.rules.utils.FixedLenBitset;
+import org.apache.pinot.core.query.request.context.ExpressionContext;
+import org.apache.pinot.core.query.request.context.FilterContext;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import org.apache.pinot.core.query.request.context.predicate.Predicate;
+import 
org.apache.pinot.core.query.request.context.utils.BrokerRequestToQueryContextConverter;
+import org.apache.pinot.core.requesthandler.BrokerRequestOptimizer;
+import org.apache.pinot.core.requesthandler.PinotQueryParserFactory;
+import org.apache.pinot.parsers.AbstractCompiler;
+import org.apache.pinot.sql.parsers.SqlCompilationException;
+import org.apache.pinot.controller.recommender.io.ConfigManager;
+import org.apache.pinot.controller.recommender.io.InputManager;
+import org.apache.pinot.controller.recommender.rules.AbstractRule;
+import 
org.apache.pinot.controller.recommender.rules.io.params.BloomFilterRuleParams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class  BloomFilterRule extends AbstractRule {
+  private final Logger LOGGER = LoggerFactory.getLogger(BloomFilterRule.class);
+  private final BloomFilterRuleParams _params;
+  protected final BrokerRequestOptimizer _brokerRequestOptimizer = new 
BrokerRequestOptimizer();
+
+  public BloomFilterRule(InputManager inputManager, ConfigManager 
outputManager) {
+    super(inputManager, outputManager);
+    _params = inputManager.getBloomFilterRuleParams();
+  }
+
+  @Override
+  public void run() {
+    int numDims = _inputManager.getNumDims();
+    double[] weights = new double[numDims];
+    AtomicDouble totalWeight = new AtomicDouble(0);
+
+    // For each query, find out the dimensions used in 'EQ'
+    // and accumulate the (weighted) frequencies
+    _inputManager.getQueryWeightMap().forEach((query,weight) -> {
+      totalWeight.addAndGet(weight);
+      FixedLenBitset fixedLenBitset = parseQuery(query);
+      LOGGER.debug("fixedLenBitset {}", fixedLenBitset);
+      for (Integer i : fixedLenBitset.getOffsets()) {
+        weights[i] += weight;
+      }
+    });
+    LOGGER.debug("Weight: {}, Total {}", weights, totalWeight);
+
+    for (int i = 0; i < numDims; i++) {
+      String dimName = _inputManager.intToColName(i);
+      if (((weights[i] / totalWeight.get()) > 
_params.THRESHOLD_MIN_PERCENT_EQ_BLOOMFILTER)
+          //The partitioned dimension should be frequently > P used
+          && (_inputManager.getCardinality(dimName)
+          < _params.THRESHOLD_MAX_CARDINALITY_BLOOMFILTER)) { //The 
Cardinality < C (1 million for 1MB size)
+        _outputManager.getIndexConfig().getBloomFilterColumns().add(dimName);
+      }
+    }
+  }
+
+  public FixedLenBitset parseQuery(String queryString) {
+    LOGGER.debug("Parsing query: {}", queryString);
+    if (queryString == null) {
+      return FixedLenBitset.IMMUTABLE_EMPTY_SET;
+    }
+
+    BrokerRequest brokerRequest;
+    AbstractCompiler parser = 
PinotQueryParserFactory.get(_inputManager.getQueryType());
+    try {
+      brokerRequest = parser.compileToBrokerRequest(queryString);
+    } catch (SqlCompilationException e) {
+      LOGGER.error("Error parsing query: {}, {}", queryString, e.getMessage());
+      return FixedLenBitset.IMMUTABLE_EMPTY_SET;
+    }
+    BrokerRequest optimizedRequest = 
_brokerRequestOptimizer.optimize(brokerRequest, _inputManager.getTimeCol());
+    QueryContext queryContext = 
BrokerRequestToQueryContextConverter.convert(optimizedRequest);
+
+    if (queryContext.getFilter() == null) {
+      return FixedLenBitset.IMMUTABLE_EMPTY_SET;
+    }
+
+    LOGGER.trace("Parsing Where Clause: {}", 
queryContext.getFilter().toString());
+    return parsePredicateList(queryContext.getFilter());
+  }
+
+  /**
+   * The partitioned dimension should used in the “=” (IN, NOT IN, != are not 
using bloom filter in Pinot for now) filter.
+   * @param filterContext filterContext

Review comment:
       done




----------------------------------------------------------------
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

Reply via email to