Skip to content

lib/logstorage: support sort by (...) for the values stats function#1553

Open
niladrix719 wants to merge 2 commits into
VictoriaMetrics:masterfrom
niladrix719:sort-by-stats-fn#1426
Open

lib/logstorage: support sort by (...) for the values stats function#1553
niladrix719 wants to merge 2 commits into
VictoriaMetrics:masterfrom
niladrix719:sort-by-stats-fn#1426

Conversation

@niladrix719

@niladrix719 niladrix719 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #1426

Add sort by (...) support (with optional limit N for top-N selection) to the values stats function, mirroring json_values, so collected values can be ordered by fields of the matching log entries

Example

Ingest 3 logs:

curl -s -X POST 'http://localhost:9428/insert/jsonline' \
  -H 'Content-Type: application/stream+json' --data-binary '
{"_msg":"x","_time":"2024-01-01T00:00:03Z","ip":"10.0.0.3"}
{"_msg":"x","_time":"2024-01-01T00:00:01Z","ip":"10.0.0.1"}
{"_msg":"x","_time":"2024-01-01T00:00:02Z","ip":"10.0.0.2"}
'

Collect ip values ordered by _time:

curl -s 'http://localhost:9428/select/logsql/query' \
  --data-urlencode 'query=* | stats values(ip) sort by (_time) as ips'
{"ips":"[\"10.0.0.1\",\"10.0.0.2\",\"10.0.0.3\"]"}

Keep the 2 most recent with sort by (_time desc) limit 2:

curl -s 'http://localhost:9428/select/logsql/query' \
  --data-urlencode 'query=* | stats values(ip) sort by (_time desc) limit 2 as ips'
{"ips":"[\"10.0.0.3\",\"10.0.0.2\"]"}

Signed-off-by: Niladri Adhikary <niladrix719@gmail.com>
@niladrix719 niladrix719 changed the title lib/logstorage: support for the stats function lib/logstorage: support sort by (...) for the values stats function Jun 29, 2026
@niladrix719 niladrix719 changed the title lib/logstorage: support sort by (...) for the values stats function lib/logstorage: support sort by (...) for the values stats function Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logsql: support sorting values returned by values(...) stats function

2 participants