|
| 1 | +--- |
| 2 | +title: Paging CQL, and the running statement you cannot stop |
| 3 | +status: published |
| 4 | +author: |
| 5 | + name: LibreDB |
| 6 | + picture: '' |
| 7 | +slug: cassandra-cql-paging-and-cancellation |
| 8 | +description: A second page is refused with its reason rather than silently re-serving the first, and there is no cancel path at all on this engine to fall back on. |
| 9 | +coverImage: '' |
| 10 | +tags: |
| 11 | + - value: engineering |
| 12 | + label: Engineering |
| 13 | + - value: databases |
| 14 | + label: Databases |
| 15 | +publishedAt: 2026-02-26T09:00:00.000Z |
| 16 | +--- |
| 17 | + |
| 18 | +The editor bounds every SELECT before it sends it, and on Apache Cassandra the |
| 19 | +`LIMIT` / `OFFSET` pair that paging usually rests on has only its first half. |
| 20 | +`LIMIT n` is correct CQL. `OFFSET n` is not in the grammar at all, so the shared |
| 21 | +row limiter has to be overridden here in three places - and two of the three are |
| 22 | +about text rather than about rows. |
| 23 | + |
| 24 | +The statements below were measured against Apache Cassandra 5.0.9, the official |
| 25 | +`cassandra:5.0.9` image, before the provider existed. |
| 26 | + |
| 27 | +## Why there is no second page: CQL has LIMIT, and no OFFSET |
| 28 | + |
| 29 | +The shared limiter appends a bound to a statement that carries none. That part is |
| 30 | +right on CQL. Page two is where it stops: |
| 31 | + |
| 32 | +```sql |
| 33 | +SELECT id, name FROM probe.customers LIMIT 5 OFFSET 5 |
| 34 | +-- line 1:45 mismatched input 'OFFSET' expecting EOF |
| 35 | +SELECT id, name FROM probe.customers OFFSET 5 |
| 36 | +-- line 1:37 mismatched input 'OFFSET' expecting EOF |
| 37 | +``` |
| 38 | + |
| 39 | +There is no clause to write. Cassandra pages a result set on the wire: the server |
| 40 | +hands back its own cursor for the next page, which is not an offset a fresh |
| 41 | +statement can restate. A second page cannot be assembled by rewriting the text, |
| 42 | +which is the only thing a query rewriter is allowed to do. |
| 43 | + |
| 44 | +So the grid on this engine gives you a first page and nothing after it. What the |
| 45 | +request does when it arrives anyway is the part worth reading. |
| 46 | + |
| 47 | +## Refusing beats re-serving page one |
| 48 | + |
| 49 | +There were three ways to answer a page-two request, and two of them are worse in |
| 50 | +a way that is easy to miss. |
| 51 | + |
| 52 | +**Send the clause anyway.** The user gets `mismatched input 'OFFSET'` about a |
| 53 | +keyword they never typed, from a rewrite they never saw. The error is true and |
| 54 | +unactionable: nothing in it says which layer wrote the word. |
| 55 | + |
| 56 | +**Drop the clause silently.** This is the expensive one. Without `OFFSET` the |
| 57 | +statement is page one again, and the editor appends what comes back to what it is |
| 58 | +already showing. The user sees rows one through five, then rows one through five |
| 59 | +again, presented as rows six through ten. Nothing errors. That is not a missing |
| 60 | +feature, it is a wrong answer, and it is a wrong answer that looks like real data |
| 61 | +in a screenshot. |
| 62 | + |
| 63 | +**Refuse the request, with the reason.** `prepareQuery` declines a page after the |
| 64 | +first and says why, and the editor reports that instead of pretending. The |
| 65 | +refusal covers every SELECT, including one that already carries its own `LIMIT n` |
| 66 | +and is therefore never rewritten - because the failure mode being avoided is not |
| 67 | +the rewrite, it is the duplicate rows. |
| 68 | + |
| 69 | +This is the same rule the [capability declarations](/features) follow everywhere |
| 70 | +else in the product: a control that cannot work is absent with its reason |
| 71 | +attached, rather than offered and then quietly wrong. The engine row on the |
| 72 | +[databases page](/databases) states the same boundary before you connect. |
| 73 | + |
| 74 | +## Keeping the filtering clause last when a bound is added |
| 75 | + |
| 76 | +The second override is a clause-order fact. `ALLOW FILTERING` must be the last |
| 77 | +thing in a CQL SELECT: |
| 78 | + |
| 79 | +```sql |
| 80 | +SELECT * FROM probe.orders WHERE amount > 5 LIMIT 3 ALLOW FILTERING |
| 81 | +-- 3 rows |
| 82 | +SELECT * FROM probe.orders WHERE amount > 5 ALLOW FILTERING LIMIT 3 |
| 83 | +-- line 1:60 mismatched input 'LIMIT' |
| 84 | +``` |
| 85 | + |
| 86 | +The shared limiter appends, so on this dialect it would produce the second form |
| 87 | +every time. The provider transposes the two clauses instead, preserving the |
| 88 | +writer's own spacing, so the bound lands before `ALLOW FILTERING` rather than |
| 89 | +after it. |
| 90 | + |
| 91 | +The alternative was to leave a statement ending in `ALLOW FILTERING` unbounded. |
| 92 | +That is exactly backwards: a user types those two words precisely when a scan is |
| 93 | +about to happen, which is the statement that most needs a ceiling on the rows it |
| 94 | +brings back. |
| 95 | + |
| 96 | +One shape is knowingly left unbounded, and it is worth naming rather than |
| 97 | +discovering. A statement whose last clause is `PER PARTITION LIMIT n` reads as |
| 98 | +already bounded to the shared reader, so nothing is injected. `... PER PARTITION |
| 99 | +LIMIT 2 LIMIT 3` is valid CQL, but adding the second bound would mean stripping |
| 100 | +the clause the reader matched, and a rewriter that removes clauses is a rewriter |
| 101 | +that can corrupt a statement. |
| 102 | + |
| 103 | +## A statement whose rewrite would end inside a comment |
| 104 | + |
| 105 | +The third override is not about rows at all. A CQL line comment may not be |
| 106 | +closed by the end of the input: |
| 107 | + |
| 108 | +```sql |
| 109 | +SELECT * FROM probe.customers LIMIT 3 -- note |
| 110 | +-- line 1:45 mismatched character '<EOF>' expecting set null |
| 111 | +SELECT * FROM probe.customers LIMIT 3 -- note\n |
| 112 | +-- 3 rows |
| 113 | +SELECT * FROM probe.customers LIMIT 3 // note\n |
| 114 | +-- 3 rows |
| 115 | +``` |
| 116 | + |
| 117 | +Two facts are stacked there. `//` is a line comment in CQL as well as `--`; that |
| 118 | +half is a shared grammar fact, probed on Cassandra 5.0.9 and on ScyllaDB |
| 119 | +2026.2.4 rather than assumed from a neighbouring dialect. While it was a private |
| 120 | +scan inside this provider, the shared readers were blind to it, and the statement |
| 121 | +splitter cut `SELECT id FROM probe.customers // note; DROP TABLE probe.customers` |
| 122 | +- one statement to this server, with both the semicolon and the write inside the |
| 123 | +comment - into a read and a bare `DROP`. |
| 124 | + |
| 125 | +The half that stays local to Cassandra is the end-of-input rule. The limiter |
| 126 | +inserts its clause before trailing trivia, and the trim that follows drops the |
| 127 | +newline that closed the comment. A valid statement becomes a syntax error, caused |
| 128 | +entirely by the bound the user did not ask for. So a statement whose rewritten |
| 129 | +form would end inside a line comment is left exactly as written, and the result is |
| 130 | +reported as unlimited rather than as bounded. The check walks the shared span |
| 131 | +reader, which is also what keeps a `//` inside a string literal - `WHERE url = |
| 132 | +'http://x'` - from being read as the start of a comment. |
| 133 | + |
| 134 | +## No cancel frame, no kill, no driver method |
| 135 | + |
| 136 | +Everything above assumes the statement comes back. When it does not, this engine |
| 137 | +offers nothing to press. |
| 138 | + |
| 139 | +There is no query cancellation on Cassandra at all: the native protocol has no |
| 140 | +cancel frame, CQL has no `KILL` statement, and the driver publishes no cancel |
| 141 | +method - its client surface is `connect`, `execute`, `eachRow`, `stream`, |
| 142 | +`batch`, `getReplicas`, `getState`, `log` and `shutdown` - so the only bound on a |
| 143 | +running statement is a client-side read timeout. `cancelQuery()` is therefore not |
| 144 | +implemented, and because both routes detect support by the method's presence, the |
| 145 | +cancel endpoint answers *"Query cancellation is not supported for this database |
| 146 | +type"* rather than reporting a cancellation that silently did nothing. |
| 147 | + |
| 148 | +The timeout is the driver's `readTimeout`, 12000 ms by default, set from the |
| 149 | +provider's query timeout. Read what it does precisely: after it expires this |
| 150 | +client stops waiting. The coordinator carries on. The work does not stop, the |
| 151 | +watching stops. |
| 152 | + |
| 153 | +The per-statement server-side deadline is not available either. `USING TIMEOUT` |
| 154 | +is not in 5.0's grammar: `SELECT * FROM probe.orders USING TIMEOUT 1ms` answers |
| 155 | +`line 1:27 mismatched input 'USING' expecting EOF`. |
| 156 | + |
| 157 | +Monitoring does list the node's running statements, read from |
| 158 | +`system_views.queries` - thread id, task text and two microsecond readings. So you can watch a statement run, including |
| 159 | +the read that is watching it, and there is no action attached to any row in that |
| 160 | +list. Seeing is not reaching. The maintenance toolkit does not fill the gap |
| 161 | +either: compaction, flush, cleanup and repair are `nodetool` actions over JMX, so |
| 162 | +`supportsMaintenance` is false and the operations list is empty. |
| 163 | + |
| 164 | +So bound the statement in the text, restrict the partition key, and read the |
| 165 | +timeout as the thing it actually is - a limit on how long you will wait, not a |
| 166 | +limit on how long the cluster will work. |
0 commit comments