Skip to main content

SortOrder Class

The SortOrder class defines how data and delete files should be ordered in an Apache Iceberg table. Package: org.apache.iceberg

Overview

The SortOrder class provides:
  • Multi-column sorting specifications
  • Custom sort directions (ascending/descending)
  • Null ordering control (nulls first/last)
  • Sort order evolution with order IDs
  • Transform-based sorting
Sort order ID 0 is reserved for unsorted tables. All custom sort orders must use IDs greater than 0.

Basic Methods

schema()

Returns the schema for this sort order. Returns: The Schema this sort order is bound to

orderId()

Returns the ID of this sort order. Returns: The order ID (0 for unsorted)

fields()

Returns the list of sort fields for this sort order. Returns: List of SortField objects Example:

isSorted()

Returns true if the sort order has at least one sort field. Returns: True if sorted, false otherwise

isUnsorted()

Returns true if the sort order has no sort fields. Returns: True if unsorted, false otherwise

Sort Order Comparison

satisfies(SortOrder anotherSortOrder)

Checks whether this order satisfies another order. A sort order satisfies another if:
  • The other order is unsorted, OR
  • This order has at least as many fields as the other, AND
  • Each field in the other order is satisfied by the corresponding field in this order
SortOrder
required
The sort order to check against
Returns: True if this order satisfies the given order Example:

sameOrder(SortOrder anotherSortOrder)

Checks whether this order is equivalent to another order while ignoring the order ID.
SortOrder
required
The sort order to compare
Returns: True if the orders are equivalent

Conversion

toUnbound()

Converts this sort order to an unbound sort order. Returns: An UnboundSortOrder representation

Static Factory Methods

unsorted()

Returns a sort order for unsorted tables. Returns: An unsorted order with order ID 0 Example:

builderFor(Schema schema)

Creates a new sort order builder for the given schema.
Schema
required
The table schema
Returns: A Builder instance Example:

Builder Class

The Builder class creates valid sort orders.

Builder Methods

asc(Term term)

Adds an expression term to the sort in ascending order with nulls first.
Term
required
An expression term (e.g., Expressions.ref("column"))
Returns: This builder for method chaining Example:

asc(Term term, NullOrder nullOrder)

Adds an expression term to the sort in ascending order with custom null ordering.
Term
required
An expression term
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
Returns: This builder for method chaining Example:

desc(Term term)

Adds an expression term to the sort in descending order with nulls first.
Term
required
An expression term
Returns: This builder for method chaining Example:

desc(Term term, NullOrder nullOrder)

Adds an expression term to the sort in descending order with custom null ordering.
Term
required
An expression term
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
Returns: This builder for method chaining

sortBy(String name, SortDirection direction, NullOrder nullOrder)

Adds a column to the sort by name.
String
required
The column name
SortDirection
required
Sort direction (ASC or DESC)
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
Returns: This builder for method chaining Example:

sortBy(Term term, SortDirection direction, NullOrder nullOrder)

Adds an expression term to the sort.
Term
required
An expression term
SortDirection
required
Sort direction (ASC or DESC)
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
Returns: This builder for method chaining

withOrderId(int newOrderId)

Sets the order ID for this sort order.
int
required
The order ID (must be > 0 for sorted orders)
Returns: This builder for method chaining
Order ID 0 is reserved for unsorted tables and will cause an error if used with sort fields.

caseSensitive(boolean sortCaseSensitive)

Sets whether column name matching should be case-sensitive.
boolean
required
True for case-sensitive matching
Returns: This builder for method chaining

build()

Builds the sort order and validates compatibility with the schema. Returns: A new SortOrder Throws: ValidationException if the sort order is invalid

Static Validation Methods

checkCompatibility(SortOrder sortOrder, Schema schema)

Checks the compatibility of a sort order with a schema. Validates that:
  • All source fields exist in the schema
  • All source fields are primitive types
  • All transforms can be applied to their source types
SortOrder
required
The sort order to validate
Schema
required
The schema to validate against
Throws: ValidationException if incompatible

Usage Examples

Creating an Unsorted Order

Creating a Simple Sort Order

Creating a Multi-Column Sort Order

Custom Null Ordering

Sort with Transforms

Using sortBy Method

Updating Table Sort Order

Checking Sort Order Satisfaction

Inspecting Sort Fields

Sort Order Evolution

Case-Insensitive Builder

Enums

SortDirection

NullOrder

Best Practices

Sort Order Strategy:
  • Sort by commonly filtered columns to improve query performance
  • Place high-cardinality columns first
  • Use transforms (like year, month, bucket) for efficient data organization
  • Consider data clustering when designing sort orders
Performance Considerations:
  • Sort orders affect file organization and query performance
  • Changing sort orders requires rewriting data files
  • More sort fields can improve specific queries but increase complexity

Source Code Reference

Source: org/apache/iceberg/SortOrder.java:41