ashishjayamohan commented on code in PR #14385: URL: https://github.com/apache/pinot/pull/14385#discussion_r1833152007
########## pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/filter/TimePredicateFilterOptimizer.java: ########## @@ -411,6 +428,80 @@ && 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 = new DateTimeFormatSpec("TIMESTAMP"); + String inputTimeUnit = (dateTruncOperands.size() >= 3) ? dateTruncOperands.get(2).getLiteral().getStringValue() + : TimeUnit.MILLISECONDS.name(); + String outputTimeUnit = (dateTruncOperands.size() == 5) ? dateTruncOperands.get(4).getLiteral().getStringValue() + : TimeUnit.MILLISECONDS.name(); + boolean lowerInclusive = true; + boolean upperInclusive = true; + List<Expression> operands = new ArrayList<>(dateTruncOperands); + switch (filterKind) { + case EQUALS: + operands.set(1, getExpression(filterOperands.get(1), inputFormat, inputTimeUnit, outputTimeUnit)); + lowerMillis = dateTruncFloor(operands); + upperMillis = dateTruncCeil(operands); + // Check if it is impossible to obtain literal equality + if (lowerMillis != TimeUnit.valueOf(inputTimeUnit).convert(getLongValue(filterOperands.get(1)), Review Comment: Good catch! I actually made a mistake here. This line should be: `if (lowerMillis != getLongValue(filterOperands.get(1),` Essentially, in equality predicates it is possible that the literal value (`filterOperands.get(1)`) is not a feasible output from `dateTrunc`. In this case, there are no values that could be truncated to that literal. Consider this example where a specified literal is not on an even granularity step (assume that the granularity is the . In this case, an equality predicate would be existentially false. We can check this by taking the regular `dateTrunc` of the literal and checking if the output is equal to the literal itself. ![not-on-gran](https://github.com/user-attachments/assets/771ea6fe-9eaf-47fe-8a4c-718368b80e6a) In the case where the specified literal *is* on an even granularity step, like below, we have multiple acceptable values. Specifically, the range from the literal to the next granularity step (non-inclusive of the maximum). This case is identified when the `dateTrunc` of the literal equals the literal. ![on-gran](https://github.com/user-attachments/assets/da5fae4d-5b47-4f39-913c-fa9619475390) -- 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