-
Notifications
You must be signed in to change notification settings - Fork 470
Add union query support #2146
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
seladb
wants to merge
21
commits into
tortoise:develop
Choose a base branch
from
seladb:add-union
base: develop
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.
+494
−4
Open
Add union query support #2146
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ee4ca6a
Merge from tortoise/develop
seladb 483f720
Add test and some fixes
seladb 7d761b8
Fix union count
seladb 1484a9b
Add documentation and changelog
seladb 808c302
Fix checks
seladb 3e4dee7
Fix count query
seladb 9080bfb
Another fix for count query
seladb 18dfd14
Small fix
seladb 852faa0
Add debug SQL
seladb da59410
Fixed test for MSSql
seladb e96065b
Fix count query
seladb 7618481
Cleanup
seladb ba5973e
Remove unused variable
seladb 8f6a104
Address PR comments
seladb d64f5b7
Merge branch 'develop' into add-union
seladb 709ddfa
Raise a proper exception when qs uses annotations
seladb dbbed50
Add offset support
seladb a1accfd
Exclude MS-SQL from offset tests
seladb 211bbfe
Fix UnionCountQuery to properly build union query before counting
seladb 7c1225a
Fix style
seladb f8dd7c8
Trigger CI
seladb 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
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
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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| NotExistOrMultiple, | ||
| ParamsError, | ||
| ) | ||
| from tortoise.expressions import F, RawSQL, Subquery | ||
| from tortoise.expressions import F, RawSQL, Subquery, Value | ||
| from tortoise.functions import Avg | ||
|
|
||
| # TODO: Test the many exceptions in QuerySet | ||
|
|
@@ -901,3 +901,218 @@ def test_multiple_objects_returned(): | |
| exp_cls: type[NotExistOrMultiple] = MultipleObjectsReturned | ||
| assert str(exp_cls("old format")) == "old format" | ||
| assert str(exp_cls(Tournament)) == exp_cls.TEMPLATE.format(Tournament.__name__) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_basic(db): | ||
| t1 = await Tournament.create(name="T1") | ||
| t2 = await Tournament.create(name="T2") | ||
| t3 = await Tournament.create(name="T3") | ||
| await Tournament.create(name="T4") | ||
|
|
||
| qs1 = Tournament.filter(name__in=["T1", "T2"]) | ||
| qs2 = Tournament.filter(name="T3") | ||
|
|
||
| result = await qs1.union(qs2) | ||
| assert set(result) == {t1, t2, t3} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_all(db): | ||
| t1 = await Tournament.create(name="T1") | ||
| await Tournament.create(name="T2") | ||
|
|
||
| qs1 = Tournament.filter(name="T1") | ||
| qs2 = Tournament.filter(name="T1") | ||
|
|
||
| result = await qs1.union(qs2, all=True) | ||
| assert list(result) == [t1, t1] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_mixed_models(db): | ||
| r1 = await Reporter.create(name="R1") | ||
| r2 = await Reporter.create(name="R2") | ||
| await Reporter.create(name="R3") | ||
| t1 = await Tournament.create(name="T1") | ||
| await Tournament.create(name="T2") | ||
|
|
||
| qs1 = Tournament.filter(name="T1").only("id", "name") | ||
| qs2 = Reporter.filter(name__in=["R1", "R2"]).only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2) | ||
| assert set(result) == {t1, r1, r2} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "orderings,expected_instances", | ||
| [ | ||
| ("name", ["t2", "t1", "r1"]), | ||
| ("-name", ["r1", "t1", "t2"]), | ||
| ], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_union_order_by(db, orderings, expected_instances): | ||
| t1 = await Tournament.create(name="C") | ||
| await Reporter.create(name="A") | ||
| t2 = await Tournament.create(name="B") | ||
| await Reporter.create(name="D") | ||
| await Tournament.create(name="E") | ||
| r1 = await Reporter.create(name="F") | ||
|
|
||
| qs1 = Tournament.filter(id__in=[t1.id, t2.id]).only("id", "name") | ||
| qs2 = Reporter.filter(id=r1.id).only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2).order_by(*orderings.split(",")) | ||
|
|
||
| instance_map = {"t1": t1, "t2": t2, "r1": r1} | ||
| expected = [instance_map[k] for k in expected_instances] | ||
|
|
||
| assert result == expected | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_order_by_multiple_fields(db): | ||
| t1 = await Tournament.create(name="C") | ||
| t2 = await Tournament.create(name="B") | ||
| r1 = await Reporter.create(name="C") | ||
| await Tournament.create(name="Z") | ||
| await Reporter.create(name="Z") | ||
|
|
||
| qs1 = Tournament.filter(id__in=[t1.id, t2.id]).only("id", "name") | ||
| qs2 = Reporter.filter(id=r1.id).only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2).order_by("name", "id") | ||
|
|
||
| if r1.id == t1.id: | ||
| return | ||
|
|
||
| if r1.id > t1.id: | ||
| expected = [t2, t1, r1] | ||
| else: | ||
| expected = [t2, r1, t1] | ||
|
|
||
| assert result == expected | ||
|
|
||
|
|
||
| @requireCapability(dialect=NotEQ("mssql")) | ||
| @pytest.mark.asyncio | ||
| async def test_union_limit(db): | ||
| r1 = await Reporter.create(name="B") | ||
| t1 = await Tournament.create(name="A") | ||
| await Reporter.create(name="D") | ||
| await Tournament.create(name="C") | ||
|
|
||
| qs1 = Tournament.all().only("id", "name") | ||
| qs2 = Reporter.all().only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2).order_by("name").limit(2) | ||
| assert list(result) == [t1, r1] | ||
|
|
||
|
|
||
| @requireCapability(dialect=NotEQ("mssql")) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto: #2146 (comment) |
||
| @pytest.mark.asyncio | ||
| async def test_union_offset(db): | ||
| await Tournament.create(name="T1") | ||
| await Tournament.create(name="T2") | ||
| t3 = await Tournament.create(name="T3") | ||
| t4 = await Tournament.create(name="T4") | ||
|
|
||
| qs1 = Tournament.filter(name__in=["T1", "T2"]).only("id", "name") | ||
| qs2 = Tournament.filter(name__in=["T3", "T4"]).only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2).order_by("name").limit(4).offset(2) | ||
| assert list(result) == [t3, t4] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_offset_negative_raises(db): | ||
| qs1 = Tournament.all().only("id", "name") | ||
| qs2 = Tournament.all().only("id", "name") | ||
|
|
||
| with pytest.raises(ParamsError, match="Offset should be non-negative number"): | ||
| await qs1.union(qs2).offset(-1) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_chained(db): | ||
| t1 = await Tournament.create(name="T1") | ||
| t2 = await Tournament.create(name="T2") | ||
| await Tournament.create(name="T3") | ||
| r1 = await Reporter.create(name="R1") | ||
| await Reporter.create(name="R2") | ||
|
|
||
| qs1 = Tournament.filter(name="T1").only("id", "name") | ||
| qs2 = Tournament.filter(name="T2").only("id", "name") | ||
| qs3 = Reporter.filter(name="R1").only("id", "name") | ||
|
|
||
| result = await qs1.union(qs2).union(qs3) | ||
| assert set(result) == {t1, t2, r1} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_count(db): | ||
| await Tournament.create(name="T1") | ||
| await Reporter.create(name="R1") | ||
| await Tournament.create(name="T2") | ||
| await Reporter.create(name="R2") | ||
|
|
||
| qs1 = Tournament.filter(name="T1").only("id") | ||
| qs2 = Reporter.filter(name="R1").only("id") | ||
|
|
||
| assert await qs1.union(qs2).count() == 2 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_different_select_fields_raises(db): | ||
| await Tournament.create(name="T1") | ||
|
|
||
| qs1 = Tournament.filter(name="T1").only("name") | ||
| qs2 = Tournament.filter(name="T1").only("desc") | ||
|
|
||
| with pytest.raises(ParamsError, match="Union queries must have the same select fields"): | ||
| await qs1.union(qs2) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_different_fields__in_different_models_raises(db): | ||
| await Tournament.create(name="T1") | ||
| await Reporter.create(name="R1") | ||
|
|
||
| qs1 = Tournament.all() | ||
| qs2 = Reporter.all() | ||
|
|
||
| with pytest.raises(ParamsError, match="Union queries must have the same select fields"): | ||
| await qs1.union(qs2) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_order_by_field_not_in_select_raises(db): | ||
| await Tournament.create(name="T1") | ||
|
|
||
| qs1 = Tournament.filter(name="T1").only("id", "name") | ||
| qs2 = Tournament.filter(name="T1").only("id", "name") | ||
|
|
||
| qs = qs1.union(qs2) | ||
| with pytest.raises(ParamsError, match="Order by field must be in the select list"): | ||
| await qs.order_by("desc") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_union_with_annotate_raises(db): | ||
| await Tournament.create(name="T1") | ||
| await Reporter.create(name="R1") | ||
|
|
||
| qs1 = ( | ||
| Tournament.filter(name="T1") | ||
| .annotate(annotated_value=Value(1)) | ||
| .only("id", "name", "annotated_value") | ||
| ) | ||
| qs2 = ( | ||
| Reporter.filter(name="R1") | ||
| .annotate(annotated_value=Value(1)) | ||
| .only("id", "name", "annotated_value") | ||
| ) | ||
|
|
||
| with pytest.raises(ParamsError, match="Union queries do not support annotations"): | ||
| await qs1.union(qs2) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
MSSql limit syntax is different but it's not currently supported in
_SetOperationwhich has its own implementation of_limit_sql()instead of using the dialect SQL. I guess we can leave it as a TODO for laterhttps://github.com/tortoise/pypika-tortoise/blob/378f6145f7529d88fa58510851d80454b742406e/pypika_tortoise/queries.py#L672