From the PostgreSQL documentation:
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
[ * | expression [ [ AS ] output_name ] [, ...] ]
In the first line of that quoted syntax you will find that the ON portion is optional, but it is also that ON portion which references parentheses. In other words, unless the ON is present then the parentheses are meaningless.
So, for this question the [ ON ( expression [, ...] ) ] is not relevant.
Here is some very simple test data:
CREATE TABLE bar
(foo varchar(3), fub varchar(1), flut timestamp)
;
INSERT INTO bar
(foo, fub, flut)
VALUES
('one', 'a', '2016-01-01 01:01:03'),
('one', 'b', '2016-01-01 01:01:02'),
('one', 'c', '2016-01-01 01:01:01'),
('two', 'd', '2016-01-01 01:01:03'),
('two', 'e', '2016-01-01 01:01:02'),
('two', 'f', '2016-01-01 01:01:01')
;
Let us first concentrate on the parentheses. What do parentheses alone do around an expression following select? e.g.
select (foo) from bar;
| foo |
|-----|
| one |
| one |
| one |
| two |
| two |
| two |
I trust that you will see that this result is identical to a query without parentheses surrounding column foo, and so what we find from that query is that the parentheses do NOTHING. They are simply ignored. What happens however if we introduce DISTINCT?
select distinct(foo) from bar;
| foo |
|-----|
| two |
| one |
select distinct foo from bar;
| foo |
|-----|
| two |
| one |
Again, we see that the parentheses have no effect at all. If we refer back to the syntax this is consistent. DISTINCT is NOT a FUNCTION and placing an expression inside parentheses after DISTINCT does not alter the way it works.
So, for the question:
just came across a SQL query, specifically against a Postgres database, that uses a function named "distinct". Namely:
select distinct(pattern) as pattern, style, ... etc ...
from styleview
where ... etc ...
DISTINCT is NOT a function! and the parentheses in that example query are ignored.
If used the optional [ ON (expression) ] really does alter results.
Test a:
select distinct ON (foo) foo, fub, flut from bar order by foo
| foo | fub | flut |
|-----|-----|---------------------------|
| one | a | January, 01 2016 01:01:03 |
| two | d | January, 01 2016 01:01:03 |
Test b:
select distinct ON (fub) foo, fub, flut from bar order by fub
| foo | fub | flut |
|-----|-----|---------------------------|
| one | a | January, 01 2016 01:01:03 |
| one | b | January, 01 2016 01:01:02 |
| one | c | January, 01 2016 01:01:01 |
| two | d | January, 01 2016 01:01:03 |
| two | e | January, 01 2016 01:01:02 |
| two | f | January, 01 2016 01:01:01 |
Test c:
select distinct ON (flut) foo, fub, flut from bar order by flut
| foo | fub | flut |
|-----|-----|---------------------------|
| one | c | January, 01 2016 01:01:01 |
| one | b | January, 01 2016 01:01:02 |
| one | a | January, 01 2016 01:01:03 |
The [ ON (expression) ] facility is very useful as it can provide the "first", or "last", or "earliest", or "most recent" rows in a distinct list. But keep in mind that this capability is coupled with the ORDER BY clause and in fact unless the order by clause ALSO refers to expressions used by in the SELECT DISTINCT ON PostgreSQL produces an error:
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY
expressions
The examples above may be seen operating at sqlfiddle here
While I don't wish to over complicate my answer there is a wrinkle worth mentioning:
select distinct (foo,fub) from bar;
NOW the parentheses do something, but what they do has no direct relationship to distinct. See "complex types"