-
Notifications
You must be signed in to change notification settings - Fork 22
Fix combined title+author API query producing invalid SQL (#268) #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
notartom
wants to merge
1
commit into
LibriVox:master
Choose a base branch
from
notartom:issue-268
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
application/tests/controllers/api/Feed_audiobooks_test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Regression test for part of issue #268: | ||
| * https://github.com/LibriVox/librivox-catalog/issues/268 | ||
| * | ||
| * When both title and author params are provided in _build_data_set(), the | ||
| * subqueries (author, genre) must run before any main query WHERE clauses are | ||
| * added to the shared query builder, otherwise clauses like 'p.title' leak | ||
| * into subqueries that don't have a 'p' table alias, producing invalid SQL. | ||
| */ | ||
| class Feed_audiobooks_test extends TestCase { | ||
|
|
||
| /** | ||
| * @link https://github.com/LibriVox/librivox-catalog/issues/268 | ||
| */ | ||
| public function test_title_and_author_combined_does_not_error() { | ||
| $output = $this->request( | ||
| 'GET', | ||
| 'api/feed/audiobooks?format=json&title=test&author=NONEXISTENT' | ||
| ); | ||
| $this->assertResponseCode(200); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
ifblock, 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 thewhere_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.
There was a problem hiding this comment.
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 theLIKEs. 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...
There was a problem hiding this comment.
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. 🤔
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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. 😓