Skip to content

fix: correlate $search ranking ORDER BY to the outer row - #1717

Draft
patricebender wants to merge 4 commits into
feat/hana-search-orderbyfrom
feat/hana-search-orderby-ranking-correlation
Draft

fix: correlate $search ranking ORDER BY to the outer row#1717
patricebender wants to merge 4 commits into
feat/hana-search-orderbyfrom
feat/hana-search-orderby-ranking-correlation

Conversation

@patricebender

@patricebender patricebender commented Aug 27, 2026

Copy link
Copy Markdown
Member

The deep-search ranking ORDER BY reused the search expression without binding it to the outer query row, so the sub-select produced a key IN (key) tautology instead of a correlation. By defering the ranking ORDER BY to after infer(), where the outer alias is known, the score sub-select can be correlated to the outer row post-transform (mirroring expand's _correlate). A deep search may lead to one outer row yielding many joined child rows, so wrap the score in MAX() to keep the scalar sub-select single-valued and rank by the best-matching child.

The deep-search ranking ORDER BY reused the search expression without binding
it to the outer query row, so the sub-select produced a `key IN (key)`
tautology instead of a correlation (the TODO in cqn4sql). Defer the ranking
ORDER BY to after infer(), where the outer alias is known, and correlate the
score sub-select to the outer row post-transform (mirroring expand's
_correlate). A deep search fans one outer row out to many joined child rows,
so wrap the score in MAX() to keep the scalar sub-select single-valued and
rank by the best-matching child.

Also emit the numeric flag as the proper CQN literal `{ val: true }`, and make
the HANA fuzzy `search` renderer idempotent: it rewrites annotated columns
in place (ref -> xpr), which crashed when the same search() args are rendered
twice (WHERE predicate + injected ranking ORDER BY).

Verified end-to-end against a real HANA: deep to-many $search ranks by best
match, de-duplicated; fuzzy suite green.
The inherited `$self` composition (SearchAuthors : Authors) made the backlink
resolve to the base entity, which cds-compiler 7.0.1 (CI) rejects during the
relational SQL transform. Make the model self-contained: SearchAuthors owns its
books composition and Books.author points to SearchAuthors directly, so the
$self backlink is unambiguous. Verified compile+to.sql on 7.0.1 and the e2e
ranking still passes on a real HANA.
@patricebender
patricebender force-pushed the feat/hana-search-orderby-ranking-correlation branch from 0bd6c4d to 6083a81 Compare August 27, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant