Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cannot form the union of more than two tables #926

Description

@najjajm

Bug Report

Description

I have a situation where I need to take the union over multiple tables (restriction or other methods will not work in this case). Taking the union over more than two tables throws an error.

Reproducibility

Include:

  • OS (MACOS)
  • Python Version 3.8.8
  • MySQL Deployment Strategy (remote)
  • DataJoint Version 0.13.2

This is a trivial example that seems to reproduce the issue:

@schema
class Params(dj.Lookup):
    definition = """
    params_id: tinyint unsigned
    """

Params.insert([[x] for x in range(3)])

params = [Params & {'params_id': x} for x in range(3)]

params[0] + params[1] + params[2]

Error stack:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    392                         if cls is not object \
    393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
    395 
    396             return _default_pprint(obj, self, cycle)

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    698     """A pprint that just redirects to the normal repr function."""
    699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
    701     lines = output.splitlines()
    702     with p.group():

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in __repr__(self)
    527         :rtype: str
    528         """
--> 529         return super().__repr__() if config['loglevel'].lower() == 'debug' else self.preview()
    530 
    531     def preview(self, limit=None, width=None):

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in preview(self, limit, width)
    531     def preview(self, limit=None, width=None):
    532         """ :return: a string of preview of the contents of the query. """
--> 533         return preview(self, limit, width)
    534 
    535     def _repr_html_(self):

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/preview.py in preview(query_expression, limit, width)
     11     if width is None:
     12         width = config['display.width']
---> 13     tuples = rel.fetch(limit=limit + 1, format="array")
     14     has_more = len(tuples) > limit
     15     tuples = tuples[:limit]

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/fetch.py in __call__(self, offset, limit, order_by, format, as_dict, squeeze, download_path, *attrs)
    190                 ret = return_values[0] if len(attrs) == 1 else return_values
    191         else:  # fetch all attributes as a numpy.record_array or pandas.DataFrame
--> 192             cur = self._expression.cursor(
    193                 as_dict=as_dict, limit=limit, offset=offset, order_by=order_by)
    194             heading = self._expression.heading

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in cursor(self, offset, limit, order_by, as_dict)
    511         if offset and limit is None:
    512             raise DataJointError('limit is required when offset is set')
--> 513         sql = self.make_sql()
    514         if order_by is not None:
    515             sql += ' ORDER BY ' + ', '.join(order_by)

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in make_sql(self, fields)
    111             distinct="DISTINCT " if distinct else "",
    112             fields=self.heading.as_sql(fields or self.heading.names),
--> 113             from_=self.from_clause(), where=self.where_clause())
    114 
    115     # --------- query operators -----------

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in from_clause(self)
     91             self._subquery_alias_count) if isinstance(src, QueryExpression)
     92             else src for src in self.support)
---> 93         clause = next(support)
     94         for s, left in zip(support, self._left):
     95             clause += ' NATURAL{left} JOIN {clause}'.format(

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in (.0)
     88 
     89     def from_clause(self):
---> 90         support = ('(' + src.make_sql() + ') as `$%x`' % next(
     91             self._subquery_alias_count) if isinstance(src, QueryExpression)
     92             else src for src in self.support)

~/anaconda3/envs/neural-data-kit/lib/python3.8/site-packages/datajoint/expression.py in make_sql(self)
    627             fields = arg1.primary_key
    628             return "({sql1}) UNION ({sql2})".format(
--> 629                 sql1=arg1.make_sql(fields),
    630                 sql2=arg2.make_sql(fields))
    631         # with secondary attributes, use union of left join with antijoin

TypeError: make_sql() takes 1 positional argument but 2 were given

Expected Behavior

Forming the union over two elements of params works as expected with the same setup as described above.

Note: taking the union over more than two tables does work for python version 3.8.2 and datajoint version 0.12.6.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behavior

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions