Parameterized scoring mismatch and subquery error

Open
#11 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
postgresql, rust, sql
Domain
databases

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from planetscale/lead

All issues in planetscale/lead

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.