kishoreg commented on a change in pull request #6957:
URL: https://github.com/apache/incubator-pinot/pull/6957#discussion_r636652258
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/MergeEqInFilterOptimizer.java
##########
@@ -151,7 +150,7 @@ private static FilterQueryTree getFilterQueryTree(String
column, Set<String> val
@Override
public Expression optimize(Expression filterExpression, @Nullable Schema
schema) {
- return filterExpression.getType() == ExpressionType.FUNCTION ?
optimize(filterExpression) : filterExpression;
+ return optimize(filterExpression);
Review comment:
revert this
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/MergeRangeFilterOptimizer.java
##########
@@ -114,26 +112,6 @@ public FilterQueryTree optimize(FilterQueryTree
filterQueryTree, @Nullable Schem
}
}
- /**
Review comment:
why was this removed?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/utils/Range.java
##########
@@ -0,0 +1,119 @@
+/**
+ * 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.utils;
Review comment:
why under utils?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -59,11 +62,13 @@ public void optimize(BrokerRequest brokerRequest, @Nullable
Schema schema) {
*/
public void optimize(PinotQuery pinotQuery, @Nullable Schema schema) {
Expression filterExpression = pinotQuery.getFilterExpression();
- if (filterExpression != null) {
- for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+ for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+ if (filterExpression != null && filterExpression.getType() ==
ExpressionType.FUNCTION) {
Review comment:
I see that you moved the logic from each optimizer which was checking if
its a function one level up.. while its a good optimization, I don't think its
a good design, lets say we add another optimizer that does not care, then
someone has to change all the existing optimizers.
My suggestion is to let each optimizer decide if it can optimize the
filterExpression or have a method on the optimizer to check if its only
applicable to function expression
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java
##########
@@ -0,0 +1,330 @@
+/**
+ * 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+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.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import
org.apache.pinot.core.operator.transform.function.DateTimeConversionTransformFunction;
+import
org.apache.pinot.core.operator.transform.function.TimeConversionTransformFunction;
+import org.apache.pinot.core.query.optimizer.filter.utils.Range;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.DateTimeFieldSpec.TimeFormat;
+import org.apache.pinot.spi.data.DateTimeFormatSpec;
+import org.apache.pinot.spi.data.DateTimeGranularitySpec;
+import org.apache.pinot.spi.data.Schema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code TimePredicateFilterOptimizer} optimizes the time related
predicates:
+ * <ul>
+ * <li>
+ * Optimizes TIME_CONVERT/DATE_TIME_CONVERT function with range/equality
predicate to directly apply the predicate
+ * to the inner expression.
+ * </li>
+ * </ul>
+ *
+ * NOTE: This optimizer is followed by the {@link MergeRangeFilterOptimizer},
which can merge the generated ranges.
+ */
+public class TimePredicateFilterOptimizer implements FilterOptimizer {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TimePredicateFilterOptimizer.class);
+
+ @Override
+ public FilterQueryTree optimize(FilterQueryTree filterQueryTree, @Nullable
Schema schema) {
+ // Do not rewrite PQL queries because PQL is deprecated
+ return filterQueryTree;
+ }
+
+ @Override
+ public Expression optimize(Expression filterExpression, @Nullable Schema
schema) {
+ return optimize(filterExpression);
+ }
+
+ @VisibleForTesting
+ Expression optimize(Expression filterExpression) {
+ Function filterFunction = filterExpression.getFunctionCall();
+ FilterKind filterKind = FilterKind.valueOf(filterFunction.getOperator());
+ List<Expression> operands = filterFunction.getOperands();
+ if (filterKind == FilterKind.AND || filterKind == FilterKind.OR) {
+ operands.replaceAll(this::optimize);
+ } else if (filterKind.isRange() || filterKind == FilterKind.EQUALS) {
+ Expression expression = operands.get(0);
+ if (expression.getType() == ExpressionType.FUNCTION) {
+ Function expressionFunction = expression.getFunctionCall();
+ String functionName =
StringUtils.remove(expressionFunction.getOperator(), '_');
+ if
(functionName.equalsIgnoreCase(TimeConversionTransformFunction.FUNCTION_NAME)) {
+ optimizeTimeConvert(filterFunction, filterKind);
+ } else if
(functionName.equalsIgnoreCase(DateTimeConversionTransformFunction.FUNCTION_NAME))
{
+ optimizeDateTimeConvert(filterFunction, filterKind);
+ }
+ }
+ }
+ return filterExpression;
+ }
+
+ /**
+ * Helper method to optimize TIME_CONVERT function with range/equality
predicate to directly apply the predicate to
+ * the inner expression. Changes are applied in-place of the filter function.
+ */
+ private void optimizeTimeConvert(Function filterFunction, FilterKind
filterKind) {
+ List<Expression> filterOperands = filterFunction.getOperands();
+ List<Expression> timeConvertOperands =
filterOperands.get(0).getFunctionCall().getOperands();
+ Preconditions.checkArgument(timeConvertOperands.size() == 3,
Review comment:
this should be done somewhere else right?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -59,11 +62,13 @@ public void optimize(BrokerRequest brokerRequest, @Nullable
Schema schema) {
*/
public void optimize(PinotQuery pinotQuery, @Nullable Schema schema) {
Expression filterExpression = pinotQuery.getFilterExpression();
- if (filterExpression != null) {
- for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+ for (FilterOptimizer filterOptimizer : FILTER_OPTIMIZERS) {
+ if (filterExpression != null && filterExpression.getType() ==
ExpressionType.FUNCTION) {
Review comment:
this is changing the existing behavior right? looks like it optimizes
only the filterExpression is a function?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java
##########
@@ -0,0 +1,330 @@
+/**
+ * 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+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.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import
org.apache.pinot.core.operator.transform.function.DateTimeConversionTransformFunction;
+import
org.apache.pinot.core.operator.transform.function.TimeConversionTransformFunction;
+import org.apache.pinot.core.query.optimizer.filter.utils.Range;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.DateTimeFieldSpec.TimeFormat;
+import org.apache.pinot.spi.data.DateTimeFormatSpec;
+import org.apache.pinot.spi.data.DateTimeGranularitySpec;
+import org.apache.pinot.spi.data.Schema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code TimePredicateFilterOptimizer} optimizes the time related
predicates:
+ * <ul>
+ * <li>
+ * Optimizes TIME_CONVERT/DATE_TIME_CONVERT function with range/equality
predicate to directly apply the predicate
+ * to the inner expression.
+ * </li>
+ * </ul>
+ *
+ * NOTE: This optimizer is followed by the {@link MergeRangeFilterOptimizer},
which can merge the generated ranges.
Review comment:
can you add some examples?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/QueryOptimizer.java
##########
@@ -32,12 +33,14 @@
import org.apache.pinot.core.query.optimizer.filter.MergeEqInFilterOptimizer;
import org.apache.pinot.core.query.optimizer.filter.MergeRangeFilterOptimizer;
import org.apache.pinot.core.query.optimizer.filter.NumericalFilterOptimizer;
+import
org.apache.pinot.core.query.optimizer.filter.TimePredicateFilterOptimizer;
import org.apache.pinot.spi.data.Schema;
public class QueryOptimizer {
private static final List<FilterOptimizer> FILTER_OPTIMIZERS = Arrays
- .asList(new FlattenAndOrFilterOptimizer(), new
NumericalFilterOptimizer(), new MergeEqInFilterOptimizer(), new
MergeRangeFilterOptimizer());
+ .asList(new FlattenAndOrFilterOptimizer(), new
MergeEqInFilterOptimizer(), new NumericalFilterOptimizer(),
Review comment:
does the order matter?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java
##########
@@ -0,0 +1,330 @@
+/**
+ * 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+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.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import
org.apache.pinot.core.operator.transform.function.DateTimeConversionTransformFunction;
+import
org.apache.pinot.core.operator.transform.function.TimeConversionTransformFunction;
+import org.apache.pinot.core.query.optimizer.filter.utils.Range;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.DateTimeFieldSpec.TimeFormat;
+import org.apache.pinot.spi.data.DateTimeFormatSpec;
+import org.apache.pinot.spi.data.DateTimeGranularitySpec;
+import org.apache.pinot.spi.data.Schema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code TimePredicateFilterOptimizer} optimizes the time related
predicates:
+ * <ul>
+ * <li>
+ * Optimizes TIME_CONVERT/DATE_TIME_CONVERT function with range/equality
predicate to directly apply the predicate
+ * to the inner expression.
+ * </li>
+ * </ul>
+ *
+ * NOTE: This optimizer is followed by the {@link MergeRangeFilterOptimizer},
which can merge the generated ranges.
+ */
+public class TimePredicateFilterOptimizer implements FilterOptimizer {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TimePredicateFilterOptimizer.class);
+
+ @Override
+ public FilterQueryTree optimize(FilterQueryTree filterQueryTree, @Nullable
Schema schema) {
+ // Do not rewrite PQL queries because PQL is deprecated
+ return filterQueryTree;
+ }
+
+ @Override
+ public Expression optimize(Expression filterExpression, @Nullable Schema
schema) {
+ return optimize(filterExpression);
+ }
+
+ @VisibleForTesting
+ Expression optimize(Expression filterExpression) {
+ Function filterFunction = filterExpression.getFunctionCall();
+ FilterKind filterKind = FilterKind.valueOf(filterFunction.getOperator());
+ List<Expression> operands = filterFunction.getOperands();
+ if (filterKind == FilterKind.AND || filterKind == FilterKind.OR) {
+ operands.replaceAll(this::optimize);
+ } else if (filterKind.isRange() || filterKind == FilterKind.EQUALS) {
+ Expression expression = operands.get(0);
+ if (expression.getType() == ExpressionType.FUNCTION) {
+ Function expressionFunction = expression.getFunctionCall();
+ String functionName =
StringUtils.remove(expressionFunction.getOperator(), '_');
Review comment:
why?
##########
File path:
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java
##########
@@ -0,0 +1,330 @@
+/**
+ * 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 com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+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.utils.request.FilterQueryTree;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import
org.apache.pinot.core.operator.transform.function.DateTimeConversionTransformFunction;
+import
org.apache.pinot.core.operator.transform.function.TimeConversionTransformFunction;
+import org.apache.pinot.core.query.optimizer.filter.utils.Range;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.data.DateTimeFieldSpec.TimeFormat;
+import org.apache.pinot.spi.data.DateTimeFormatSpec;
+import org.apache.pinot.spi.data.DateTimeGranularitySpec;
+import org.apache.pinot.spi.data.Schema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code TimePredicateFilterOptimizer} optimizes the time related
predicates:
+ * <ul>
+ * <li>
+ * Optimizes TIME_CONVERT/DATE_TIME_CONVERT function with range/equality
predicate to directly apply the predicate
+ * to the inner expression.
+ * </li>
+ * </ul>
+ *
+ * NOTE: This optimizer is followed by the {@link MergeRangeFilterOptimizer},
which can merge the generated ranges.
+ */
+public class TimePredicateFilterOptimizer implements FilterOptimizer {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TimePredicateFilterOptimizer.class);
+
+ @Override
+ public FilterQueryTree optimize(FilterQueryTree filterQueryTree, @Nullable
Schema schema) {
+ // Do not rewrite PQL queries because PQL is deprecated
+ return filterQueryTree;
+ }
+
+ @Override
+ public Expression optimize(Expression filterExpression, @Nullable Schema
schema) {
+ return optimize(filterExpression);
+ }
+
+ @VisibleForTesting
+ Expression optimize(Expression filterExpression) {
+ Function filterFunction = filterExpression.getFunctionCall();
+ FilterKind filterKind = FilterKind.valueOf(filterFunction.getOperator());
+ List<Expression> operands = filterFunction.getOperands();
+ if (filterKind == FilterKind.AND || filterKind == FilterKind.OR) {
+ operands.replaceAll(this::optimize);
Review comment:
when did we start using this coding convention?
--
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]