Skip to content

Timestamp bucketed queries are messing up the ORDER BY in the UI #3905

In review

Description

@czephyr

I think I bumped into a weird UI behaviour. To repro it easily for the screen I set the max queried rows to 5 in the config; but this happens with default amount of rows too. Version is v2.47.0

This is the "Generated SQL" of the query I'm running, notice the ORDER BY Duration DESC:

SELECT
  Timestamp,
  ServiceName as service,
  StatusCode as level,
  round(Duration / 1e6) as duration,
  SpanName,
  materialize(hostName()) AS replica,
  toUnixTimestamp(Timestamp)
FROM
  otel_traces.distributed_otel_traces
WHERE
  (
    Timestamp >= fromUnixTimestamp64Milli(1796381500000)
    AND Timestamp <= fromUnixTimestamp64Milli(1796385100000)
  )
  AND (
    (
      (
        `SpanAttributes` ['aws.s3.bucket'] = 'XXXXXXXXXXXXXX'
        AND indexHint(mapContains(`SpanAttributes`, 'aws.s3.bucket'))
      )
      AND (ServiceName = 'XXX')
    )
  )
ORDER BY
  Duration DESC,
  Timestamp DESC
LIMIT
  5

When running on the DB this SQL the result is:

┌─────────────────────Timestamp─┬─service─┬─level─┬─duration─┬─SpanName─┬─replica────────────────────────────┬─toUnixTimestamp(Timestamp)─┐
1. │ 2026-12-04 11:10:17.085063226 │ XXX     │ Unset │      160 │ HTTP GET │ clickhouse-0-0-0 │                 1796382617 │ -- 1.80 billion
2. │ 2026-12-04 11:46:07.334630365 │ XXX     │ Unset │       84 │ HTTP PUT │ clickhouse-0-0-0 │                 1796384767 │ -- 1.80 billion
3. │ 2026-12-04 11:47:09.287748311 │ XXX     │ Unset │       82 │ HTTP PUT │ clickhouse-0-0-0 │                 1796384829 │ -- 1.80 billion
4. │ 2026-12-04 11:44:09.568353399 │ XXX     │ Unset │       81 │ HTTP PUT │ clickhouse-0-0-0 │                 1796384649 │ -- 1.80 billion
5. │ 2026-12-04 11:26:07.981116895 │ XXX     │ Unset │       80 │ HTTP PUT │ clickhouse-0-0-0 │                 1796383567 │ -- 1.80 billion
   └───────────────────────────────┴─────────┴───────┴──────────┴──────────┴────────────────────────────────────┴────────────────────────────┘

The queries HyperDX runs, taken from the console, are actually bucketed by Timestamp and have an OFFSET:

Sending query:
SELECT
  Timestamp,
  ServiceName as service,
  StatusCode as level,
  round(Duration / 1e6) as duration,
  SpanName,
  materialize(hostName()) AS replica,
  toUnixTimestamp(Timestamp),
  ServiceName,
  toDate(Timestamp),
  toDateTime(Timestamp)
FROM
  otel_traces.distributed_otel_traces
WHERE
  (
    Timestamp >= fromUnixTimestamp64Milli(1796385040000)
    AND Timestamp <= fromUnixTimestamp64Milli(1796385100000)
  )
  AND (
    (
      (
        `SpanAttributes` ['aws.s3.bucket'] = 'XXXXXXXXXXXXXXXXXXX'
        AND indexHint(mapContains(`SpanAttributes`, 'aws.s3.bucket'))
      )
      AND (ServiceName = 'XXX')
    )
  )
ORDER BY
  Duration DESC,
  Timestamp DESC
LIMIT
  5 OFFSET 0 1021-97fd44dc6a95f2cb.js:1:2939

Sending query:
SELECT
  Timestamp,
  ServiceName as service,
  StatusCode as level,
  round(Duration / 1e6) as duration,
  SpanName,
  materialize(hostName()) AS replica,
  toUnixTimestamp(Timestamp),
  ServiceName,
  toDate(Timestamp),
  toDateTime(Timestamp)
FROM
  otel_traces.distributed_otel_traces
WHERE
  (
    Timestamp >= fromUnixTimestamp64Milli(1796385040000)
    AND Timestamp <= fromUnixTimestamp64Milli(1796385100000)
  )
  AND (
    (
      (
        `SpanAttributes` ['aws.s3.bucket'] = 'XXXXXXXXXXXXXXXXXXX'
        AND indexHint(mapContains(`SpanAttributes`, 'aws.s3.bucket'))
      )
      AND (ServiceName = 'XXX')
    )
  )
ORDER BY
  Duration DESC,
  Timestamp DESC
LIMIT
  5 OFFSET 4

Sending query:
SELECT
  Timestamp,
  ServiceName as service,
  StatusCode as level,
  round(Duration / 1e6) as duration,
  SpanName,
  materialize(hostName()) AS replica,
  toUnixTimestamp(Timestamp),
  ServiceName,
  toDate(Timestamp),
  toDateTime(Timestamp)
FROM
  otel_traces.distributed_otel_traces
WHERE
  (
    Timestamp >= fromUnixTimestamp64Milli(1796384140000)
    AND Timestamp <= fromUnixTimestamp64Milli(1796385040000)
  )
  AND (
    (
      (
        `SpanAttributes` ['aws.s3.bucket'] = 'XXXXXXXXXXXXXXXXXXX'
        AND indexHint(mapContains(`SpanAttributes`, 'aws.s3.bucket'))
      )
      AND (ServiceName = 'XXX')
    )
  )
ORDER BY
  Duration DESC,
  Timestamp DESC
LIMIT
  5 OFFSET 0
[...]

As it can be seen in the screen, the UI will render them in what seems to be the order the bucketed queries returned, instead of the actual ORDER BY that was requested. In the screen, the first OFFSET query (first of the three I reported) gets put on top and the rest of the results get appended.

This can be a rather confusing UX (notice that the top result duration: 160 is not even in the first screen, and will only be retrieved by scrolling down quite far, notice in the second screen)
image

image

Activity

  1. added
    bug
    Needs Feedback
    on Dec 4, 2026
  2. changed the title [-]OFFSET queries are messing up the ORDER BY in the UI[/-] [+]Timestamp bucketed queries are messing up the ORDER BY in the UI[/+] on Dec 4, 2026
  3. pulpdrew commented on Dec 4, 2026

    @pulpdrew
    Contributor

    Hi @czephyr thanks for reporting - could you please share what you have configured for the source's Timestamp Column in the source settings? And could you share the source's schema(s)?

  4. czephyr commented on Dec 4, 2026

    @czephyr
    Author

    Hey @pulpdrew thanks for your answer.

    Timestamp Column: Timestamp (autoselected at source creation time)

    DDL Schema:

     ┌─statement─────────────────────────────────────────────────────────────────────────────────────────┐
    1. │ CREATE TABLE otel_traces.otel_traces                                                             ↴│
       │↳(                                                                                                ↴│
       │↳    `Timestamp` DateTime64(9) CODEC(Delta(8), ZSTD(1)),                                          ↴│
       │↳    `TraceId` String CODEC(ZSTD(1)),                                                             ↴│
       │↳    `SpanId` String CODEC(ZSTD(1)),                                                              ↴│
       │↳    `ParentSpanId` String CODEC(ZSTD(1)),                                                        ↴│
       │↳    `TraceState` String CODEC(ZSTD(1)),                                                          ↴│
       │↳    `SpanName` LowCardinality(String) CODEC(ZSTD(1)),                                            ↴│
       │↳    `SpanKind` LowCardinality(String) CODEC(ZSTD(1)),                                            ↴│
       │↳    `ServiceName` LowCardinality(String) CODEC(ZSTD(1)),                                         ↴│
       │↳    `ResourceAttributes` Map(LowCardinality(String), String) CODEC(ZSTD(1)),                     ↴│
       │↳    `ScopeName` String CODEC(ZSTD(1)),                                                           ↴│
       │↳    `ScopeVersion` String CODEC(ZSTD(1)),                                                        ↴│
       │↳    `SpanAttributes` Map(LowCardinality(String), String) CODEC(ZSTD(1)),                         ↴│
       │↳    `Duration` UInt64 CODEC(ZSTD(1)),                                                            ↴│
       │↳    `StatusCode` LowCardinality(String) CODEC(ZSTD(1)),                                          ↴│
       │↳    `StatusMessage` String CODEC(ZSTD(1)),                                                       ↴│
       │↳    `Events.Timestamp` Array(DateTime64(9)) CODEC(ZSTD(1)),                                      ↴│
       │↳    `Events.Name` Array(LowCardinality(String)) CODEC(ZSTD(1)),                                  ↴│
       │↳    `Events.Attributes` Array(Map(LowCardinality(String), String)) CODEC(ZSTD(1)),               ↴│
       │↳    `Links.TraceId` Array(String) CODEC(ZSTD(1)),                                                ↴│
       │↳    `Links.SpanId` Array(String) CODEC(ZSTD(1)),                                                 ↴│
       │↳    `Links.TraceState` Array(String) CODEC(ZSTD(1)),                                             ↴│
       │↳    `Links.Attributes` Array(Map(LowCardinality(String), String)) CODEC(ZSTD(1)),                ↴│
       │↳    INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,                           ↴│
       │↳    INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,    ↴│
       │↳    INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,↴│
       │↳    INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,       ↴│
       │↳    INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,   ↴│
       │↳    INDEX idx_duration Duration TYPE minmax GRANULARITY 1                                        ↴│
       │↳)                                                                                                ↴│
       │↳ENGINE = ReplicatedMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}')                   ↴│
       │↳PARTITION BY toDate(Timestamp)                                                                   ↴│
       │↳ORDER BY (ServiceName, SpanName, toDateTime(Timestamp))                                          ↴│
       │↳TTL toDate(Timestamp) + toIntervalDay(10)                                                        ↴│
       │↳SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1                                        │
       └───────────────────────────────────────────────────────────────────────────────────────────────────┘
    
  5. pulpdrew commented on Dec 4, 2026

    @pulpdrew
    Contributor

    Thanks again for reporting - #3907 should fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Feedbackbugexternal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions