Skip to content
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

Missing primary filter lifting to HAVING clause #690

Closed
hrach opened this issue Oct 30, 2024 · 4 comments · Fixed by #687
Closed

Missing primary filter lifting to HAVING clause #690

hrach opened this issue Oct 30, 2024 · 4 comments · Fixed by #687
Milestone

Comments

@hrach
Copy link
Member

hrach commented Oct 30, 2024

          I am a bit confused with separate WHERE and HAVING in some cases. For example these two produce identical query, which can't be right, as it only works with AND.
$this->model->books->findBy([
    ICollection::OR,
    ['title' => 'Book 1'],
    [CompareGreaterThanFunction::class, [CountAggregateFunction::class, 'tags->id'], 0],
]);
$this->model->books->findBy([
    ICollection::AND,
    ['title' => 'Book 1'],
    [CompareGreaterThanFunction::class, [CountAggregateFunction::class, 'tags->id'], 0],
]);
SELECT "books".* FROM "books" AS "books" 
LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") 
LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") 
WHERE ((("books"."title" = 'Book 1'))) 
GROUP BY "books"."id", "books"."title" 
HAVING ((COUNT("tags__COUNT"."id") > 0));

Originally posted by @stepapo in #685 (comment)

@hrach hrach linked a pull request Oct 30, 2024 that will close this issue
@hrach
Copy link
Member Author

hrach commented Oct 30, 2024

@stepapo Thanks for the thorough testing, fix in #687

@hrach hrach added this to the v5.0 milestone Oct 30, 2024
@stepapo
Copy link

stepapo commented Oct 31, 2024

I really wish to tell you everything is ok :-D But this does not work in postgres: column "author.name" must appear in the GROUP BY. Not sure about creating new issue as it might be connected to this one, so just posting it here.

$this->model->books->findBy([
	ICollection::OR,
	['author->name' => 'Writer 1'],
	[CompareGreaterThanFunction::class, [CountAggregateFunction::class, 'tags->id'], 0],
]);

hrach added a commit that referenced this issue Oct 31, 2024
To provide more smarted SQL rewrites, we need to know if the expression
itself is in AND/OR junction and if other parts of the junction
require a HAVING clause. This is possible only after getting the full
expression tree. Then we collect the actual expressions.

[closes #690]
[closes #686]
@hrach
Copy link
Member Author

hrach commented Oct 31, 2024

@stepapo fixed, added more tests 🙏 🤞

@stepapo
Copy link

stepapo commented Oct 31, 2024

Found one more, hopefully not a big deal. Just like the first one, AND and OR produce identical query, only correct for AND.

$this->model->books->findBy([
	ICollection::OR,
	['title' => 'Book 1'],
	[ICollection::AND, new NoneAggregator, 'tags->id' => 3],
]);
$this->model->books->findBy([
	ICollection::AND,
	['title' => 'Book 1'],
	[ICollection::AND, new NoneAggregator, 'tags->id' => 3],
]);

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 a pull request may close this issue.

2 participants