Skip to content

[BUG] Span millisecond incorrectly converted to microsecond interval #4550

Closed

Description

@dai-chen

What is the bug?

When using span=X milliseconds in timechart commands, the span unit is incorrectly converted to IntervalUnit.MICROSECOND by PlanUtils.spanUnitToIntervalUnit(). The root cause is that the IntervalUnit enum does not include a MILLISECOND value, only MICROSECOND.

How can one reproduce the bug?
Steps to reproduce the behavior:

  1. Execute a PPL query with timechart using millisecond span:
    source=logs | timechart span=500ms count()
  2. The span calculation will use microsecond interval unit instead of millisecond, causing incorrect time bucketing.

What is the expected behavior?

  • IntervalUnit enum should include a MILLISECOND value
  • PlanUtils should correctly convert between SpanUnit.MILLISECOND and IntervalUnit.MILLISECOND

Do you have any additional context?

Impact: This bug also blocks the implementation of millisecond support in the timechart per_* functions (per_second, per_minute, per_hour, per_day). The per_* function transformation logic calculates span width in seconds instead of millisecond, leading to incorrect results when a millisecond span is used.

source=test_data_2023 | timechart span=1ms per_second(packets);
fetched rows / total rows = 5/5
+---------------------+-----------------------+
| @timestamp          | per_second(packets)   |
|---------------------+-----------------------|
| 2023-10-08 10:00:00 | null                  |
| 2023-10-08 10:00:05 | null                  |
| 2023-10-08 10:00:30 | null                  |
| 2023-10-08 10:00:40 | null                  |
| 2023-10-08 10:00:50 | null                  |
+---------------------+-----------------------+

Activity

  1. added
    bug
    PPL
    on Oct 14, 2025
  2. self-assigned this
    on Oct 20, 2025
  3. yuancu commented on Oct 27, 2025

    @yuancu
    Collaborator

    I think the missing support for MILLISECOND in IntervalUnit only contributes to part of the problem. The implementation of per functions is also flawed in handling units below seconds:

    In the below snippets:

    PerFunction perFunc = perFuncOpt.get();
        Span span = (Span) this.binExpression;
        Field spanStartTime = AstDSL.field(IMPLICIT_FIELD_TIMESTAMP);
        Function spanEndTime = timestampadd(span.getUnit(), span.getValue(), spanStartTime);
        Function spanSeconds = timestampdiff(SECOND, spanStartTime, spanEndTime);
    
        return eval(
            timechart(AstDSL.alias(perFunc.aggName, sum(perFunc.aggArg))),
            let(perFunc.aggName).multiply(perFunc.seconds).dividedBy(spanSeconds));

    It calculates the per-results as:

    $$\frac{\text{rate in seconds}}{\text{span length in seconds}}$$

    in which the span length in seconds is computed with TIMESTAMPDIFF function. However, TIMESTAMPDIFF only returns bigint, rendering a millisecond in second returning 0. I'll try to fix it by making the computation in milliseconds.

  4. moved this from Not Started to Done in PPL Trackeron Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions