Skip to content

Commit 1f0b756

Browse files
committed
Do not clobber SQLite's default cost estimates in BestIndex
1 parent 0cfec60 commit 1f0b756

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

sqlite3_opt_vtable.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,22 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
482482
if res.AlreadyOrdered {
483483
info.orderByConsumed = C.int(1)
484484
}
485-
info.estimatedCost = C.double(res.EstimatedCost)
486-
info.estimatedRows = C.sqlite3_int64(res.EstimatedRows)
485+
// SQLite pre-initializes estimatedCost and estimatedRows with sensible
486+
// defaults; overwriting them with the Go zero value would make every
487+
// candidate plan look free and break query planning, so only pass
488+
// values the implementation actually set.
489+
if res.EstimatedCost > 0 {
490+
info.estimatedCost = C.double(res.EstimatedCost)
491+
}
492+
if res.EstimatedRows > 0 {
493+
var rows int64
494+
if res.EstimatedRows >= float64(math.MaxInt64) {
495+
rows = math.MaxInt64
496+
} else {
497+
rows = int64(res.EstimatedRows)
498+
}
499+
info.estimatedRows = C.sqlite3_int64(rows)
500+
}
487501

488502
return nil
489503
}

0 commit comments

Comments
 (0)