Skip to main content

PartitionSpec Class

The PartitionSpec class defines how to produce partition data for an Apache Iceberg table. Partition data is produced by transforming columns in a table. Package: org.apache.iceberg

Overview

The PartitionSpec class provides:
  • Column transformation for partitioning (identity, bucket, truncate, temporal)
  • Partition evolution with spec IDs
  • Multiple partition fields per spec
  • Compatibility checking with schemas
Partition field IDs start at 1000 to avoid conflicts with schema field IDs.

Basic Methods

schema()

Returns the schema for this partition spec. Returns: The Schema this spec is bound to

specId()

Returns the ID of this partition spec. Returns: The spec ID (0 for unpartitioned tables)

fields()

Returns the list of partition fields for this spec. Returns: List of PartitionField objects Example:

isPartitioned()

Returns true if the spec has partition fields (excluding void transforms). Returns: True if partitioned, false otherwise

isUnpartitioned()

Returns true if the spec has no partition fields. Returns: True if unpartitioned, false otherwise

Partition Field Access

getFieldsBySourceId(int fieldId)

Returns the partition fields that partition the given source field.
int
required
A field ID from the source schema
Returns: List of PartitionField objects that partition this source field Example:

identitySourceIds()

Returns the source field IDs for identity partitions. Returns: Set of source field IDs that use identity partitioning

Partition Types

partitionType()

Returns a struct type for partition data defined by this spec. Returns: StructType describing the partition fields Example:

rawPartitionType()

Returns a struct matching partition information as written into manifest files. This differs from partitionType() when field IDs have been reassigned. Returns: StructType with original field IDs

javaClasses()

Returns the Java classes for partition field values. Returns: Array of Class objects corresponding to partition fields

Partition Path Operations

partitionToPath(StructLike data)

Converts partition data to a partition path string.
StructLike
required
The partition data
Returns: A partition path string (e.g., “year=2024/month=03/day=05”) Example:

Compatibility

compatibleWith(PartitionSpec other)

Returns true if this spec is equivalent to another, with partition field IDs ignored. Two specs are compatible if they have the same:
  • Number of fields
  • Field order
  • Field names
  • Source columns
  • Transforms
PartitionSpec
required
Another PartitionSpec to compare
Returns: True if compatible, false otherwise

Conversion

toUnbound()

Converts this partition spec to an unbound spec. Returns: An UnboundPartitionSpec representation

Static Factory Methods

unpartitioned()

Returns a spec for unpartitioned tables. Returns: An empty partition spec Example:

builderFor(Schema schema)

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

Builder Class

The Builder class creates valid partition specs.

Builder Methods

identity(String sourceName)

Adds an identity partition using the source column name.
String
required
The source column name
Example:

identity(String sourceName, String targetName)

Adds an identity partition with a custom partition field name.
String
required
The source column name
String
required
The partition field name

year(String sourceName)

Adds a year transform partition.
String
required
The source date/timestamp column name
Example:

year(String sourceName, String targetName)

Adds a year transform partition with a custom name.

month(String sourceName)

Adds a month transform partition.
String
required
The source date/timestamp column name
Example:

month(String sourceName, String targetName)

Adds a month transform partition with a custom name.

day(String sourceName)

Adds a day transform partition.
String
required
The source date/timestamp column name
Example:

day(String sourceName, String targetName)

Adds a day transform partition with a custom name.

hour(String sourceName)

Adds an hour transform partition.
String
required
The source timestamp column name
Example:

hour(String sourceName, String targetName)

Adds an hour transform partition with a custom name.

bucket(String sourceName, int numBuckets)

Adds a bucket transform partition.
String
required
The source column name
int
required
The number of buckets
Example:

bucket(String sourceName, int numBuckets, String targetName)

Adds a bucket transform partition with a custom name.

truncate(String sourceName, int width)

Adds a truncate transform partition.
String
required
The source column name
int
required
The truncation width
Example:

truncate(String sourceName, int width, String targetName)

Adds a truncate transform partition with a custom name.

alwaysNull(String sourceName)

Adds a void transform partition (always returns null).
String
required
The source column name
Void transforms are used for deprecated partition fields in version 1 tables.

alwaysNull(String sourceName, String targetName)

Adds a void transform partition with a custom name.

withSpecId(int newSpecId)

Sets the spec ID for this partition spec.
int
required
The spec ID
Returns: This builder for method chaining

caseSensitive(boolean sensitive)

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 partition spec and validates compatibility with the schema. Returns: A new PartitionSpec Throws: ValidationException if the spec is invalid

build(boolean allowMissingFields)

Builds the partition spec with optional validation relaxation.
boolean
required
If true, allows partition fields whose source columns are missing
Returns: A new PartitionSpec

Usage Examples

Creating an Unpartitioned Spec

Creating a Simple Partitioned Spec

Creating a Multi-Level Time Partition

Creating a Hash Bucket Partition

Creating a Truncate Partition

Working with Partition Data

Checking Spec Compatibility

Inspecting Partition Fields

Partition Evolution Example

Source Code Reference

Source: org/apache/iceberg/PartitionSpec.java:53