{"record":{"id":"9635d047e9d7457a","repo":"prestodb/presto","slug":"invalid-function-argument-9635d0","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"sorted_by array must contain only varchar elements, found: %s","messagePattern":"sorted_by array must contain only varchar elements, found: (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-spi/src/main/java/com/facebook/presto/spi/procedure/TableDataRewriteDistributedProcedure.java","lineNumber":159,"sourceCode":"    public interface FinishCallDistributedProcedure\n    {\n        void finish(ConnectorSession session, ConnectorProcedureContext procedureContext, ConnectorDistributedProcedureHandle procedureHandle, Collection<Slice> fragments);\n    }\n\n    public static List<String> extractSortFieldStrings(Object[] arguments, OptionalInt sortOrderIndex)\n    {\n        List<String> sortFieldStrings = Collections.emptyList();\n        if (sortOrderIndex.isPresent()) {\n            Object value = arguments[sortOrderIndex.getAsInt()];\n            if (value == null) {\n                sortFieldStrings = Collections.emptyList();\n            }\n            else if (value instanceof List<?>) {\n                try {\n                    sortFieldStrings = Collections.unmodifiableList(((List<?>) value).stream()\n                            .map(element -> {\n                                if (!(element instanceof String)) {\n                                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT,\n                                            format(\"sorted_by array must contain only varchar elements, found: %s\",\n                                                    element == null ? \"null\" : element.getClass().getSimpleName()));\n                                }\n                                return (String) element;\n                            })\n                            .collect(Collectors.toList()));\n                }\n                catch (PrestoException e) {\n                    throw e;\n                }\n                catch (ClassCastException e) {\n                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT,\n                            \"sorted_by array must contain only varchar elements\", e);\n                }\n            }\n            else {\n                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"sorted_by must be an array(varchar)\");\n            }","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-spi/src/main/java/com/facebook/presto/spi/procedure/TableDataRewriteDistributedProcedure.java#L141-L177","documentation":"When parsing the sorted_by argument of the table data rewrite procedure, each element of the supplied array must be a varchar string naming a sort column or a zorder(...) expression. extractSortFieldStrings throws INVALID_FUNCTION_ARGUMENT if any element is not a Java String (e.g. a number, boolean, or nested list), reporting the offending element's runtime type.","triggerScenarios":"Calling the table data rewrite procedure with sorted_by = ARRAY[1, 2] or ARRAY[NULL, 'col'] — any non-varchar element inside the array.","commonSituations":"Passing integer column indices instead of column names; untyped NULL elements in the array; scripting/macro expansion that injects numbers into the array; copying an example with numeric literals.","solutions":["Ensure every element of sorted_by is a varchar string, e.g. ARRAY['col1','col2']","Replace NULL elements with quoted column names or drop them","Cast numeric identifiers to varchar if they are intended as column names","Check the element type reported in the message and fix that position in the array"],"exampleFix":"// before\nCALL system.rewrite_table_data('t', sorted_by => ARRAY[1, 2]);\n// after\nCALL system.rewrite_table_data('t', sorted_by => ARRAY['col1', 'col2']);","handlingStrategy":"validation","validationCode":"function validateSortedBy(arr) {\n  if (!Array.isArray(arr)) throw new Error('sorted_by must be an array');\n  arr.forEach((el, i) => {\n    if (typeof el !== 'string') throw new Error(`sorted_by[${i}] must be varchar, got ${el === null ? 'null' : typeof el}`);\n  });\n}","typeGuard":"static boolean isAllVarChar(List<?> list) {\n    return list != null && list.stream().allMatch(e -> e instanceof String);\n}","tryCatchPattern":"try {\n    runRewrite(sortedBy);\n} catch (PrestoException e) {\n    if (e.getErrorCode() == INVALID_FUNCTION_ARGUMENT && e.getMessage().contains(\"varchar elements\")) {\n        sortedBy = sortedBy.stream().map(String::valueOf).collect(toList());\n    } else { throw e; }\n}","preventionTips":["Build sorted_by arrays only from quoted string literals","Strip NULLs from arrays before passing to procedures","Add client-side type checks on procedure arguments"],"tags":["procedure","invalid-arguments","type-mismatch"],"backgroundTag":"invalid-function-arguments","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"}