{"record":{"id":"52919a2f67707d2f","repo":"apache/iceberg","slug":"cannot-mix-identity-sort-columns-and-a-zorder-or-h-52919a","errorCode":null,"errorMessage":"Cannot mix identity sort columns and a Zorder or Hilbert sort expression: ${sortOrderString}","messagePattern":"Cannot mix identity sort columns and a Zorder or Hilbert sort expression: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/procedures/RewriteDataFilesProcedure.java","lineNumber":210,"sourceCode":"    ExtendedParser.parseSortOrder(spark(), sortOrderString)\n        .forEach(\n            field -> {\n              if (field.term() instanceof Zorder) {\n                zOrderTerms.add((Zorder) field.term());\n              } else if (field.term() instanceof Hilbert) {\n                hilbertTerms.add((Hilbert) field.term());\n              } else {\n                sortOrderFields.add(field);\n              }\n            });\n\n    if (!zOrderTerms.isEmpty() && !hilbertTerms.isEmpty()) {\n      throw new IllegalArgumentException(\n          \"Cannot mix Zorder and Hilbert sort expressions: \" + sortOrderString);\n    }\n\n    if ((!zOrderTerms.isEmpty() || !hilbertTerms.isEmpty()) && !sortOrderFields.isEmpty()) {\n      throw new IllegalArgumentException(\n          \"Cannot mix identity sort columns and a Zorder or Hilbert sort expression: \"\n              + sortOrderString);\n    }\n  }\n\n  private RewriteDataFilesSparkAction applySortStrategy(\n      RewriteDataFilesSparkAction action,\n      List<Zorder> zOrderTerms,\n      List<Hilbert> hilbertTerms,\n      List<ExtendedParser.RawOrderField> sortOrderFields,\n      Schema schema) {\n    if (!zOrderTerms.isEmpty()) {\n      String[] columnNames =\n          zOrderTerms.stream()\n              .flatMap(zOrder -> zOrder.refs().stream().map(NamedReference::name))\n              .toArray(String[]::new);\n      return action.zOrder(columnNames);\n    } else if (!hilbertTerms.isEmpty()) {","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/procedures/RewriteDataFilesProcedure.java#L192-L228","documentation":"parseSortOrder in RewriteDataFilesProcedure also rejects combining plain (identity) sort columns with a zorder(...) or hilbert(...) expression, since the two are distinct ordering modes implemented differently. This IllegalArgumentException includes the full sort_order string.","triggerScenarios":"Calling rewrite_data_files with sort_order mixing plain column names and spatial functions, e.g. sort_order => 'a, zorder(b, c)'.","commonSituations":"Appending a new column to an existing zorder sort_order string; copying a sort spec from an identity-sort config into a zorder one.","solutions":["Remove plain column terms and keep only zorder(...)/hilbert(...) terms, or vice versa.","Use sort_order => 'zorder(b, c, d)' folding the extra column into the spatial expression if ordering on all columns is needed.","Run two separate rewrite passes: one identity-sorted, one spatially sorted."],"exampleFix":"// before\ncall rewrite_data_files(table => 'db.t', sort_order => 'a, zorder(b, c)')\n// after\ncall rewrite_data_files(table => 'db.t', sort_order => 'zorder(a, b, c)')","handlingStrategy":"validation","validationCode":"val spatial = sortOrder.contains(\"zorder(\") || sortOrder.contains(\"hilbert(\")\nval plainCols = sortOrder.split(\",\").map(_.trim).filterNot(_.matches(\"(zorder|hilbert)\\\\(.*\\\\)\"))\nrequire(!(spatial && plainCols.exists(_.nonEmpty)), \"no identity cols with zorder/hilbert\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fold extra columns into the zorder/hilbert expression","Keep separate configs for identity-sorted and spatially-sorted tables","Validate sort_order strings before passing to CALL"],"tags":["spark","compaction","sort-order"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}