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

distinct doesn't work as a column wrapper - no label is given #188

Closed
banuni opened this issue Jun 9, 2021 · 0 comments · Fixed by #275
Closed

distinct doesn't work as a column wrapper - no label is given #188

banuni opened this issue Jun 9, 2021 · 0 comments · Fixed by #275
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. type: question Request for information or clarification. Not an issue.

Comments

@banuni
Copy link

banuni commented Jun 9, 2021

Environment details

  • OS type and version: macOs 11.4
  • Python version: Python 3.9
  • pybigquery 0.9.0 (also tested on 0.8.0)

Steps to reproduce

make a query that uses sqlalchemy.distinct and not Query.distinct
EDIT: found out that the problem is that the former does not add an AS to the query, hence - adding a label manually is a workaround

Code example

  # this raises an error
  from sqlalchemy import distinct
  db.query(distinct(MyTable.my_column)).all()
  
  # this plays fine:
  db.query(MyTable.my_column).distinct().all()
  
  #edit: this ALSO plays nice
  from sqlalchemy import distinct
  db.query(distinct(MyTable.my_column).label('just_a_random_label').all()

Stack trace

Error on request:
Traceback (most recent call last):
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/engine/cursor.py", line 639, in _index_for_key
    rec = self._keymap[key]
KeyError: <sqlalchemy.sql.elements.UnaryExpression object at 0x12e0b9760>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/some/path/.venv/lib/python3.9/site-packages/werkzeug/serving.py", line 319, in run_wsgi
    execute(self.server.app)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/werkzeug/serving.py", line 308, in execute
    application_iter = app(environ, start_response)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/some/path/.venv/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/some/path/app/my_file.py", line 666, in my_function()
    industries = get_db_session().query(distinct(MyTable.column)).all()
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2699, in all
    return self._iter().all()
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2834, in _iter
    result = self.session.execute(
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1680, in execute
    result = compile_state_cls.orm_setup_cursor_result(
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/context.py", line 318, in orm_setup_cursor_result
    return loading.instances(result, querycontext)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 88, in instances
    cursor.close()
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 68, in instances
    *[
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 69, in <listcomp>
    query_entity.row_processor(context, cursor)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/orm/context.py", line 2597, in row_processor
    getter = result._getter(column)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 881, in _getter
    return self._metadata._getter(key, raiseerr)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 98, in _getter
    index = self._index_for_key(key, raiseerr)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/engine/cursor.py", line 641, in _index_for_key
    rec = self._key_fallback(key, ke, raiseerr)
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/engine/cursor.py", line 783, in _key_fallback
    util.raise_(
  File "/Users/some/path/.venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'DISTINCT my_table.my_column'"

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Jun 9, 2021
@banuni banuni changed the title distinct doesn't work as a column wrapper distinct doesn't work as a column wrapper - no label is given Jun 9, 2021
@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Jun 10, 2021
@tseaver tseaver added type: question Request for information or clarification. Not an issue. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Jun 15, 2021
@jimfulton jimfulton self-assigned this Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants