anshul98ks123 opened a new pull request, #20276:
URL: https://github.com/apache/pinot/pull/20276

   ## Problem
   
   `DATETRUNC(unit, col, inputUnit[, timeZone])` returns values in `inputUnit` 
when the output-unit argument is omitted. `TimePredicateFilterOptimizer` 
assumes milliseconds instead, so a filter on a column in any other unit is 
rewritten to a range in the wrong unit. On a microsecond column:
   
   ```sql
   WHERE DATETRUNC('DAY', ts, 'MICROSECONDS') >= 1620777600000000
   ```
   
   is rewritten to `ts >= 1620777600000000000`, which is 1000x too large. Every 
segment is pruned (`ALL_SEGMENTS_PRUNED_ON_SERVER`) and the query returns no 
rows. On a seconds column, `DATETRUNC('DAY', ts, 'SECONDS') = 1620777600` is 
read as an unaligned millisecond value and rewritten to an always-false range. 
On a nanosecond column the bounds overflow, so `<=` turns into a match-all 
range. This affects every table type.
   
   ## Root cause
   
   When the 5th argument is absent, `optimizeDateTrunc` defaults the output 
unit to `MILLISECONDS`. `DateTruncTransformFunction` and the `dateTrunc` scalar 
function both default it to the input unit, which is what the function's 
documentation says.
   
   ## Fix
   
   The output unit now defaults to the input unit, matching the transform 
function. The 2-argument form (both units default to milliseconds) and the 
5-argument form are unchanged.
   
   ## Testing
   
   The new `testDateTruncOptimizerDefaultOutputUnit` covers the 3- and 
4-argument forms on microsecond and second columns. It fails without the fix. 
The existing `TimePredicateFilterOptimizerTest` cases still pass.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

  • [PR] Fix DATETRUNC filter rewri... anshul98ks123 (via GitHub) via commits

Reply via email to