Skip to content

[FEATURE] PPL per_* aggregation function support #4350

Closed

Description

@dai-chen

Is your feature request related to a problem?

PPL currently lacks support for per_* aggregation functions (per_second, per_minute, per_hour, per_day). These functions calculate rate-based metrics by normalizing aggregated values to specific time units, converting raw counts into meaningful per-unit rates.

Without these functions, users cannot easily perform rate calculations that are common in performance monitoring scenarios, such as calculating packets per second, requests per minute when using the timechart command.

What solution would you like?

Implement the four per_* aggregation functions in PPL:

  • per_second(<value>) - Returns values normalized to per-second rate
  • per_minute(<value>) - Returns values normalized to per-minute rate
  • per_hour(<value>) - Returns values normalized to per-hour rate
  • per_day(<value>) - Returns values normalized to per-day rate

These functions should work exclusively with the timechart command (due to implicit timestamp field dependency):

# Sample data
  {\"_time\":\"2025-09-08T10:00:00\", \"packets\":10},
  {\"_time\":\"2025-09-08T10:00:05\", \"packets\":60},
  {\"_time\":\"2025-09-08T10:00:30\", \"packets\":20},
  {\"_time\":\"2025-09-08T10:00:50\", \"packets\":30}

# Example 1
...
| eval _time=strptime(_time, "%Y-%m-%dT%H:%M:%S")
| timechart per_second(packets) span=1m

----------------------|--------------------
_time                 | per_second(packets)
----------------------|--------------------
2025-09-08T10:00:00   | 2                   # (10+60+20+30)/60s

# Example 2
timechart per_second(packets) span=20s

----------------------|--------------------
_time                 | per_second(packets)
----------------------|--------------------
2025-09-08T10:00:00   | 3.5                  # (10+60)/20s
2025-09-08T10:00:20   | 1                    # (20)/20s
2025-09-08T10:00:40   | 1.5                  # (30)/20s

# Example 3
timechart per_minute(packets), per_hour(packets), per_day(packets) span=1m

----------------------|--------------------|--------------------|--------------------
_time                 | per_minute(packets)| per_hour(packets)  | per_day(packets)
----------------------|--------------------|--------------------|--------------------
2025-09-08T10:00:00   | 120                | 7200               | 172800 

What alternatives have you considered?

  • Manual calculation: Users can manually divide aggregated values by time span, but this requires knowledge of time conversion factors.

Do you have any additional context?

Implementation approaches

  • Short-term solution: Implement rewriting for fixed-width buckets
    • Currently PPL timechart only supports span option which is fixed interval
    • We can simply transform per_* functions to mathematical formulas at compile time
# Example
... | timechart per_second(packets) span=1m

=>

SELECT SUM(packets) / 60
...
GROUP BY SPAN(@timestamp, 1m)
  • Long-term solutions [TBD]
    • The primary challenge lies in dynamic bucketing behavior in bin-options.
    • Option 1: Output bounds from bucketing function similar as windowing function in Spark SQL
    • Option 2: Dynamic calculation via LEAD window function to determine next bucket's start time

Activity

  1. added
    enhancement
    PPL
    on Sep 22, 2025
  2. self-assigned this
    on Sep 22, 2025
  3. dai-chen commented on Oct 1, 2025

    @dai-chen
    CollaboratorAuthor

    Implementation Options for Fixed Interval Support

    Here are the implementation options for implementing the short-term solution for fixed-width bucketing above. Take timechart span=10m per_second(packet) for example:

    • Option 1: Inline Arithmetic Conversion

      • Idea: => timechart span=10m sum(packet) / (60 * 10)
      • Pros:
        • Most straightforward approach
        • Only changes in AST layer without need to modify analyzer layer or register UDF
      • Cons:
        • Major blocker: Both timechart and aggregation in CalciteRelNodeVisitor require simple aggregation functions instead of expressions. This requires major architectural changes to support complex expressions.
    • Option 2: Context-Aware UDAF

      • Idea: => timechart span=10m internal_per_function(packet, unit='s', interval='10m')
      • Pros:
        • No need to change analyzer layer
        • Clean encapsulation of all per_* function logic
      • Cons:
        • Changes spread across both AST layer and function registry
        • Major blocker: Cannot support variable-length span with calendar (month/quarter/year).
    • Option 3: Eval Tranformation Post-Timechart (Preferred)

      • Idea: => timechart span=10m sum(packet) as `per_second(packet)` | eval `per_second(packet)` = `per_second(packet)`/600
      • Pros:
        • Only changes in AST layer
        • Leverages existing eval functionality
        • Currently timechart command doesn't do pivot, making this approach viable
        • Replacing seconds (600) with time function can support variable-length span
      • Cons:
        • May break if future changes to the timechart analyzer don’t account for Eval post-processing.
        • Need to manage field names carefully to avoid exposing internal sum function unexpectedly
  4. moved this from Not Started to In review in PPL Trackeron Oct 13, 2025
  5. moved this from In review to Done in PPL Trackeron Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions