prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Error applying key function to element at position %d

What it means

array_sort_by's key function is invoked per element via the generated KeyExtractor (keyExtractor.extract). If the lambda throws for any element, the wrapper rethrows as INVALID_FUNCTION_ARGUMENT with the offending element position so the caller knows which array entry broke the key function. This is a guard around user-supplied lambdas that Presto cannot validate at bind time.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/AbstractArraySortByKeyFunction.java:174

        }

        // Create array of indices and extracted keys
        int[] indices = new int[arrayLength];
        BlockBuilder keyBlockBuilder = keyType.createBlockBuilder(null, arrayLength);

        // Extract keys for all elements
        for (int i = 0; i < arrayLength; i++) {
            indices[i] = i;
            if (array.isNull(i)) {
                keyBlockBuilder.appendNull();
            }
            else {
                try {
                    // Use the generated KeyExtractor implementation (direct virtual call)
                    keyExtractor.extract(properties, array, i, keyFunction, keyBlockBuilder);
                }
                catch (Throwable t) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, String.format("Error applying key function to element at position %d", i), t);
                }
            }
        }

        Block keysBlock = keyBlockBuilder.build();

        // Sort indices based on extracted keys using Type's compareTo
        try {
            if (array.mayHaveNull() || keysBlock.mayHaveNull()) {
                quickSort(indices, new NullableComparator(array, keysBlock, keyType, function));
            }
            else {
                quickSort(indices, new NonNullableComparator(keysBlock, keyType, function));
            }
        }
        catch (NotSupportedException | UnsupportedOperationException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Key type does not support comparison", e);
        }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Inspect element at the reported position in the input array; fix or filter out the value the key function cannot handle.
  2. Make the key lambda null-safe and total (use COALESCE/IF/try() style logic inside the lambda).
  3. Filter the array first: array_sort_by(filter(arr, x -> key is valid), k).
  4. If using map element access, provide defaults instead of assuming the key exists.

Example fix

// before
array_sort_by(a, x -> element_at(x, 'k')) -- throws if 'k' missing
// after
array_sort_by(a, x -> coalesce(element_at(x, 'k'), 0))
Defensive patterns

Strategy: validation

Validate before calling

-- verify the key lambda is total over all elements
SELECT array_position(transform(a, x -> key(x)), NULL) IS NULL AS key_safe FROM t

Prevention

When it happens

Trigger: Calling array_sort_by(arr, k -> ...) where the key lambda throws for element i — e.g. element_at on an out-of-range index, arithmetic overflow, a map lookup missing the key with a failing accessor, or a division by zero inside the lambda.

Common situations: Sorting arrays of maps/rows where some elements lack the key the lambda extracts; null-handling mistakes in the key expression; malformed data rows in nested collections.

Related errors


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