{"record":{"id":"23ca200a389db8fd","repo":"prestodb/presto","slug":"unable-to-parse-sort-field-s","errorCode":null,"errorMessage":"Unable to parse sort field: [%s]","messagePattern":"Unable to parse sort field: \\[(.+?)\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-iceberg/src/main/java/com/facebook/presto/iceberg/SortFieldUtils.java","lineNumber":84,"sourceCode":"        for (SortField field : sortOrder.fields()) {\n            if (!baseColumnFieldIds.contains(field.sourceId())) {\n                throw new PrestoException(COLUMN_NOT_FOUND, \"Column not found: \" + schema.findColumnName(field.sourceId()));\n            }\n        }\n\n        return sortOrder;\n    }\n\n    public static void parseSortFields(SortOrderBuilder<?> sortOrderBuilder, List<String> fields)\n    {\n        fields.forEach(field -> parseSortField(sortOrderBuilder, field));\n    }\n\n    private static void parseSortField(SortOrderBuilder<?> builder, String field)\n    {\n        Matcher matcher = PATTERN.matcher(field);\n        if (!matcher.matches()) {\n            throw new IllegalArgumentException(format(\"Unable to parse sort field: [%s]\", field));\n        }\n\n        String columnName = fromIdentifierToColumn(matcher.group(\"identifier\"));\n        boolean ascending;\n        String ordering = firstNonNull(matcher.group(\"ordering\"), \"ASC\").toUpperCase(Locale.ENGLISH);\n\n        switch (ordering) {\n            case \"ASC\":\n                ascending = true;\n                break;\n            case \"DESC\":\n                ascending = false;\n                break;\n            default:\n                throw new IllegalStateException(\"Unexpected ordering value: \" + ordering);\n        }\n\n        String nullOrderDefault = ascending ? \"FIRST\" : \"LAST\";","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-iceberg/src/main/java/com/facebook/presto/iceberg/SortFieldUtils.java#L66-L102","documentation":"parseSortField applies a regex (PATTERN) to each sorted_by entry to extract identifier, ordering (ASC/DESC), optional null ordering, and transform. Entries that don't match the grammar throw IllegalArgumentException(\"Unable to parse sort field: [%s]\") which surfaces from parseSortFields. It enforces the strict syntax of the sorted_by property.","triggerScenarios":"CREATE TABLE ... WITH (sorted_by = ARRAY[...]) containing an entry that fails the regex — e.g. empty string, missing identifier, malformed transform like 'bucket(abc, col)', stray characters, or a completely free-form sort expression.","commonSituations":"Passing SQL ORDER BY expressions instead of the identifier grammar; quoting errors leaving extra quotes in the string; whitespace or case variants the pattern rejects; copying syntax from other engines (e.g. Spark CLUSTER BY).","solutions":["Rewrite each entry in the accepted form: identifier [ASC|DESC] with optional transform, e.g. 'col ASC', 'truncate(10, col) DESC'","Remove surrounding quotes/spaces accidentally included inside the array element","Replace complex expressions with plain column names plus Iceberg transforms","Create without sorted_by, then add the property incrementally, testing one field at a time to isolate the malformed entry"],"exampleFix":"// before\nWITH (sorted_by = ARRAY['LOWER(name) ASC'])\n// after\nWITH (sorted_by = ARRAY['name ASC'])","handlingStrategy":"validation","validationCode":"Pattern P = Pattern.compile(\"(?<identifier>\\\\S+?)(\\\\s+(?<ordering>ASC|DESC))?(\\\\s+NULLS\\\\s+(?<nulls>FIRST|LAST))?\\\\s*$\");\nfor (String f : sortedBy) {\n    if (!P.matcher(f).matches()) throw new IllegalArgumentException(\"Unable to parse sort field: [\" + f + \"]\");\n}","typeGuard":"boolean isValidSortField(String f) {\n    return f != null && f.matches(\"^\\\\S+(\\\\s+(ASC|DESC))?(\\\\s+NULLS\\\\s+(FIRST|LAST))?\\\\s*$\");\n}","tryCatchPattern":"try { /* CREATE TABLE with sorted_by */ } catch (Exception e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Unable to parse sort field\")) {\n        // offending entry is inside the brackets; fix syntax and retry\n    } else { throw e; }\n}","preventionTips":["Use only the grammar: identifier [ASC|DESC] [NULLS FIRST|LAST], with optional Iceberg transforms","Do not pass SQL expressions or ORDER BY clauses as sorted_by entries","Trim whitespace and avoid stray quotes inside array elements","Test each sorted_by entry individually before final DDL"],"tags":["iceberg","ddl","syntax","sort-order"],"backgroundTag":"sort-field-parse-error","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"}