Skip to content

Fix problem with sorting in GraphQL by case expression

What does this MR do?

Related to #249245 (closed)

This MR fixes problem with sorting in GraphQL when CASE expression is used. Previously we have not fixed fully the problem, we need to provide CASE expression in SELECT statement (as case_order_value) to properly build queries for paginated GraphQL responses in Gitlab::Graphql::Pagination::Keyset::QueryBuilder.

Queries

This MR slightly modifies queries for fetching vulnerabilities (it simply adds CASE statement as an additional column in results).

Before:

SELECT "vulnerabilities".*
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 19
ORDER BY CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END ASC,
  "vulnerabilities"."id" DESC
LIMIT 100

https://explain.depesz.com/s/IZXp (~60ms)

SELECT "vulnerabilities".*
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 19
ORDER BY CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END DESC,
  "vulnerabilities"."id" DESC
LIMIT 100

https://explain.depesz.com/s/wAD8a (~51ms)

After:

SELECT "vulnerabilities".*,
  CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END AS case_order_value
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 19
ORDER BY CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END ASC,
  "vulnerabilities"."id" DESC
LIMIT 100

https://explain.depesz.com/s/mTOk (~54ms)

SELECT "vulnerabilities".*,
  CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END AS case_order_value
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 19
ORDER BY CASE
    "vulnerabilities"."report_type"
    WHEN 2 THEN 0
    WHEN 5 THEN 1
    WHEN 3 THEN 2
    WHEN 1 THEN 3
    WHEN 0 THEN 4
    WHEN 4 THEN 5
  END DESC,
  "vulnerabilities"."id" DESC
LIMIT 100

https://explain.depesz.com/s/2eh6 (~62ms)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • [-] Label as security and @ mention @gitlab-com/gl-security/appsec
  • [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • [-] Security reports checked/validated by a reviewer from the AppSec team
Edited by Alan (Maciej) Paruszewski

Merge request reports