ashishjayamohan commented on code in PR #14385:
URL: https://github.com/apache/pinot/pull/14385#discussion_r1830325783


##########
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java:
##########
@@ -411,6 +417,96 @@ && isStringLiteral(dateTimeConvertOperands.get(3)),
     }
   }
 
+  private void optimizeDateTrunc(Function filterFunction, FilterKind 
filterKind) {
+    List<Expression> filterOperands = filterFunction.getOperands();
+    List<Expression> dateTruncOperands = 
filterOperands.get(0).getFunctionCall().getOperands();
+
+    // Check if date trunc function is being applied on a literal value
+    if (dateTruncOperands.get(1).isSetLiteral()) {
+      return;
+    }
+
+    Long lowerMillis = null;
+    Long upperMillis = null;
+    DateTimeFormatSpec inputFormat = (dateTruncOperands.size() >= 3)
+        ? new 
DateTimeFormatSpec(dateTruncOperands.get(2).getLiteral().getStringValue())
+        : new DateTimeFormatSpec("TIMESTAMP");
+    boolean lowerInclusive = true;
+    boolean upperInclusive = true;
+    List<Expression> operands = new ArrayList<>(dateTruncOperands);
+    switch (filterKind) {
+      case EQUALS:
+        operands.set(1, filterOperands.get(1));
+        lowerMillis = dateTruncFloor(operands);
+        upperMillis = dateTruncCeil(operands);
+        // Check if it is impossible to obtain literal equality
+        if (lowerMillis != 
inputFormat.fromFormatToMillis(filterOperands.get(1).getLiteral().getLongValue()))
 {
+          lowerMillis = Long.MAX_VALUE;
+          upperMillis = Long.MIN_VALUE;
+        }
+        break;
+      case GREATER_THAN:
+        lowerInclusive = false;
+        operands.set(1, filterOperands.get(1));
+        lowerMillis = dateTruncCeil(operands);
+        break;
+      case GREATER_THAN_OR_EQUAL:
+        operands.set(1, filterOperands.get(1));
+        lowerInclusive = false;
+        lowerMillis = dateTruncCeil(operands);
+        if (dateTruncFloor(operands)
+            == 
inputFormat.fromFormatToMillis(filterOperands.get(1).getLiteral().getLongValue()))
 {
+          lowerInclusive = true;
+          lowerMillis = dateTruncFloor(operands);
+        }
+        break;
+      case LESS_THAN:
+        upperInclusive = false;
+        operands.set(1, filterOperands.get(1));
+        upperMillis = dateTruncFloor(operands);
+        if (upperMillis != 
inputFormat.fromFormatToMillis(filterOperands.get(1).getLiteral().getLongValue()))
 {
+          upperInclusive = true;
+          upperMillis = dateTruncCeil(operands);
+        }
+        break;
+      case LESS_THAN_OR_EQUAL:
+        operands.set(1, filterOperands.get(1));
+        upperMillis = dateTruncCeil(operands);
+        break;
+      case BETWEEN:
+        Literal lowerLiteral = new Literal();
+        if (filterOperands.get(1).getLiteral().isSetStringValue()) {
+          lowerLiteral.setLongValue(new 
DateTimeFormatSpec("TIMESTAMP").fromFormatToMillis(
+              filterOperands.get(1).getLiteral().getStringValue()));
+        } else {
+          lowerLiteral.setLongValue(getLongValue(filterOperands.get(1)));
+        }
+        Expression lowerExpression = new Expression(ExpressionType.LITERAL);
+        lowerExpression.setLiteral(lowerLiteral);
+        operands.set(1, lowerExpression);
+        lowerMillis = dateTruncCeil(operands);
+        Literal upperLiteral = new Literal();
+        if (filterOperands.get(2).getLiteral().isSetStringValue()) {
+          upperLiteral.setLongValue(new 
DateTimeFormatSpec("TIMESTAMP").fromFormatToMillis(

Review Comment:
   Make output format with TIMESTAMP as default, output format if specified



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

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

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