Commit f60384b
fix: use single-column comparator for BTree index in multi-column orderBy (#1401)
* fix: use single-column comparator for BTree index in multi-column orderBy
When a query has multiple orderBy columns (e.g. `.orderBy(createdAt, 'desc').orderBy(id, 'desc')`), the order-by compiler creates a multi-column comparator that expects array values. Previously, `ensureIndexForField` received this multi-column comparator to create a single-column BTree index on the first field.
The BTree stored individual field values (numbers), but the multi-column comparator treated them as arrays — indexing into a number returns `undefined`, so all values compared as NaN/equal. This collapsed the entire BTree to a single entry, causing `takeFromStart()` to return at most 1 key. Any live query subscription created after data was already in the collection would see 0 results.
The fix passes `makeComparator(compareOpts)` — a proper single-column comparator built from the first orderBy column's compare options — instead of the multi-column `compare` function.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
* ci: apply automated fixes
* fix: type error in test — import DEFAULT_COMPARE_OPTIONS from utils
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
* test: move index tests to deterministic-ordering.test.ts
Move the single-column comparator and multi-column orderBy tests into
the existing BTreeIndex describe block in deterministic-ordering.test.ts
rather than keeping them in a separate file.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
* ci: apply automated fixes
* chore: add changeset for BTree index comparator fix
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>1 parent a6eb289 commit f60384b
3 files changed
Lines changed: 106 additions & 1 deletion
File tree
- .changeset
- packages/db
- src/query/compiler
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
161 | 168 | | |
162 | 169 | | |
163 | 170 | | |
164 | 171 | | |
165 | 172 | | |
166 | | - | |
| 173 | + | |
167 | 174 | | |
168 | 175 | | |
169 | 176 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
| 11 | + | |
7 | 12 | | |
8 | 13 | | |
9 | 14 | | |
| |||
216 | 221 | | |
217 | 222 | | |
218 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
219 | 312 | | |
220 | 313 | | |
221 | 314 | | |
| |||
0 commit comments