HarperFast / HarperFast/harper

table.search({ sort: ... }) without an indexed condition throws "id is not indexed and not combined with any other conditions"

Open
#773 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
89
Forks
10
Avg merge
2d 2h
Merged PRs (30d)
205

Description

## Bug Summary

Iterating an entire table with a single `sort` key fails when the sort attribute happens to be the primary key (or any attribute that isn't combined with another condition):

```js
for await (const r of tables.Celebrity.search({ limit: 1, sort: { attribute: 'id' } })) ...
```

throws:

```
HdbError: id is not indexed and not combined with any other conditions
```

`Celebrity.id` *is* the `@primaryKey`, so the error message is also misleading — it's not that `id` isn't indexed, it's that the optimizer refuses to use the primary-key index for an unconditional sort.

## Expected Behaviour

Either:
1. Allow `search({ sort: { attribute: '' } })` (and any other indexed attribute) to do a pure index scan — this is what RDBMSes do for `SELECT * FROM t ORDER BY pk LIMIT 1`.
2. Improve the error message to say "sort attribute X is indexed but the optimizer requires a condition on X (e.g. `conditions: { attribute: 'X', comparator: '>', value: 0 }`) to use the index for ordering" — actually point the user at a fix.

The current message implies a missing `@indexed` decorator that the user already has.

## Repro

```js
type Celebrity @table @export {
id: ID @primaryKey
name: String @indexed
embedding: [Float] @indexed(type: "HNSW", distance: "cosine")
}

// In a resource:
async get(target) {
for await (const r of tables.Celebrity.search({ limit: 1, sort: { attribute: 'id' } })) {
console.log(r)
}
}
```

→ 404 to the caller, `HdbError: id is not indexed and not combined with any other conditions` in the resource.

## Workaround

Drop the `sort` and iterate:

```js
for await (const r of tables.Celebrity.search({})) ... // works
```

…but you lose ordering and have to accept "iteration order is unspecified".

## Priority/Impact

Low impact (everyone hits it once and switches to no-sort), but high friction for new users — the error message is actively misleading.

## Found in

`harper-celebrity-match` demo, `resources/CelebrityLookalike.js`. Came up while computing a row count for the demo page header.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.