Skip to content

Joining subquery with orderBy and limit ignores the limit #507

@kevin-dp

Description

@kevin-dp

Consider this query:

q
  .from({ issue: issuesCollection })
  .join(
    {
      users: q
        .from({ user: usersCollection })
        .where(({ user }) => eq(user.status, `active`))
        .orderBy(({ user }) => user.name, `asc`)
        .limit(1),
    },
    ({ issue, users }) => eq(issue.userId, users.id),
    `left`
  )

It joins with a subquery that returns a single user. So the results of the query should only ever contain that one user. However, the optimized version of this query doesn't take into account the limit on the subquery and the resulting rows are joined with all users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions