question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Window ordering when no ordering clause is specified

See original GitHub issue

Consider following query:

with test (a, b, c) as (
      (VALUES (0, 5, 50),
              (0, 3, 30),
              (0, 4, 40),
              (0, 1, 10),
              (1, 8, 80),
              (1, 7, 70)
      )
  ),
test_ordered AS (
  SELECT * FROM test ORDER BY a, b
)
SELECT
  a,
  array_agg(c) OVER(PARTITION BY a) sequence
FROM test_ordered

Can I assume that the ordering for window function be the same as the one in output of test_ordered when window ordering cluase for window function is not specified?

So the expected output always be:

0 | [10, 30, 40, 50] 0 | [10, 30, 40, 50] 0 | [10, 30, 40, 50] 0 | [10, 30, 40, 50] 1 | [70, 80] 1 | [70, 80]

This what I found in SQL spec:

If WD has no window ordering clause, then the window ordering is implementation-dependent, and all rows are peers. Although the window ordering of peer rows within a window partition is implementation-dependent, the window ordering shall be the same for all window structure descriptors that are order-equivalent. It shall also be the same for any pair of windows W1 and W2 such that W1 is the ordering window for W2.

However I didn’t find any details in presto documentation.

Edit: I refer to Athena (Presto 0.172 if I’m not wrong)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rschlusselcommented, Mar 29, 2019

If the order isn’t specified, Presto doesn’t guarantee that it will be in any particular order (and, since it’s not semantically required by your query presto could remove the initial order by entirely: https://github.com/prestodb/presto/issues/7613)

0reactions
cyprnosecommented, Apr 3, 2019

@rschlussel Sure, thank you for the answer, it was very helpful!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window Ordering - Vertica
The window order clause only specifies order within a window result set. The query can have its own ORDER BY clause outside the...
Read more >
Window function without ORDER BY - Stack Overflow
There is a window function without ORDER BY in OVER () clause. Is there a guarantee that the rows will be processed in...
Read more >
WINDOW clause - Sybase Infocenter
If an ORDER BY clause is not specified, the window starts with the first row in the partition (UNBOUNDED PRECEDING) and ends with...
Read more >
Window function syntax summary - Amazon Redshift
When no ORDER BY clause is specified, the implied frame is unbounded, equivalent to ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Read more >
OVER Clause (Transact-SQL) - SQL Server - Microsoft Learn
If it is not specified, the default order is ASC and window function will use all rows in partition. If it is specified,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found