Skip to content

Commit 16b49ee

Browse files
committed
Harden WITHSCORES detection and reject empty scorer names
- Detect WITHSCORES mode via translated.score_alias instead of scanning args for the literal token, preventing false positives if a field is named WITHSCORES. - Reject score('') with a clear ValueError so the caller's intent is not silently lost by falling back to the default scorer.
1 parent b94cf66 commit 16b49ee

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

sql_redis/executor.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ def execute(self, sql: str, *, params: dict | None = None) -> QueryResult:
209209
rows = []
210210

211211
if translated.command == "FT.SEARCH":
212-
# Check if WITHSCORES was requested — changes response format
213-
with_scores = "WITHSCORES" in translated.args
212+
# Use the explicit score_alias signal rather than scanning args
213+
# for the literal token "WITHSCORES", which could false-positive
214+
# if a returned field happened to be named "WITHSCORES".
215+
with_scores = translated.score_alias is not None
214216
# RETURN 0 suppresses document fields (like NOCONTENT);
215217
# with WITHSCORES the reply is [count, id, score, id, score, ...]
216218
no_content = self._has_return_0(translated.args)
@@ -336,7 +338,7 @@ async def execute(self, sql: str, *, params: dict | None = None) -> QueryResult:
336338
rows = []
337339

338340
if translated.command == "FT.SEARCH":
339-
with_scores = "WITHSCORES" in translated.args
341+
with_scores = translated.score_alias is not None
340342
no_content = self._has_return_0(translated.args)
341343

342344
score_alias: str | None = None

sql_redis/parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ def _process_select_expression_inner(
476476
"score() argument must be a string scorer name "
477477
f"(e.g., 'BM25', 'TFIDF'), got {scorer_val!r}."
478478
)
479+
if not scorer_val:
480+
raise ValueError(
481+
"score() scorer name must not be empty. "
482+
"Use score() with no arguments for the default "
483+
"BM25 scorer, or pass a valid name like 'TFIDF'."
484+
)
479485
scorer = scorer_val
480486
if result.scoring is not None:
481487
raise ValueError(

0 commit comments

Comments
 (0)