-
Notifications
You must be signed in to change notification settings - Fork 290
fix: reject negative score indices in formula variables #1341
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
joein
merged 2 commits into
qdrant:dev
from
shashvat-singham:fix/formula-negative-score-index
Sep 11, 2026
+69
−35
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import re | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from qdrant_client.hybrid.formula import ( | ||
| evaluate_variable, | ||
| parse_variable, | ||
| try_extract_payload_value, | ||
| ) | ||
|
|
||
|
|
||
| def test_parse_variable_score_index() -> None: | ||
| assert parse_variable("$score") == 0 | ||
| assert parse_variable("$score[0]") == 0 | ||
| assert parse_variable("$score[1]") == 1 | ||
| assert parse_variable("$score[2]") == 2 | ||
| assert parse_variable("$score[10]") == 10 | ||
|
|
||
| # qdrant core resolves the index through the json path grammar into a usize, so anything | ||
| # that is not a plain run of ascii digits is not a valid score pattern | ||
| for var in ( | ||
| "$score[-1]", | ||
| "$score[+1]", | ||
| "$score[1_0]", | ||
| "$score[ 1 ]", | ||
| "$score[²]", | ||
| "$score[invalid]", | ||
| "$score[]", | ||
| "$score[1][2]", | ||
| "$score[10].other", | ||
| "$score.invalid", | ||
| ): | ||
| with pytest.raises(ValueError, match=f"Invalid score pattern: {re.escape(var)}"): | ||
| parse_variable(var) | ||
|
|
||
|
|
||
| def test_evaluate_variable_rejects_negative_score_index() -> None: | ||
| scores = [{1: 10.0}, {1: 20.0}, {1: 30.0}] | ||
|
|
||
| assert evaluate_variable("$score[0]", 1, scores, {}, {}) == 10.0 | ||
| assert evaluate_variable("$score[2]", 1, scores, {}, {}) == 30.0 | ||
| # an index past the end falls back to the default score | ||
| assert evaluate_variable("$score[3]", 1, scores, {}, {}) == 0.0 | ||
|
|
||
| # a negative index must not wrap around to the last prefetch, nor leak an IndexError | ||
| with pytest.raises(ValueError): | ||
| evaluate_variable("$score[-1]", 1, scores, {}, {}) | ||
| with pytest.raises(ValueError): | ||
| evaluate_variable("$score[-9]", 1, scores, {}, {}) | ||
|
|
||
|
|
||
| def test_try_extract_payload_value() -> None: | ||
| for payload_value, expected in [(1.2, 1.2), ([1.2], 1.2), ([1.2, 2.3], [1.2, 2.3])]: | ||
| empty_defaults: dict[str, Any] = {} | ||
|
|
||
| payload = {"key": payload_value} | ||
| assert try_extract_payload_value("key", payload, empty_defaults) == expected | ||
|
|
||
| defaults = {"key": payload_value} | ||
| empty_payload: dict[str, Any] = {} | ||
| assert try_extract_payload_value("key", empty_payload, defaults) == expected | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge qdrant/qdrant-client /tmp/coderabbit-repo-knowledge/qdrant-qdrant-client-6240b70e/learningsLength of output: 1366
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 14961
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 50376
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 1180
🌐 Web query:
Qdrant core JSON path grammar usize score formula $score index💡 Result:
In Qdrant, JSON path grammar and formula scoring are distinct components utilized for data retrieval and custom result re-ranking, respectively [1][2]. JSON Path Grammar Qdrant uses a specialized JSON path implementation to navigate and manipulate structured payload data [1][3]. - Grammar: Paths are defined as a series of keys and array indices [4]. They support dot-notation for keys (e.g., a.b) and bracket-notation for array access (e.g.,, [4]) [4]. - Advanced Features: The system supports quoted paths to escape special characters (e.g., foo."bar.baz".qux) and wildcard indexing for array traversal [4][1]. - Implementation: Parsed paths are represented by a internal JsonPath structure, which validates strings upon input to ensure they conform to the expected format, rejecting invalid or empty paths [1][5]. Formula Score Formula and Indexing The Query API allows for custom score re-ranking using a formula-based syntax [2][6]. - Formula Syntax: Formulas are composed of expressions (such as sum, mult, div, and built-in functions like exp_decay) that operate on prefetch scores and payload fields [2][6][7]. - $score Indexing: Within a formula, the reserved keyword $score refers to the primary result score from a prefetch operation [2][8]. - Score Indexing: Users can access specific prefetch results using an index in the $score variable (e.g., $score refers to the first prefetch, $score[4] to the second) [8]. If no index is provided, it defaults to $score [8]. - Internals: The formula engine parses these expressions into a typed structure [8]. During execution, the system evaluates the formula per point by retrieving the corresponding payload values and indexed prefetch scores [9]. If a requested score or field is missing, the system utilizes default values [9]. Formula results must ultimately resolve to finite numerical values for sorting, which is performed in descending order [2][9].
Citations:
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 5807
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 12107
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 3448
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 9216
🏁 Script executed:
Repository: qdrant/qdrant-client
Length of output: 3120
Match score-index parsing to Qdrant’s
usizecontract.parse_variableaccepts any ASCII digit string and converts it with Pythonint. Qdrant parses the same index withstr::parse::<usize>(). Therefore,$score[18446744073709551616]is accepted locally, andevaluate_variabletreats it as an out-of-range prefetch and returnsDEFAULT_SCOREwhen no default exists. Qdrant core rejects the overflow.Add boundary tests for the maximum
usizevalue and the first value above it. Reject values above theusizelimit inparse_variable.🤖 Prompt for AI Agents