Skip to content

Conversation

@timnyborg
Copy link
Contributor

It seems that while this backend can support window functions out of the box, the flag hasn't been set, so a NotSupportError is thrown (see ESSolutions/django-mssql-backend#67)

I've just tried some common uses (rank over a partition, average over a row range frame), and they worked perfectly.

@absci
Copy link
Contributor

absci commented May 20, 2021

There're few unit tests in expressions_window.tests.WindowFunctionTests failed. So I think it's better to fix those before enabling the feature.
For example:

ERROR: test_row_number_no_ordering (expressions_window.tests.WindowFunctionTests)
The row number window function computes the number based on the order
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/mssql/base.py", line 565, in execute
    return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The function 'ROW_NUMBER' must have an OVER clause with ORDER BY. (4112) (SQLExecDirectW)")

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

Traceback (most recent call last):
  File "/home/vsts/work/1/s/django/tests/expressions_window/tests.py", line 156, in test_row_number_no_ordering
    ], lambda entry: (entry.name, entry.department, entry.row_number))
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/test/testcases.py", line 1047, in assertQuerysetEqual
    items = map(transform, qs)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
    cursor.execute(sql, params)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/vsts/work/1/s/.tox/py37-django22/lib/python3.7/site-packages/mssql/base.py", line 565, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The function 'ROW_NUMBER' must have an OVER clause with ORDER BY. (4112) (SQLExecDirectW)")

@ghost
Copy link

ghost commented May 20, 2021

CLA assistant check
All CLA requirements met.

@timnyborg
Copy link
Contributor Author

timnyborg commented May 20, 2021

Thanks. I see the contribution guide now has instructions for running the test suite, so I'll take a look.

@timnyborg
Copy link
Contributor Author

Now passes the test suite. NthValue has been marked as unimplemented, and its 2 tests excluded. Window functions order by (select null) if no order_by is provided.

@absci absci merged commit c04b420 into microsoft:dev May 26, 2021
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 this pull request may close these issues.

2 participants