-- label --
(inherited) untracked • entity+version+file

-- original SQL --
SELECT * FROM lix_internal_state_vtable
	        WHERE schema_key = 'bench_vtable_schema'
          AND entity_id = ?
          AND version_id = ?
          AND file_id = 'bench-file'

-- rewritten SQL --
SELECT *
FROM (
WITH
  wanted_versions AS (
    SELECT 'bench_active_version' AS version_id
  )
SELECT
  w."source_tag" AS "source_tag",
  w."entity_id" AS "entity_id",
  w."schema_key" AS "schema_key",
  w."file_id" AS "file_id",
  w."plugin_key" AS "plugin_key",
  w."snapshot_content" AS "snapshot_content",
  w."schema_version" AS "schema_version",
  w."version_id" AS "version_id",
  w."created_at" AS "created_at",
  w."updated_at" AS "updated_at",
  w."inherited_from_version_id" AS "inherited_from_version_id",
  w."change_id" AS "change_id",
  w."untracked" AS "untracked",
  w."commit_id" AS "commit_id",
  w."metadata" AS "metadata",
  COALESCE(ws_dst.writer_key, ws_src.writer_key, w.writer_key) AS "writer_key"
FROM (
  SELECT
    c.source_tag AS source_tag,
    c.entity_id AS entity_id,
    c.schema_key AS schema_key,
    c.file_id AS file_id,
    c.plugin_key AS plugin_key,
    c.snapshot_content AS snapshot_content,
    c.schema_version AS schema_version,
    c.version_id AS version_id,
    c.created_at AS created_at,
    c.updated_at AS updated_at,
    c.inherited_from_version_id AS inherited_from_version_id,
    c.change_id AS change_id,
    c.untracked AS untracked,
    c.commit_id AS commit_id,
    c.metadata AS metadata,
    c.writer_key AS writer_key,
    c.priority AS priority,
    ROW_NUMBER() OVER (
            PARTITION BY c.file_id, c.schema_key, c.entity_id, c.version_id
            ORDER BY c.priority, c.created_at DESC, c.file_id, c.schema_key, c.entity_id, c.version_id, c.change_id
          ) AS rn
  FROM (
      SELECT
        'U' AS source_tag,
        unt.entity_id AS entity_id,
        unt.schema_key AS schema_key,
        unt.file_id AS file_id,
        unt.plugin_key AS plugin_key,
        json(unt.snapshot_content) AS snapshot_content,
        unt.schema_version AS schema_version,
        unt.version_id AS version_id,
        unt.created_at AS created_at,
        unt.updated_at AS updated_at,
        NULL AS inherited_from_version_id,
        'untracked' AS change_id,
        1 AS untracked,
        'untracked' AS commit_id,
        NULL AS metadata,
        NULL AS writer_key,
        2 AS priority
      FROM lix_internal_state_all_untracked unt
      JOIN wanted_versions wv_unt ON wv_unt.version_id = unt.version_id
      WHERE (unt.schema_key IN ('bench_vtable_schema')) AND (unt.file_id IN ('bench-file')) AND (unt.entity_id IN ('bench_priority_untracked'))

          UNION ALL

      SELECT
        'C' AS source_tag,
        cache.entity_id AS entity_id,
        cache.schema_key AS schema_key,
        cache.file_id AS file_id,
        cache.plugin_key AS plugin_key,
        json(cache.snapshot_content) AS snapshot_content,
        cache.schema_version AS schema_version,
        cache.version_id AS version_id,
        cache.created_at AS created_at,
        cache.updated_at AS updated_at,
        cache.inherited_from_version_id AS inherited_from_version_id,
        cache.change_id AS change_id,
        0 AS untracked,
        cache.commit_id AS commit_id,
        NULL AS metadata,
        NULL AS writer_key,
        3 AS priority
      FROM "lix_internal_state_cache_v1_bench_vtable_schema" cache
      JOIN wanted_versions wv_cache ON wv_cache.version_id = cache.version_id
      WHERE (cache.schema_key IN ('bench_vtable_schema')) AND (cache.file_id IN ('bench-file')) AND (cache.entity_id IN ('bench_priority_untracked'))

          UNION ALL

      		SELECT
          'CI' AS source_tag,
          cache.entity_id AS entity_id,
          cache.schema_key AS schema_key,
          cache.file_id AS file_id,
          cache.plugin_key AS plugin_key,
          json(cache.snapshot_content) AS snapshot_content,
          cache.schema_version AS schema_version,
          vi.version_id AS version_id,
          cache.created_at AS created_at,
          cache.updated_at AS updated_at,
          cache.version_id AS inherited_from_version_id,
          cache.change_id AS change_id,
          0 AS untracked,
          cache.commit_id AS commit_id,
          NULL AS metadata,
          NULL AS writer_key,
          4 AS priority
      		FROM (
      SELECT 'bench_active_version' AS version_id, 'global' AS ancestor_version_id
      ) AS vi
      		JOIN wanted_versions wv_vi ON wv_vi.version_id = vi.version_id
      		JOIN "lix_internal_state_cache_v1_bench_vtable_schema" cache ON cache.version_id = vi.ancestor_version_id
      		WHERE cache.is_tombstone = 0
      		  AND cache.snapshot_content IS NOT NULL AND (cache.schema_key IN ('bench_vtable_schema')) AND (cache.file_id IN ('bench-file')) AND (cache.entity_id IN ('bench_priority_untracked'))

          UNION ALL

      		SELECT
          'UI' AS source_tag,
          unt.entity_id AS entity_id,
          unt.schema_key AS schema_key,
          unt.file_id AS file_id,
          unt.plugin_key AS plugin_key,
          json(unt.snapshot_content) AS snapshot_content,
          unt.schema_version AS schema_version,
          vi.version_id AS version_id,
          unt.created_at AS created_at,
          unt.updated_at AS updated_at,
          unt.version_id AS inherited_from_version_id,
          'untracked' AS change_id,
          1 AS untracked,
          'untracked' AS commit_id,
          NULL AS metadata,
          NULL AS writer_key,
          5 AS priority
      		FROM (
      SELECT 'bench_active_version' AS version_id, 'global' AS ancestor_version_id
      ) AS vi
      		JOIN wanted_versions wv_vi ON wv_vi.version_id = vi.version_id
      		JOIN lix_internal_state_all_untracked unt ON unt.version_id = vi.ancestor_version_id
      		WHERE unt.is_tombstone = 0
      		  AND unt.snapshot_content IS NOT NULL AND (unt.schema_key IN ('bench_vtable_schema')) AND (unt.file_id IN ('bench-file')) AND (unt.entity_id IN ('bench_priority_untracked'))
  ) AS c
) AS w

LEFT JOIN lix_internal_state_writer ws_dst ON
  ws_dst.file_id = w.file_id AND
  ws_dst.entity_id = w.entity_id AND
  ws_dst.schema_key = w.schema_key AND
  ws_dst.version_id = w.version_id
LEFT JOIN lix_internal_state_writer ws_src ON
  ws_src.file_id = w.file_id AND
  ws_src.entity_id = w.entity_id AND
  ws_src.schema_key = w.schema_key AND
  ws_src.version_id = w.inherited_from_version_id

LEFT JOIN lix_internal_change chc ON chc.id = w.change_id

WHERE w.rn = 1
) AS "lix_internal_state_vtable"
WHERE schema_key = 'bench_vtable_schema' AND entity_id = ? AND version_id = ? AND file_id = 'bench-file'

-- plan --
[
  {
    "id": 2,
    "parent": 0,
    "notused": 0,
    "detail": "CO-ROUTINE w"
  },
  {
    "id": 5,
    "parent": 2,
    "notused": 0,
    "detail": "CO-ROUTINE (subquery-11)"
  },
  {
    "id": 7,
    "parent": 5,
    "notused": 0,
    "detail": "CO-ROUTINE c"
  },
  {
    "id": 8,
    "parent": 7,
    "notused": 0,
    "detail": "COMPOUND QUERY"
  },
  {
    "id": 9,
    "parent": 8,
    "notused": 0,
    "detail": "LEFT-MOST SUBQUERY"
  },
  {
    "id": 12,
    "parent": 9,
    "notused": 0,
    "detail": "MATERIALIZE wanted_versions"
  },
  {
    "id": 14,
    "parent": 12,
    "notused": 0,
    "detail": "SCAN CONSTANT ROW"
  },
  {
    "id": 33,
    "parent": 9,
    "notused": 16,
    "detail": "SCAN wv_unt"
  },
  {
    "id": 39,
    "parent": 9,
    "notused": 47,
    "detail": "SEARCH unt USING INDEX sqlite_autoindex_lix_internal_state_all_untracked_1 (entity_id=? AND schema_key=? AND file_id=? AND version_id=?)"
  },
  {
    "id": 75,
    "parent": 8,
    "notused": 0,
    "detail": "UNION ALL"
  },
  {
    "id": 88,
    "parent": 75,
    "notused": 45,
    "detail": "SEARCH cache USING PRIMARY KEY (entity_id=? AND file_id=? AND version_id=?)"
  },
  {
    "id": 102,
    "parent": 75,
    "notused": 216,
    "detail": "SCAN wv_cache"
  },
  {
    "id": 128,
    "parent": 8,
    "notused": 0,
    "detail": "UNION ALL"
  },
  {
    "id": 130,
    "parent": 128,
    "notused": 0,
    "detail": "CO-ROUTINE vi"
  },
  {
    "id": 131,
    "parent": 130,
    "notused": 0,
    "detail": "SCAN CONSTANT ROW"
  },
  {
    "id": 159,
    "parent": 128,
    "notused": 16,
    "detail": "SCAN vi"
  },
  {
    "id": 164,
    "parent": 128,
    "notused": 27,
    "detail": "SEARCH cache USING INDEX idx_lix_internal_state_cache_v1_bench_vtable_schema_live_vfe (version_id=? AND file_id=? AND entity_id=?)"
  },
  {
    "id": 182,
    "parent": 128,
    "notused": 216,
    "detail": "SCAN wv_vi"
  },
  {
    "id": 209,
    "parent": 8,
    "notused": 0,
    "detail": "UNION ALL"
  },
  {
    "id": 211,
    "parent": 209,
    "notused": 0,
    "detail": "CO-ROUTINE vi"
  },
  {
    "id": 212,
    "parent": 211,
    "notused": 0,
    "detail": "SCAN CONSTANT ROW"
  },
  {
    "id": 240,
    "parent": 209,
    "notused": 16,
    "detail": "SCAN vi"
  },
  {
    "id": 245,
    "parent": 209,
    "notused": 47,
    "detail": "SEARCH unt USING INDEX sqlite_autoindex_lix_internal_state_all_untracked_1 (entity_id=? AND schema_key=? AND file_id=? AND version_id=?)"
  },
  {
    "id": 265,
    "parent": 209,
    "notused": 216,
    "detail": "SCAN wv_vi"
  },
  {
    "id": 293,
    "parent": 5,
    "notused": 197,
    "detail": "SCAN c"
  },
  {
    "id": 345,
    "parent": 5,
    "notused": 0,
    "detail": "USE TEMP B-TREE FOR LAST 7 TERMS OF ORDER BY"
  },
  {
    "id": 390,
    "parent": 2,
    "notused": 177,
    "detail": "SCAN (subquery-11)"
  },
  {
    "id": 476,
    "parent": 0,
    "notused": 177,
    "detail": "SCAN w"
  },
  {
    "id": 489,
    "parent": 0,
    "notused": 45,
    "detail": "SEARCH ws_dst USING PRIMARY KEY (file_id=? AND version_id=? AND entity_id=? AND schema_key=?) LEFT-JOIN"
  },
  {
    "id": 502,
    "parent": 0,
    "notused": 45,
    "detail": "SEARCH ws_src USING PRIMARY KEY (file_id=? AND version_id=? AND entity_id=? AND schema_key=?) LEFT-JOIN"
  }
]
