Skip to content

Fix combined title+author API query producing invalid SQL (#268)#271

Open
notartom wants to merge 1 commit into
LibriVox:masterfrom
notartom:issue-268
Open

Fix combined title+author API query producing invalid SQL (#268)#271
notartom wants to merge 1 commit into
LibriVox:masterfrom
notartom:issue-268

Conversation

@notartom

@notartom notartom commented Feb 7, 2026

Copy link
Copy Markdown
Member

Previously, _build_author_project_id_list() and
_build_genre_project_id_list() ran their subqueries after the main
query's WHERE clauses (e.g. p.title) had already been added to
CodeIgniter's shared query builder. Because the builder is stateful
and accumulates WHERE clauses until a ->get() consumes them, clauses
referencing the 'p' alias leaked into the author/genre subqueries,
which query different tables without a 'p' alias, producing invalid
SQL. Additionally, _build_author_project_id_list() could return 0
instead of an empty array when no authors matched, which
where_in() would not handle correctly.

With this patch, both subqueries are moved to the top of
_build_data_set(), before any main query WHERE clauses are added to
the builder, so the builder is still clean when they execute. Both
helper methods now also always return a results array (never 0),
making the subsequent empty() guard and where_in() behave correctly
in all cases. Together, these changes fix both underlying root
causes: the WHERE clause leaking into the subqueries, and the
potential return of 0 being unhandled.

A regression test is included that requests the API with both title
and author params and asserts a 200 response.

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

@notartom notartom force-pushed the issue-268 branch 7 times, most recently from f99a0e3 to 3cd02f8 Compare February 7, 2026 20:49
@notartom notartom changed the title Add reproducer test for issue #268 Add reproducer test and fix for issue #268 Feb 7, 2026
Previously, _build_author_project_id_list() and
_build_genre_project_id_list() ran their subqueries after the main
query's WHERE clauses (e.g. p.title) had already been added to
CodeIgniter's shared query builder. Because the builder is stateful
and accumulates WHERE clauses until a ->get() consumes them, clauses
referencing the 'p' alias leaked into the author/genre subqueries,
which query different tables without a 'p' alias, producing invalid
SQL. Additionally, _build_author_project_id_list() could return 0
instead of an empty array when no authors matched, which
where_in() would not handle correctly.

With this patch, both subqueries are moved to the top of
_build_data_set(), before any main query WHERE clauses are added to
the builder, so the builder is still clean when they execute. Both
helper methods now also always return a results array (never 0),
making the subsequent empty() guard and where_in() behave correctly
in all cases. Together, these changes fix both underlying root
causes: the WHERE clause leaking into the subqueries, and the
potential return of 0 being unhandled.

A regression test is included that requests the API with both title
and author params and asserts a 200 response.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@notartom notartom changed the title Add reproducer test and fix for issue #268 Fix combined title+author API query producing invalid SQL (#268) Feb 14, 2026
@notartom

Copy link
Copy Markdown
Member Author

@redrun45 thoughts? Claude Code could be controversial here. I wanted to play around with it to see what it can do - and it's genuinely impressive as an accelerator if you know what you're doing in the first place, environmental and intellectual property concerns aside.

Comment on lines +135 to 138
if (!empty($author_project_id_list))
{
$project_id_list = $this->_build_author_project_id_list($params['author']);
$this->db->where_in('p.id', $project_id_list);
$this->db->where_in('p.id', $author_project_id_list);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I can see the appeal... but I also have questions. Those other concerns you mentioned would be worth talking to more folks about, but here's what I see in this PR. I don't know whether I would have made the same mistake, but I do know there is at least one.

We have our if block, with two statements in it. We find out we need to move the _build_..._list() call so that it's earlier in the process of building the query. Is there a reason we can't move the where_in() call up there, too? Have we checked?

If we do need to have them in different places, then we don't get the benefit of seeing them all together, so we need to be very careful about the way their conditions overlap (or don't). Is there a reason we switched this original condition from checking whether the author search parameter is empty, to checking whether the list of authors matching the search is empty?

On live, if we search by author name, and that author doesn't exist, then we will get no results. With this PR as written, we would instead get results from any author, as if we were not searching by author name at all.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I didn't just tell Claude "fix the bug" and push the PR. There were iterations and guidance along the way, with me trying to understand and double check what it did. So any mistakes are my responsibility, not AI's ;)

In terms of having them in separate places... I genuinely don't know if there's a better solution. This is such messy spaghetti code that it's hard to follow all the branches and the WHEREs and the LIKEs. Assuming we do need them in separate places, you raise a good point that the conditions have been subtly changed with the current proposal. If there's an author clause in the search but it comes up empty, the expected behaviour is to have the entire search turn up nothing, because there's no author match. With this proposal, we'd still run all the other clauses, and treat the search as if the author clause didn't exist.

I'll need some time to fix this, mostly because I want the tests to cover all the cases...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood - and all the kudos for doing more test cases! Often, that takes far longer than the actual code change...

I suppose there's already enough difficulty in guessing an author's thought processes, that we really should assume nothing.
If it were me writing this, I'd have defaulted to keeping things together if I could. If someone sent me this PR without any explanation, I might assume they did too, but there was a reason to deviate from that. And maybe that wouldn't be a safe assumption, AI-assistance or no. 🤔

@redrun45 redrun45 Feb 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potentially different technical avenue: these searches are essentially sub-queries - they run first, and then the main query is restricted to only the project IDs they return.

Key word being return, as it looks like they are both built and executed before the main query begins execution. If we're "leaking" WHEREs from these into the main query, that would seem to be a bug too, even if it's not causing other problems now.

Like you said... spaghetti code. 😓

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.

2 participants