Repository navigation
fix(query): restricting by a union no longer raises TypeError - #1755
Open
dimitri-yatsenko wants to merge 1 commit into
Open
dimitri-yatsenko wants to merge 1 commit into
dimitri-yatsenko wants to merge 1 commit into
Conversation
Restricting by a union raised TypeError because Union.make_sql takes no field list. A union with secondary attributes restricts its right operand by its left one, so a union whose left operand is a union failed the same way when fetched or counted. make_condition now wraps a union operand in a subquery, as it already does for an aggregation or a dj.Top-limited expression. Fixes #1739
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 24, 2026 21:38
20 tasks
MilagrosMarin
approved these changes
Nov 28, 2026
MilagrosMarin
left a comment
Contributor
There was a problem hiding this comment.
Thanks @dimitri-yatsenko, this is a tight fix for #1739.
- New tests: 10 passed on MySQL and PostgreSQL. With master's
condition.pyrestored, 8 fail with theUnion.make_sql()TypeError; the 2 no-common-attribute cases pass either way, as the description says. - Full suite after merging current master: 2063 passed, 17 skipped.
- I compared
&,-anddj.Notagainst expected rows computed in Python for 7 union shapes: same-table, differing operands (NULL-padded secondaries), nested, nested with differing operands, a left-union with secondaries on both sides, primary-key-only, and empty. Restricted targets were a table, a two-level primary key, aggregations (default andexclude_nonmatching=True), and aggregations overB & u/B - u. All matched on both backends. git merge-treeis clean against #1663, #1733 and #1662.- Not from this PR:
agg - uandagg & dj.Not(u)still raiseTypeError: sequence item 0: expected str instance, bool foundwhen the union shares no attributes with the aggregation. It is the same issue noted on #1663 and also happens with a plain table in place of the union.
Wrapping the union in a subquery, the same way aggregations already are, keeps this change small and easy to reconcile with #1663.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1739
What changes
Restricting by a union raised
TypeError: Union.make_sql() takes 1 positional argument but 2 were given. This affected:A & (B + C)andA - (B + C)len(), or use as a restriction. Such a union compilesarg2 - arg1, which restricts by the left operand.make_conditionnow wraps a union operand in a subquery before building theIN (...)condition, as it already does for an aggregation or adj.Top-limited expression. Two lines change insrc/datajoint/condition.py.No change for queries that work today
Every code path changed here raised before, so no query that runs today returns a different result. A union with no attributes in common with the restricted query still takes the existing true/false shortcut. A new test covers that case and passes before and after the change.
Relation to the 2.4 fix
NOT EXISTS) and raises the minimum MySQL version.Unionimport and theisinstance(condition, (Aggregation, Union))check, with the same text as 1663.git merge-treeshows no conflicts between this branch and 1663, or between this branch andnext.NOT EXISTSform, and the expected results stay the same because the test data has no NULL keys.Tests
New
tests/integration/test_union_restriction.py, run on MySQL and PostgreSQL:len()The full suite passes on both backends.