Jackie-Jiang commented on a change in pull request #6957:
URL: https://github.com/apache/incubator-pinot/pull/6957#discussion_r637208782



##########
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,
+        "Exactly 3 arguments are required for TIME_CONVERT transform 
function");
+    Preconditions
+        .checkArgument(isStringLiteral(timeConvertOperands.get(1)) && 
isStringLiteral(timeConvertOperands.get(2)),
+            "The 2nd and 3rd argument for TIME_CONVERT transform function must 
be string literal");
+
+    try {
+      TimeUnit inputTimeUnit = 
TimeUnit.valueOf(timeConvertOperands.get(1).getLiteral().getStringValue().toUpperCase());
+      TimeUnit outputTimeUnit =
+          
TimeUnit.valueOf(timeConvertOperands.get(2).getLiteral().getStringValue().toUpperCase());
+
+      // Step 1: Convert output range to millis range
+      Long lowerMillis = null;
+      Long upperMillis = null;
+      switch (filterKind) {
+        case GREATER_THAN: {
+          // millisToFormat(millis) > n
+          // -> millisToFormat(millis) >= n + 1
+          // -> millis >= formatToMillis(n + 1)

Review comment:
       We have to do `+1` here, or it won't be correct. E.g. 
`millisToSeconds(millis) > 0` is not equivalent to `millis > 0` because of the 
flooring of the value. Will add the example into the comment




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