Skip to content

fix: queryMany returns [] instead of [[]] for a single query with zero matches - #70

Merged
CahidArda merged 2 commits into
upstash:masterfrom
OsamaAnsar:fix/query-many-zero-match-shape
Nov 19, 2026
Merged

CahidArda merged 2 commits into
upstash:masterfrom
OsamaAnsar:fix/query-many-zero-match-shape

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

Summary

QueryManyCommand.exec() normalizes the API's flat single-query response into the documented QueryResult[][] shape, but the normalization is skipped when the single query has zero matches - returning [] instead of [[]].

Root cause

if (this.queryCount === 1 && result.length > 0 && !Array.isArray(result[0])) {
  return [result as unknown as QueryResult<TMetadata>[]];
}

This is exactly the case #66 (fixing #38) was written to handle: a single query sent via queryMany() gets a flat array back from the API instead of a nested one, and this code wraps it. But when that single query matches nothing, the API returns [] (a flat empty array) - result.length > 0 is false, the wrapping branch is skipped, and the method returns [] instead of [[]]. This breaks the QueryResult<TMetadata>[][] return type contract for exactly the case the fix exists for, and a caller doing results[0] to read the first (only) query's matches gets undefined instead of [].

result[0] is already undefined (not an array) when result is empty, so !Array.isArray(result[0]) alone is sufficient to detect the flat shape - the result.length > 0 guard was unnecessary, and it happened to exclude the one case that needed the wrap most.

Fix

Drop the result.length > 0 guard; !Array.isArray(result[0]) already correctly identifies the flat shape in both the non-empty and empty cases.

Tests

index.test.ts's existing coverage is integration-style (a real Upstash Vector index via live credentials), which can't cheaply construct a guaranteed-zero-match single query. Added index.unit.test.ts with a mocked Requester to test the normalization logic directly:

  • flat non-empty result -> wrapped (existing behavior, still correct)
  • flat empty result (zero matches) -> wrapped into [[]] (the bug this PR fixes)
  • already-nested multi-query result -> passed through unchanged
  • multi-query zero-matches ([[], []]) -> passed through unchanged

Verified the new zero-match test actually catches the bug: reverted the fix locally and confirmed that test fails (expected [[]], received []) while the other three still pass; restored the fix and all four pass.

Test plan

  • bun test src/commands/client/query/query-many/index.unit.test.ts - 4/4 passing
  • Confirmed the added test fails without the fix and passes with it
  • bun run lint - no new errors introduced (pre-existing unrelated errors in examples/nextjs and test-utils.ts confirmed present on main before this change too)
  • prettier --check clean
  • Pre-push hook's full tsup build succeeds

…o matches

QueryManyCommand.exec() normalizes the API's flat single-query response
into the documented QueryResult[][] shape by checking `result.length > 0
&& !Array.isArray(result[0])`. When the single query has zero matches, the
API returns [] (a flat empty array), so `result.length > 0` is false and
the wrapping branch is skipped entirely - the method returns [] instead of
[[]], breaking the type contract and making `results[0]` undefined instead
of an empty array for a caller reading the first query's matches.

This is exactly the case PR upstash#66 (fixing upstash#38) was written to handle, just
missed at the empty-array boundary: `result[0]` is already `undefined`
(not an array) when result is empty, so `!Array.isArray(result[0])` alone
is sufficient - the `result.length > 0` guard was unnecessary and
excluded the one case that needed it most.

Added index.unit.test.ts with a mocked Requester to cover this directly,
including a check that the added test fails without the fix and passes
with it. The existing index.test.ts integration tests need a live index
and can't cheaply construct a guaranteed-zero-match single query, which is
why this is a separate mocked unit test rather than an addition there.
CahidArda
CahidArda previously approved these changes Nov 19, 2026
…xed batches

When the namespace does not exist the API answers with a single empty
array for any number of queries, so queryMany returned [] instead of one
empty array per query. Fill the result to the number of queries.

Data queries and vector queries are served by different endpoints. A
batch with both was sent to the data endpoint and failed there; fail
early with a clear error instead. A data query with an empty string is
now routed as a data query.
@CahidArda
CahidArda merged commit 69d0426 into upstash:master Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants