influxdata / influxdata/influxdb

LIMIT ignored in subqueries

Open
#10,253 5 comments 0 reactions 0 assignees View on GitHub
1.x
Dominant language
Rust
Stars
31.7k
Forks
3.7k
Avg merge
13h 37m
Merged PRs (30d)
8

Description

The following query works fine and returns the most recent record for each group:
```
SELECT something FROM WHERE time >= now() - 1h GROUP BY "tag1", "tag2" ORDER BY time DESC LIMIT 1
```

However, if we want to use it inside a subquery, we do not get the expected result.

```
SELECT * FROM (SELECT something FROM WHERE time >= now() - 1h GROUP BY "tag1", "tag2" ORDER BY time DESC LIMIT 1)
```
fails with:
```
ERR: subqueries must be ordered in the same direction as the query itself
```

and

```
SELECT * FROM (SELECT something FROM WHERE time >= now() - 1h GROUP BY "tag1", "tag2" ORDER BY time DESC LIMIT 1) ORDER BY time DESC
```
does not fail but returns different data (tags turned into fields) and multiple results per group instead of just one, as asked via `LIMIT 1`.

Contributor guide

Open the contributing guide

Research direction

Reproduce both query examples against InfluxDB and compare the nested query's grouping, ordering, tags, and LIMIT behavior with the standalone query. Trace how subqueries apply ORDER BY, GROUP BY, and LIMIT. Done means the subquery preserves one most-recent record per group without requiring matching outer ordering or converting tags into fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.