siddharthteotia commented on a change in pull request #5774: URL: https://github.com/apache/incubator-pinot/pull/5774#discussion_r465794494
########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/recommender/rules/RulesToExecute.java ########## @@ -0,0 +1,140 @@ +/** + * 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; + +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.annotation.Nulls; +import org.apache.pinot.controller.recommender.io.ConfigManager; +import org.apache.pinot.controller.recommender.io.InputManager; +import org.apache.pinot.controller.recommender.rules.impl.BloomFilterRule; +import org.apache.pinot.controller.recommender.rules.impl.FlagQueryRule; +import org.apache.pinot.controller.recommender.rules.impl.InvertedSortedIndexJointRule; +import org.apache.pinot.controller.recommender.rules.impl.KafkaPartitionRule; +import org.apache.pinot.controller.recommender.rules.impl.NoDictionaryOnHeapDictionaryJointRule; +import org.apache.pinot.controller.recommender.rules.impl.PinotTablePartitionRule; +import org.apache.pinot.controller.recommender.rules.impl.VariedLengthDictionaryRule; + +import static org.apache.pinot.controller.recommender.rules.io.params.RecommenderConstants.RulesToExecute.*; + + +public class RulesToExecute { + public static class RuleFactory { + public static AbstractRule getRule(Rules rule, InputManager inputManager, ConfigManager outputManager) { + switch (rule) { + case FlagQueryRule: + return new FlagQueryRule(inputManager, outputManager); + case InvertedSortedIndexJointRule: + return new InvertedSortedIndexJointRule(inputManager, outputManager); + case KafkaPartitionRule: + return new KafkaPartitionRule(inputManager, outputManager); + case PinotTablePartitionRule: + return new PinotTablePartitionRule(inputManager, outputManager); + case BloomFilterRule: + return new BloomFilterRule(inputManager, outputManager); + case NoDictionaryOnHeapDictionaryJointRule: + return new NoDictionaryOnHeapDictionaryJointRule(inputManager, outputManager); + case VariedLengthDictionaryRule: + return new VariedLengthDictionaryRule(inputManager, outputManager); + default: + return null; + } + } + } + // All rules will execute by default unless explicitly specifying "recommendInvertedSortedIndexJoint" = "false" + boolean _recommendKafkaPartition = DEFAULT_RECOMMEND_KAFKA_PARTITION; + boolean _recommendPinotTablePartition = DEFAULT_RECOMMEND_PINOT_TABLE_PARTITION; + boolean _recommendInvertedSortedIndexJoint = DEFAULT_RECOMMEND_INVERTED_SORTED_INDEX_JOINT; + boolean _recommendBloomFilter = DEFAULT_RECOMMEND_BLOOM_FILTER; + boolean _recommendNoDictionaryOnHeapDictionaryJoint = DEFAULT_RECOMMEND_NO_DICTIONARY_ONHEAP_DICTIONARY_JOINT; + boolean _recommendVariedLengthDictionary = DEFAULT_RECOMMEND_VARIED_LENGTH_DICTIONARY; + boolean _recommendFlagQuery = DEFAULT_RECOMMEND_FLAG_QUERY; + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendVariedLengthDictionary(boolean recommendVariedLengthDictionary) { + _recommendVariedLengthDictionary = recommendVariedLengthDictionary; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendFlagQuery(boolean recommendFlagQuery) { + _recommendFlagQuery = recommendFlagQuery; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendNoDictionaryOnHeapDictionaryJoint(boolean recommendNoDictionaryOnHeapDictionaryJoint) { + _recommendNoDictionaryOnHeapDictionaryJoint = recommendNoDictionaryOnHeapDictionaryJoint; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendKafkaPartition(boolean recommendKafkaPartition) { + _recommendKafkaPartition = recommendKafkaPartition; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendInvertedSortedIndexJoint(boolean recommendInvertedSortedIndexJoint) { + _recommendInvertedSortedIndexJoint = recommendInvertedSortedIndexJoint; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendPinotTablePartition(boolean recommendPinotTablePartition) { + _recommendPinotTablePartition = recommendPinotTablePartition; + } + + @JsonSetter(nulls = Nulls.SKIP) + public void setRecommendBloomFilter(boolean recommendBloomFilter) { + _recommendBloomFilter = recommendBloomFilter; + } + + public boolean isRecommendVariedLengthDictionary() { + return _recommendVariedLengthDictionary; + } + + public boolean isRecommendFlagQuery() { + return _recommendFlagQuery; + } + + public boolean isRecommendNoDictionaryOnHeapDictionaryJoint() { + return _recommendNoDictionaryOnHeapDictionaryJoint; + } + + public boolean isRecommendKafkaPartition() { + return _recommendKafkaPartition; + } + + public boolean isRecommendInvertedSortedIndexJoint() { + return _recommendInvertedSortedIndexJoint; + } + + public boolean isRecommendPinotTablePartition() { + return _recommendPinotTablePartition; + } + + public boolean isRecommendBloomFilter() { + return _recommendBloomFilter; + } + + public enum Rules { Review comment: The enum name shouldn't really have "plural". The enum although defines multiple constants, it represents only 1 of them at a give time. So, we should simply call it Rule? ########## 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 { Review comment: Please add javadoc and brief explanation of the rule's algorithm. We already have that in the design, so just englishize it here :) ########## 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: Mark this is as a TODO since it can be easily spotted in the code if someone stumbles upon it in the future. TODO: once Pinot starts supporting bloom filter based pruning for IN, !=, NOT IN, we should enhance the algorithm of this rule. ---------------------------------------------------------------- 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