prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Array contains elements not supported for comparison

What it means

array_sort() without a lambda uses the element type's compareTo, which is not implemented for some types. When the underlying comparison raises NOT_SUPPORTED, Presto converts it to INVALID_FUNCTION_ARGUMENT because the sort itself is the invalid operation for those element types.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArraySortFunction.java:82

            listOfPositions.sort(new Comparator<Integer>()
            {
                @Override
                public int compare(Integer p1, Integer p2)
                {
                    if (block.isNull(p1)) {
                        return block.isNull(p2) ? 0 : 1;
                    }
                    else if (block.isNull(p2)) {
                        return -1;
                    }

                    try {
                        //TODO: This could be quite slow, it should use parametric equals
                        return type.compareTo(block, p1, block, p2);
                    }
                    catch (PrestoException | NotSupportedException e) {
                        if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {
                            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Array contains elements not supported for comparison", e);
                        }
                        throw e;
                    }
                }
            });
        }
        else {
            listOfPositions.sort(new Comparator<Integer>()
            {
                @Override
                public int compare(Integer p1, Integer p2)
                {
                    try {
                        //TODO: This could be quite slow, it should use parametric equals
                        return type.compareTo(block, p1, block, p2);
                    }
                    catch (PrestoException | NotSupportedException e) {
                        if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Provide an explicit lambda comparator that defines ordering on the element's comparable fields: sort(arr, (a,b) -> ...)
  2. Extract a comparable field first, e.g. sort via transform on the scalar component
  3. Restructure data so arrays contain primitives with defined ordering
  4. Use try() to return a default (e.g. the unsorted array) when elements are non-comparable

Example fix

// before
sort(arr_of_rows)
// after
sort(arr_of_rows, (a, b) -> if(a.row_field IS NULL, -1, compare(a.row_field, b.row_field)))
Defensive patterns

Strategy: fallback

Validate before calling

-- sort only arrays of orderable element types (scalar types)
SELECT sort(arr) FROM t WHERE typeof(arr) IN ('array(integer)','array(bigint)','array(varchar)','array(double)','array(date)','array(timestamp)');

Try / catch

CASE WHEN element_type_is_orderable THEN sort(arr) ELSE arr END -- or try(sort(arr))

Prevention

When it happens

Trigger: Calling sort(array) where the array element type does not support ordering comparison via Type.compareTo (e.g. arrays of rows/maps/complex types lacking a defined order).

Common situations: Sorting arrays of ROW or MAP elements; schema changes introducing non-comparable element types; migrating queries from engines where such sorting was allowed.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/ca11ab2e67ddff83. Report an issue: GitHub.