AggInverse
Execute the xInverse function for an aggregate.
An example
2 of the 178 statements we compiled against the sample database use AggInverse. This is the one with the shortest program; the rows that use AggInverse are highlighted.
SELECT percent_rank() OVER (ORDER BY ms) FROM tracks
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 80 | 0 | 0 | |
| 1 | Null | 0 | 1 | 0 | 0 | |
| 2 | InitCoroutine | 3 | 17 | 3 | 0 | |
| 3 | SorterOpen | 6 | 3 | 0 | k(1,B) | 0 |
| 4 | OpenRead | 0 | 4 | 0 | 4 | 0 |
| 5 | Rewind | 0 | 10 | 0 | 0 | |
| 6 | Column | 0 | 3 | 4 | 0 | |
| 7 | MakeRecord | 4 | 1 | 6 | 0 | |
| 8 | SorterInsert | 6 | 6 | 4 | 1 | 0 |
| 9 | Next | 0 | 6 | 0 | 1 | |
| 10 | OpenPseudo | 7 | 7 | 3 | 0 | |
| 11 | SorterSort | 6 | 16 | 0 | 0 | |
| 12 | SorterData | 6 | 7 | 7 | 0 | |
| 13 | Column | 7 | 0 | 5 | 0 | |
| 14 | Yield | 3 | 0 | 0 | 0 | |
| 15 | SorterNext | 6 | 12 | 0 | 0 | |
| 16 | EndCoroutine | 3 | 0 | 0 | 0 | |
| 17 | OpenEphemeral | 1 | 1 | 0 | 0 | |
| 18 | OpenDup | 2 | 1 | 0 | 0 | |
| 19 | OpenDup | 3 | 1 | 0 | 0 | |
| 20 | OpenDup | 4 | 1 | 0 | 0 | |
| 21 | Integer | 1 | 8 | 0 | 0 | |
| 22 | InitCoroutine | 3 | 0 | 3 | 0 | |
| 23 | Yield | 3 | 49 | 0 | 0 | |
| 24 | Copy | 5 | 10 | 0 | 2 | |
| 25 | MakeRecord | 10 | 1 | 11 | 0 | |
| 26 | NewRowid | 2 | 12 | 0 | 0 | |
| 27 | Insert | 2 | 11 | 12 | 0 | |
| 28 | Ne | 8 | 38 | 12 | 0 | |
| 29 | Null | 0 | 1 | 0 | 0 | |
| 30 | Rewind | 3 | 0 | 0 | 0 | |
| 31 | Rewind | 1 | 0 | 0 | 0 | |
| 32 | Rewind | 4 | 0 | 0 | 0 | |
| 33 | Copy | 10 | 13 | 0 | 0 | |
| 34 | Copy | 13 | 14 | 0 | 0 | |
| 35 | Copy | 13 | 15 | 0 | 0 | |
| 36 | Copy | 13 | 16 | 0 | 0 | |
| 37 | Goto | 0 | 48 | 0 | 0 | |
| 38 | Compare | 13 | 10 | 1 | k(1,B) | 0 |
| 39 | Jump | 40 | 48 | 40 | 0 | |
| 40 | Copy | 10 | 13 | 0 | 0 | |
| 41 | AggStep | 0 | 17 | 1 | percent_rank(0) | 0 |
| 42 | Next | 4 | 44 | 0 | 0 | |
| 43 | Goto | 0 | 48 | 0 | 0 | |
| 44 | Column | 4 | 0 | 17 | 0 | |
| 45 | Compare | 16 | 17 | 1 | k(1,B) | 0 |
| 46 | Jump | 47 | 41 | 47 | 0 | |
| 47 | Copy | 17 | 16 | 0 | 0 | |
| 48 | Goto | 0 | 23 | 0 | 0 | |
| 49 | Rewind | 2 | 74 | 0 | 0 | |
| 50 | AggStep | 0 | 17 | 1 | percent_rank(0) | 0 |
| 51 | Next | 4 | 53 | 0 | 0 | |
| 52 | Goto | 0 | 57 | 0 | 0 | |
| 53 | Column | 4 | 0 | 17 | 0 | |
| 54 | Compare | 16 | 17 | 1 | k(1,B) | 0 |
| 55 | Jump | 56 | 50 | 56 | 0 | |
| 56 | Copy | 17 | 16 | 0 | 0 | |
| 57 | AggValue | 1 | 0 | 2 | percent_rank(0) | 0 |
| 58 | Gosub | 9 | 76 | 0 | 0 | |
| 59 | Next | 1 | 61 | 0 | 0 | |
| 60 | Goto | 0 | 74 | 0 | 0 | |
| 61 | Column | 1 | 0 | 17 | 0 | |
| 62 | Compare | 15 | 17 | 1 | k(1,B) | 0 |
| 63 | Jump | 64 | 58 | 64 | 0 | |
| 64 | Copy | 17 | 15 | 0 | 0 | |
| 65 | AggInverse | 1 | 17 | 1 | percent_rank(0) | 0 |
| 66 | Delete | 3 | 0 | 0 | 2 | |
| 67 | Next | 3 | 69 | 0 | 0 | |
| 68 | Goto | 0 | 73 | 0 | 0 | |
| 69 | Column | 3 | 0 | 17 | 0 | |
| 70 | Compare | 14 | 17 | 1 | k(1,B) | 0 |
| 71 | Jump | 72 | 65 | 72 | 0 | |
| 72 | Copy | 17 | 14 | 0 | 0 | |
| 73 | Goto | 0 | 57 | 0 | 0 | |
| 74 | ResetSorter | 1 | 0 | 0 | 0 | |
| 75 | Goto | 0 | 79 | 0 | 0 | |
| 76 | Copy | 2 | 18 | 0 | 0 | |
| 77 | ResultRow | 18 | 1 | 0 | 0 | |
| 78 | Return | 9 | 0 | 0 | 0 | |
| 79 | Halt | 0 | 0 | 0 | 0 | |
| 80 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 81 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: AggStep, AggValue, Column, Compare, Copy, Delete, EndCoroutine, Gosub, Goto, Halt, Init, InitCoroutine, Insert, Integer, Jump, MakeRecord, Ne, NewRowid, Next, Null, OpenDup, OpenEphemeral, OpenPseudo, OpenRead, ResetSorter, ResultRow, Return, Rewind, SorterData, SorterInsert, SorterNext, SorterOpen, SorterSort, Transaction, Yield.
What the SQLite documentation says
Execute the xInverse function for an aggregate. The function has P5 arguments. P4 is a pointer to the FuncDef structure that specifies the function. Register P3 is the accumulator.
The P5 arguments are taken from register P2 and its successors.
From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.