Since Presto 314
As Martin Traverso pointed out, since Presto 314 there is a better option, the []
subscript operator. See Martin's answer.
For older versions, see below
(x1, x2, x3)
creates a row
with anonymous fields. Currently, to access individual row fields, you need to cast the value to a row with named fields:
CAST(row_value AS row(some_field field_type, other_field, field_type, ...))
In a query this can be inside max_by
or outside (doesn't matter).
Example:
presto> SELECT r.afield, r.bfield, r.cfield
-> FROM (
-> SELECT max_by(CAST((x1, x2, x3) AS row(afield integer, bfield varchar, cfield double)), y) r
-> FROM (VALUES (1, 42, 'a', 13e0), (2, 40, 'b', 77e0)) t(y, x1, x2, x3)
-> );
afield | bfield | cfield
--------+--------+--------
40 | b | 77.0
(1 row)
I understand this is quite verbose.
There is an issue to make this more convenient: https://github.com/prestosql/presto/issues/860.