{"record":{"id":"ca11ab2e67ddff83","repo":"prestodb/presto","slug":"invalid-function-argument-ca11ab","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Array contains elements not supported for comparison","messagePattern":"Array contains elements not supported for comparison","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArraySortFunction.java","lineNumber":82,"sourceCode":"            listOfPositions.sort(new Comparator<Integer>()\n            {\n                @Override\n                public int compare(Integer p1, Integer p2)\n                {\n                    if (block.isNull(p1)) {\n                        return block.isNull(p2) ? 0 : 1;\n                    }\n                    else if (block.isNull(p2)) {\n                        return -1;\n                    }\n\n                    try {\n                        //TODO: This could be quite slow, it should use parametric equals\n                        return type.compareTo(block, p1, block, p2);\n                    }\n                    catch (PrestoException | NotSupportedException e) {\n                        if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {\n                            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Array contains elements not supported for comparison\", e);\n                        }\n                        throw e;\n                    }\n                }\n            });\n        }\n        else {\n            listOfPositions.sort(new Comparator<Integer>()\n            {\n                @Override\n                public int compare(Integer p1, Integer p2)\n                {\n                    try {\n                        //TODO: This could be quite slow, it should use parametric equals\n                        return type.compareTo(block, p1, block, p2);\n                    }\n                    catch (PrestoException | NotSupportedException e) {\n                        if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArraySortFunction.java#L64-L100","documentation":"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.","triggerScenarios":"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).","commonSituations":"Sorting arrays of ROW or MAP elements; schema changes introducing non-comparable element types; migrating queries from engines where such sorting was allowed.","solutions":["Provide an explicit lambda comparator that defines ordering on the element's comparable fields: sort(arr, (a,b) -> ...)","Extract a comparable field first, e.g. sort via transform on the scalar component","Restructure data so arrays contain primitives with defined ordering","Use try() to return a default (e.g. the unsorted array) when elements are non-comparable"],"exampleFix":"// before\nsort(arr_of_rows)\n// after\nsort(arr_of_rows, (a, b) -> if(a.row_field IS NULL, -1, compare(a.row_field, b.row_field)))","handlingStrategy":"fallback","validationCode":"-- sort only arrays of orderable element types (scalar types)\nSELECT sort(arr) FROM t WHERE typeof(arr) IN ('array(integer)','array(bigint)','array(varchar)','array(double)','array(date)','array(timestamp)');","typeGuard":null,"tryCatchPattern":"CASE WHEN element_type_is_orderable THEN sort(arr) ELSE arr END -- or try(sort(arr))","preventionTips":["Only call no-arg sort on arrays of primitive orderable types","Provide explicit lambda comparators for ROW/array element types","Check typeof(arr) when queries are generated dynamically","Transform complex elements to a comparable key before sorting"],"tags":["presto","sql","array-sort","type-comparison"],"backgroundTag":"element-type-not-comparable","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}