{"record":{"id":"4bd266564bc3a627","repo":"clockworklabs/SpacetimeDB","slug":"too-many-elements","errorCode":null,"errorMessage":"too many elements","messagePattern":"too many elements","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/bindings-typescript/src/server/runtime.ts","lineNumber":1430,"sourceCode":"        },\n      } as PointIndex<any, any>;\n    } else {\n      // numColumns != 1\n      const isCompleteScalarKey = (range: any[]): boolean => {\n        // Preserve the point-scan path only for complete scalar keys.\n        // A complete key with a Range in the final position is still a range\n        // scan over that column with equality over the preceding prefix.\n        return (\n          range.length === numColumns &&\n          !(range[range.length - 1] instanceof Range)\n        );\n      };\n\n      const serializeRange = (\n        buffer: ResizableBuffer,\n        range: any[]\n      ): IndexScanArgs => {\n        if (range.length > numColumns) throw new TypeError('too many elements');\n\n        BINARY_WRITER.reset(buffer);\n        const writer = BINARY_WRITER;\n        const prefix_elems = range.length - 1;\n        for (let i = 0; i < prefix_elems; i++) {\n          indexSerializers[i](writer, range[i]);\n        }\n        const rstartOffset = writer.offset;\n        const term = range[range.length - 1];\n        const serializeTerm = indexSerializers[range.length - 1];\n        if (term instanceof Range) {\n          const writeBound = (bound: Bound<any>) => {\n            const tags = { included: 0, excluded: 1, unbounded: 2 };\n            writer.writeU8(tags[bound.tag]);\n            if (bound.tag !== 'unbounded') serializeTerm(writer, bound.value);\n          };\n          writeBound(term.from);\n          const rstartLen = writer.offset - rstartOffset;","sourceCodeStart":1412,"sourceCodeEnd":1448,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/bindings-typescript/src/server/runtime.ts#L1412-L1448","documentation":"For btree range scans, `serializeRange` accepts a 'range' array of up to `numColumns` equality prefix elements, optionally ending in a `Range` object for the final column. If the array is longer than the number of indexed columns it throws `too many elements` before serializing. Note the asymmetry: too FEW elements is legal (it becomes a prefix scan); too MANY never is.","triggerScenarios":"Calling a range scan on an index with N columns but passing N+1 or more elements/bounds, e.g. `idx.iter([a, b, c], ...)` on a two-column index; appending a `Range` bound plus equality values that exceed the column count; using the same bound array against indexes of different widths.","commonSituations":"Copy-pasting a scan from a wider index to a narrower one; adding equality prefixes in front of a `Range` without recounting; generic query helpers that build bound arrays dynamically from user filters.","solutions":["Trim the array to at most the index's column count: for an index on `[a, b]`, valid scans are `[a]`, `[a, b]`, `[a, Range...]`, `[Range...]` — never `[a, b, c]`.","In generic query builders, clamp or reject filter arrays longer than the index width before calling the scan.","If you need to constrain more columns than the index covers, scan the index with a prefix and filter the remaining columns on the returned rows.","Write the arity rule into the type of your helper (tuple of length <= index columns) so TypeScript catches it."],"exampleFix":"// before — index on (tenantId, createdAt)\nfor (const row of byTenantCreated.iter([tenantId, from, to])) {} // 3 elems > 2 cols\n\n// after — equality prefix + Range terminal on last column\nimport { Range } from '...' ;\nfor (const row of byTenantCreated.iter([tenantId, Range.from(from).to(to)])) {}","handlingStrategy":"validation","validationCode":"// Clamp/reject bound arrays wider than the index before scanning.\nfunction checkRangeArity(range: unknown[], indexCols: number) {\n  if (range.length > indexCols) {\n    throw new Error(`range has ${range.length} elements but index has ${indexCols} columns`);\n  }\n}","typeGuard":"type RangePrefix<N extends number> = N extends 1 ? [unknown] : N extends 2 ? [unknown] | [unknown, unknown] | [unknown, Range<unknown>] | [Range<unknown>] : unknown[];","tryCatchPattern":"try { iterate(range); } catch (e) { if (e instanceof TypeError && e.message === 'too many elements') { /* trim trailing equality elems or move filters into row predicates */ } else throw e; }","preventionTips":["Remember the rule: at most index-column-count elements, last may be a Range, shorter is a prefix scan.","Put remaining predicates on returned rows instead of overloading the bound array.","Encode the index width in the type of helper functions that build scan arguments."],"tags":["index","range-scan","btree","query","runtime","spacetimedb"],"backgroundTag":"index-range-arity-mismatch","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}