fix(8.0): column-store range queries honor exclusive bounds (lessThan/greaterThan)
getIdsForRange computed includeMin/includeMax correctly for every operator (gt→false/true, lt→true/false, between→true/true) but DROPPED them when the column store served the query — it called columnStore.rangeQuery(field,min,max) with no flags, and the column-store path was inclusive-only. So whenever a field lived in the column store, strict lessThan/greaterThan silently behaved as lte/gte. The sparse-fallback path already forwarded the flags, so this was column-store-only. Thread includeMin/includeMax end to end: - ColumnSegmentCursor: add textbook lowerBound/upperBound helpers and express binarySearchRange in terms of them. Inclusive/inclusive is byte-identical to the old impl (lowerBound(lo) == old binarySearchValue(lo).index; upperBound(hi) == old "first position after hi"). Exclusive lower advances past ALL duplicates of the boundary value; exclusive upper stops before them. - ColumnStore.rangeQuery: accept the flags, apply them in the segment cursor AND the tail-buffer linear scan (strict > / < when exclusive). A bound taken from a segment's own min/max stays inclusive — it is a real stored value. - VectorIndex/types interface + getIdsForRange call site updated. Surfaced by brainy-complete dual-bound test (year/popularity exclusive both ends → ['Express','Vue.js']; React@95 and Angular@75 correctly excluded). Added 5 column-store unit tests: exclusive lower, exclusive upper, both, duplicate boundary values, and the unflushed tail-buffer path. 1469 unit green.
This commit is contained in:
parent
d918f49287
commit
009e50681e
5 changed files with 185 additions and 37 deletions
|
|
@ -120,33 +120,71 @@ export class ColumnSegmentCursor {
|
|||
}
|
||||
|
||||
/**
|
||||
* Find the range of positions where values fall within [lo, hi].
|
||||
*
|
||||
* @param lo - Lower bound (inclusive)
|
||||
* @param hi - Upper bound (inclusive)
|
||||
* @returns Start (inclusive) and end (exclusive) positions
|
||||
* First index whose value is >= target (textbook lower_bound).
|
||||
* O(log n) over the sorted column.
|
||||
*/
|
||||
binarySearchRange(lo: number | string, hi: number | string): { startIdx: number, endIdx: number } {
|
||||
const startResult = this.binarySearchValue(lo)
|
||||
const startIdx = startResult.index
|
||||
|
||||
// Find the first position AFTER hi
|
||||
private lowerBound(target: number | string): number {
|
||||
const isString = this.header.valueType === ValueType.String
|
||||
let endLo = startIdx
|
||||
let endHi = this.values.length
|
||||
while (endLo < endHi) {
|
||||
const mid = (endLo + endHi) >>> 1
|
||||
let lo = 0
|
||||
let hi = this.values.length
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi) >>> 1
|
||||
const cmp = isString
|
||||
? compareCodePoints(String(this.values[mid]), String(hi))
|
||||
: (this.values[mid] as number) - (hi as number)
|
||||
if (cmp <= 0) {
|
||||
endLo = mid + 1
|
||||
} else {
|
||||
endHi = mid
|
||||
}
|
||||
? compareCodePoints(String(this.values[mid]), String(target))
|
||||
: (this.values[mid] as number) - (target as number)
|
||||
if (cmp < 0) lo = mid + 1
|
||||
else hi = mid
|
||||
}
|
||||
return lo
|
||||
}
|
||||
|
||||
return { startIdx, endIdx: endLo }
|
||||
/**
|
||||
* First index whose value is > target (textbook upper_bound).
|
||||
* O(log n) over the sorted column.
|
||||
*/
|
||||
private upperBound(target: number | string): number {
|
||||
const isString = this.header.valueType === ValueType.String
|
||||
let lo = 0
|
||||
let hi = this.values.length
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi) >>> 1
|
||||
const cmp = isString
|
||||
? compareCodePoints(String(this.values[mid]), String(target))
|
||||
: (this.values[mid] as number) - (target as number)
|
||||
if (cmp <= 0) lo = mid + 1
|
||||
else hi = mid
|
||||
}
|
||||
return lo
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the range of positions where values fall within the bounds.
|
||||
*
|
||||
* @param lo - Lower bound
|
||||
* @param hi - Upper bound
|
||||
* @param includeMin - Include values equal to `lo` (default: true)
|
||||
* @param includeMax - Include values equal to `hi` (default: true)
|
||||
* @returns Start (inclusive) and end (exclusive) positions. When the bounds
|
||||
* collapse to empty (e.g. exclusive bounds on adjacent values, or lo > hi),
|
||||
* startIdx >= endIdx and the caller's loop yields nothing.
|
||||
*
|
||||
* The default (inclusive/inclusive) path is byte-identical to the previous
|
||||
* implementation: lowerBound(lo) equals the old binarySearchValue(lo).index,
|
||||
* and upperBound(hi) equals the old "first position after hi" scan.
|
||||
*/
|
||||
binarySearchRange(
|
||||
lo: number | string,
|
||||
hi: number | string,
|
||||
includeMin: boolean = true,
|
||||
includeMax: boolean = true
|
||||
): { startIdx: number, endIdx: number } {
|
||||
// startIdx: skip values strictly below the lower bound. Exclusive lower also
|
||||
// skips values equal to `lo`, so it advances past the last == lo.
|
||||
const startIdx = includeMin ? this.lowerBound(lo) : this.upperBound(lo)
|
||||
// endIdx: first position to exclude. Inclusive upper keeps values == hi
|
||||
// (stop after the last == hi); exclusive upper drops them (stop at first == hi).
|
||||
const endIdx = includeMax ? this.upperBound(hi) : this.lowerBound(hi)
|
||||
return { startIdx, endIdx }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -171,12 +209,19 @@ export class ColumnSegmentCursor {
|
|||
/**
|
||||
* Get all non-tombstoned entity IDs in a value range.
|
||||
*
|
||||
* @param lo - Lower bound (inclusive)
|
||||
* @param hi - Upper bound (inclusive)
|
||||
* @param lo - Lower bound
|
||||
* @param hi - Upper bound
|
||||
* @param includeMin - Include values equal to `lo` (default: true)
|
||||
* @param includeMax - Include values equal to `hi` (default: true)
|
||||
* @returns Array of matching entity int IDs
|
||||
*/
|
||||
getEntityIdsInRange(lo: number | string, hi: number | string): number[] {
|
||||
const { startIdx, endIdx } = this.binarySearchRange(lo, hi)
|
||||
getEntityIdsInRange(
|
||||
lo: number | string,
|
||||
hi: number | string,
|
||||
includeMin: boolean = true,
|
||||
includeMax: boolean = true
|
||||
): number[] {
|
||||
const { startIdx, endIdx } = this.binarySearchRange(lo, hi, includeMin, includeMax)
|
||||
const result: number[] = []
|
||||
for (let i = startIdx; i < endIdx; i++) {
|
||||
if (!this.tombstones.has(i)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue