I have a fairly simple Presto query, that is not ORDERing by the column I specified:
(SELECT
tag_monitor_domains.property_name,
count(*) as HourCount
FROM pageviews
INNER JOIN tag_monitor_domains
ON pageviews.property_id = CAST(tag_monitor_domains.property_id AS varchar)
WHERE FROM_UNIXTIME(pageviews.time) > date_add('month', -1, CURRENT_DATE)
AND FROM_UNIXTIME(pageviews.time) < date_add('hour', -0, CURRENT_TIMESTAMP)
GROUP BY 1
ORDER BY 1 DESC)
but the results are NOT ordered by property_name, the rows are random.
Thanks @DShultz for the report. This is indeed the case and I reported https://github.com/trinodb/trino/issues/6008 for this. Let's continue there discussion whether this is desired or faulty behavior.
As a workaround.... well, remove parenthesis. But this you already know.
The more general reason for why this is the case -- Presto ignores ORDER BY where it doesn't change the semantics of the query (e.g. in subqueries), as governed by the SQL spec. See https://trino.io/blog/2019/06/03/redundant-order-by.html for more information.