Skip to content

How to set sort order on S3 Tables for Iceberg sort/z-order compaction?

0

Looking to implement sort compaction on an S3 Tables Iceberg table following the blog post:

https://aws.amazon.com/blogs/aws/new-improve-apache-iceberg-query-performance-in-amazon-s3-with-sort-and-z-order-compaction/

Setup

  • S3 Tables bucket with Apache Iceberg Table created via the standard S3 Tables flow
  • Table federated through Lake Formation permissions
  • Queries run using Amazon Athena

Example

CREATE TABLE develop.daily_sales (
  sale_date date,
  product_category string,
  sales_amount double
)
PARTITIONED BY (month(sale_date))
TBLPROPERTIES ('table_type' = 'iceberg');
-- sort?

What I want is to define a sort order, for example: Sort by sale_date ASC as explained here ref

What I’ve tried Athena table properties

TBLPROPERTIES does not support write.metadata.sort-order or sorted_by

After table Alter

ALTER TABLE glue_catalog.develop.daily_sales WRITE ORDERED BY sale_date ASC

S3 Tables Console : Sort compaction fails due to missing sort order metadata

Question So, what is the correct and supported way to define a sort order (for example, sale_date ASC) on an S3 Tables Iceberg table, so that sort or Z-order compaction can be enabled?

Is defining a sort order supported at table creation time or via ALTER TABLE, and can it be configured using SQL, the AWS CLI, or AWS Glue / Spark as explained on blog?

References: https://aws.amazon.com/about-aws/whats-new/2025/06/amazon-s3-sort-z-order-compaction-apache-iceberg-tables/ https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-maintenance.html#s3-tables-maintenance-compaction

asked 10 months ago720 views

5 Answers
0

I wasnt able to do this via Athena nor by Spark.sql's CREATE Table DDL.Only thing that worked is the Alter table spark.sql("ALTER TABLE glue_catalog.develop.daily_salesWRITE ORDERED BY sale_date ASC") Please refer to https://aws.amazon.com/blogs/aws/new-improve-apache-iceberg-query-performance-in-amazon-s3-with-sort-and-z-order-compaction/

answered 10 months ago

0

As far as I can tell, you have to do this in spark. something like spark.sql("ALTER TABLE db.tbl WRITE ORDERED BY event_date DESC")

The documentation for this is currently really inaccurate unfortunately.

answered 9 months ago

0

Today this can be done at creation or after using the CLI or API: https://aws.amazon.com/about-aws/whats-new/2026/02/s3-tables-partition-and-sort-order-createtable-api/ https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-maintenance.html

If your workflow requires the ability to set this with Athena, please comment with your use case to help us prioritise. Thank you

AWS
EXPERT

answered 9 months ago

-1

To set a sort order on an S3 Tables Iceberg table for sort or z-order compaction, you need to define a sort order in your Iceberg table properties first, which is a prerequisite for both compaction strategies.

For Apache Iceberg tables, you can define the sort order using the following SQL command:

ALTER TABLE glue_catalog.develop.daily_sales 
SET TBLPROPERTIES (
'sort_order'='sale_date ASC'
)

Alternatively, you can define the sort order at table creation time:

CREATE TABLE develop.daily_sales (
sale_date date,
product_category string,
sales_amount double
)
PARTITIONED BY (month(sale_date))
TBLPROPERTIES (
'table_type' = 'iceberg',
'sort_order' = 'sale_date ASC'
);

After defining the sort order in your table properties, you can enable the appropriate compaction strategy:

  1. For S3 Tables, if a table has a defined sort order, the sort compaction strategy is automatically applied during ongoing maintenance without additional configuration.

  2. To use z-order compaction, you need to update the table maintenance configuration using the S3 Tables API and set the strategy to z-order.

For Iceberg tables in general purpose S3 buckets, you can configure AWS Glue Data Catalog to use either sort or z-order compaction during optimization by updating the compaction settings in the AWS Glue Data Catalog console.

Note that to set the compaction strategy to sort or z-order, you need the s3tables:GetTableData permission in addition to having a defined sort order in your table properties.
Sources
New: Improve Apache Iceberg query performance in Amazon S3 with sort and z-order compaction | AWS News Blog
Amazon S3 now supports sort and z-order compaction for Apache Iceberg tables - AWS
Maintenance for tables - Amazon Simple Storage Service

answered 10 months ago

  • CREATE TABLE develop.daily_sales ( sale_date date, product_category string, sales_amount double ) PARTITIONED BY (month(sale_date)) TBLPROPERTIES ( 'table_type' = 'iceberg', 'sort_order' = 'sale_date ASC' );

    Unsupported table property key: sort_order

-1

Athena doesn't support CREATE_TABLE with sort for Iceberg.

CREATE TABLE develop.daily_sales (
  sale_date date,
  product_category string,
  sales_amount double
)
PARTITIONED BY (month(sale_date))
WITH (
  table_type = 'ICEBERG'
);

Instead, you can partition the table using desired column and sort the results while running the query.

SELECT *
FROM develop.daily_sales
ORDER BY sale_date;

Refer: https://repost.aws/questions/QUhDaLJyIIQ0C983MBpZ51Cg/athena-iceberg-sorted-tables

EXPERT

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.