prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

sorted_by array must contain only varchar elements, found: %s

What it means

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.

Source

Thrown at presto-spi/src/main/java/com/facebook/presto/spi/procedure/TableDataRewriteDistributedProcedure.java:159

    public interface FinishCallDistributedProcedure
    {
        void finish(ConnectorSession session, ConnectorProcedureContext procedureContext, ConnectorDistributedProcedureHandle procedureHandle, Collection<Slice> fragments);
    }

    public static List<String> extractSortFieldStrings(Object[] arguments, OptionalInt sortOrderIndex)
    {
        List<String> sortFieldStrings = Collections.emptyList();
        if (sortOrderIndex.isPresent()) {
            Object value = arguments[sortOrderIndex.getAsInt()];
            if (value == null) {
                sortFieldStrings = Collections.emptyList();
            }
            else if (value instanceof List<?>) {
                try {
                    sortFieldStrings = Collections.unmodifiableList(((List<?>) value).stream()
                            .map(element -> {
                                if (!(element instanceof String)) {
                                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT,
                                            format("sorted_by array must contain only varchar elements, found: %s",
                                                    element == null ? "null" : element.getClass().getSimpleName()));
                                }
                                return (String) element;
                            })
                            .collect(Collectors.toList()));
                }
                catch (PrestoException e) {
                    throw e;
                }
                catch (ClassCastException e) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT,
                            "sorted_by array must contain only varchar elements", e);
                }
            }
            else {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "sorted_by must be an array(varchar)");
            }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure every element of sorted_by is a varchar string, e.g. ARRAY['col1','col2']
  2. Replace NULL elements with quoted column names or drop them
  3. Cast numeric identifiers to varchar if they are intended as column names
  4. Check the element type reported in the message and fix that position in the array

Example fix

// before
CALL system.rewrite_table_data('t', sorted_by => ARRAY[1, 2]);
// after
CALL system.rewrite_table_data('t', sorted_by => ARRAY['col1', 'col2']);
Defensive patterns

Strategy: validation

Validate before calling

function validateSortedBy(arr) {
  if (!Array.isArray(arr)) throw new Error('sorted_by must be an array');
  arr.forEach((el, i) => {
    if (typeof el !== 'string') throw new Error(`sorted_by[${i}] must be varchar, got ${el === null ? 'null' : typeof el}`);
  });
}

Type guard

static boolean isAllVarChar(List<?> list) {
    return list != null && list.stream().allMatch(e -> e instanceof String);
}

Try / catch

try {
    runRewrite(sortedBy);
} catch (PrestoException e) {
    if (e.getErrorCode() == INVALID_FUNCTION_ARGUMENT && e.getMessage().contains("varchar elements")) {
        sortedBy = sortedBy.stream().map(String::valueOf).collect(toList());
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling the table data rewrite procedure with sorted_by = ARRAY[1, 2] or ARRAY[NULL, 'col'] — any non-varchar element inside the array.

Common situations: 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.

Related errors


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