Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 58
feat: return the distance a vector query ranked by, and let the index answer it#929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93642cd
(feat): return the distance a vector query ranked by
abnegate 23d987b
(perf): order a vector search by distance alone so the index can answ…
abnegate b3cb932
(perf): match read permissions against the row instead of joining
abnegate a07ad7a
Update src/Database/Adapter/Postgres.php
abnegate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,6 +20,8 @@ | ||
| abstract class SQL extends Adapter | ||
| { | ||
| protected const VECTOR_DISTANCE_COLUMN = '_distance'; | ||
| protected mixed $pdo; | ||
| /** | ||
| @@ -1810,18 +1812,33 @@ abstract protected function getUpsertStatement( | ||
| ): mixed; | ||
| /** | ||
| * Get vector distance calculation for ORDER BY clause | ||
| * Get the SQL expression measuring distance between a vector attribute and the query vector | ||
| * | ||
| * @param Query $query | ||
| * @param array<string, mixed> $binds | ||
| * @param string $alias | ||
| * @return string|null | ||
| */ | ||
| protected function getVectorDistanceOrder(Query $query, array &$binds, string $alias): ?string | ||
| protected function getSQLVectorDistance(Query $query, array &$binds, string $alias): ?string | ||
| { | ||
| return null; | ||
| } | ||
| /** | ||
| * Render a vector distance expression in a form safe to read back into PHP | ||
| * | ||
| * A distance is undefined for a zero vector and can overflow for a large one, so the | ||
| * expression can evaluate to NaN or infinity. Those cannot survive the trip into a PHP | ||
| * float, so the value is carried as text and interpreted during hydration. | ||
| * | ||
| * @param string $distance | ||
| * @return string | ||
| */ | ||
| protected function getSQLReadableDistance(string $distance): string | ||
| { | ||
| return $distance; | ||
| } | ||
| /** | ||
| * @param string $value | ||
| * @return string | ||
| @@ -3064,18 +3081,17 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 | ||
| $sqlWhere = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : ''; | ||
| // Add vector distance calculations to ORDER BY | ||
| $vectorOrders = []; | ||
| $vectorDistances = []; | ||
| foreach ($vectorQueries as $query) { | ||
| $vectorOrder = $this->getVectorDistanceOrder($query, $binds, $alias); | ||
| if ($vectorOrder) { | ||
| $vectorOrders[] = $vectorOrder; | ||
| $vectorDistance = $this->getSQLVectorDistance($query, $binds, $alias); | ||
| if ($vectorDistance) { | ||
| $vectorDistances[] = $vectorDistance; | ||
| } | ||
| } | ||
| if (!empty($vectorOrders)) { | ||
| if (!empty($vectorDistances)) { | ||
| // Vector orders should come first for similarity search | ||
| $orders = \array_merge($vectorOrders, $orders); | ||
| $orders = \array_merge($vectorDistances, $orders); | ||
| } | ||
| $sqlOrder = !empty($orders) ? 'ORDER BY ' . implode(', ', $orders) : ''; | ||
| @@ -3093,8 +3109,15 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 | ||
| $selections = $this->getAttributeSelections($queries); | ||
| $projection = $this->getAttributeProjection($selections, $alias); | ||
| if (!empty($vectorDistances)) { | ||
| $readable = $this->getSQLReadableDistance($vectorDistances[0]); | ||
| $projection .= ", {$readable} AS {$this->quote(static::VECTOR_DISTANCE_COLUMN)}"; | ||
| } | ||
| $sql = " | ||
| SELECT {$this->getAttributeProjection($selections, $alias)} | ||
| SELECT {$projection} | ||
| FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} | ||
| {$sqlWhere} | ||
| {$sqlOrder} | ||
| @@ -3147,6 +3170,11 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 | ||
| $results[$index]['$permissions'] = \json_decode($document['_permissions'] ?? '[]', true); | ||
| unset($results[$index]['_permissions']); | ||
| } | ||
| if (\array_key_exists(static::VECTOR_DISTANCE_COLUMN, $document)) { | ||
| $value = $document[static::VECTOR_DISTANCE_COLUMN]; | ||
| $results[$index][Database::VECTOR_DISTANCE] = \is_numeric($value) ? (float)$value : null; | ||
| unset($results[$index][static::VECTOR_DISTANCE_COLUMN]); | ||
| } | ||
greptile-apps[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| $results[$index] = new Document($results[$index]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.