Parameterized scoring mismatch and subquery error
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start in postgres/src/score.rs, especially score_support(), combine_constant_queries(), and the caller around the cited lines. Reproduce both cases with the SQL in the issue, then trace parameter handling and scorer context propagation; done means parameterized and literal queries produce matching scores and the subquery aggregate no longer raises the context error.
Written by the indexing model from the issue text.
Description
Ran into a couple issues with using lead for running tests versus actual hosted tin.
Environment: Lead 1.0.3, commit bd95c7e, PostgreSQL 18.6.
Using parameters instead of identical literals changes relevance scores.
Reproduction:
CREATE TEMP TABLE lead_score_repro (
id integer PRIMARY KEY,
title text NOT NULL
);
INSERT INTO lead_score_repro VALUES
(1, 'lorem ipsum'),
(2, 'lorem ipsun');
CREATE INDEX ON lead_score_repro USING tin (title);
ANALYZE lead_score_repro;
PREPARE lead_ranked(text, text) AS
SELECT id, tin.score(ctid) AS score
FROM lead_score_repro
WHERE title ==> $1 AND title ==> $2
ORDER BY id;
EXECUTE lead_ranked(
'lorem^4',
'(ipsum^4 OR ipsum~1^1.4 OR ipsum*^2)'
);
SELECT id, tin.score(ctid) AS score
FROM lead_score_repro
WHERE title ==> 'lorem^4'
AND title ==> '(ipsum^4 OR ipsum~1^1.4 OR ipsum*^2)'
ORDER BY id;
This gives the following:
| Query | ID 1 score | ID 2 score |
|---|---|---|
| Parameters | 0.72928625 | 0.72928625 |
| Literals | 4.472281 | 0.72928625 |
I believe this is due to the first-query fallback in score_support() that causes this. combine_constant_queries() requires Const nodes and returns None for parameters. The caller then uses first_query, discarding the remaining expressions from scoring:
let combined_query = combine_constant_queries(&same_expression)
.unwrap_or_else(|| pg_sys::copyObjectImpl(first_query.cast()).cast());
In EXPLAIN (VERBOSE, COSTS OFF), I see score_bound(title, $1, ...) for the parameterized query. For the literal query, I see both search expressions in the scorer's query argument.
Aggregating scores through a subquery raises a scoring-context error
CREATE TEMP TABLE lead_score_repro (
id integer PRIMARY KEY,
title text NOT NULL
);
INSERT INTO lead_score_repro VALUES
(1, 'lorem ipsum'),
(2, 'lorem ipsun');
CREATE INDEX ON lead_score_repro USING tin (title);
ANALYZE lead_score_repro;
-- Succeeds.
SELECT max(tin.score(ctid)) AS score
FROM lead_score_repro
WHERE title ==> 'lorem^4';
-- Fails.
SELECT max(score) AS score
FROM (
SELECT tin.score(ctid) AS score
FROM lead_score_repro
WHERE title ==> 'lorem^4'
) AS matches;
For the direct aggregate, I get 0.72928625. For the subquery aggregate, I get:
ERROR: tin.score() requires a tin index scan and cannot be used in this query context
SQLSTATE: XX000
- Dominant language
- Rust
- Stars
- 137
- Forks
- 8
- Avg merge
- 3h 16m
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from planetscale/lead
-
Docker image Open
Difficulty 3/5 1-2 days Newbie friendliness 58/100
planetscale/lead#12 ·
All issues in planetscale/lead
Similar issues
-
risk:low runtime status:in-progress type:test
Difficulty 1/5 Under an hour Newbie friendliness 92/100
zeroclaw-labs/zeroclaw#11023 ·
-
good first issue refactor
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·