Skip to content

Conversation

@kokila-19
Copy link
Contributor

What changes were proposed in this pull request?

Implemented Natural ordering(write ordered by) ALTER TABLE DDL support for Iceberg tables.

Why are the changes needed?

Existing tables can be converted to natural ordered tables using ALTER command. Note that only the data inserted after alter query are ordered, not existing data.

Does this PR introduce any user-facing change?

yes, supported new syntax

SYNTAX:
ALTER TABLE table_name SET WRITE ORDERED BY column_name sort_direction NULLS FIRST/LAST, ...

EXAMPLE:
ALTER TABLE table_order SET WRITE ORDERED BY id desc nulls first, name asc nulls last;

How was this patch tested?

qtest

@kokila-19
Copy link
Contributor Author

I have addressed all the comments and refactored the code as well.
Please review them.
@deniskuzZ

@kokila-19 kokila-19 requested a review from deniskuzZ January 12, 2026 13:16
for (SortFieldDesc fieldDesc : sortFieldDescList) {
NullOrder nullOrder = convertNullOrder(fieldDesc.getNullOrdering());

if (fieldDesc.getDirection() == SortFieldDesc.SortDirection.ASC) {
Copy link
Member

@deniskuzZ deniskuzZ Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

BiConsumer<String, SortOrder.NullOrder> sortMethod =
    fieldDesc.getDirection() == SortFieldDesc.SortDirection.ASC
        ? replaceSortOrder::asc
        : replaceSortOrder::desc;

sortMethod.accept(fieldDesc.getColumnName(), nullOrder);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered the BiConsumer approach but I don’t see a clear benefit here since it introduces an extra level of indirection without reducing complexity. The logic is a simple ASC vs DESC decision and directly mutates replaceSortOrder, so an explicit if/else is more readable and easier to understand and debug.

If there is significant advantage to using BiConsumer in this context, it can be changed.

Copy link
Member

@deniskuzZ deniskuzZ Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokila-19, this way it removes duplicated call-site logic, similar to https://github.com/apache/hive/pull/6256/files#diff-e7a6dc1b51c479f2b51a7b66596d321c7c0d4a2a2ccea09a5ea65d4e5c6e0f61R64-R67.
I think it's more clear than use if else branching, but I'll leave it to you

LOG.warn("Can not create write order json. ", e);
return null;
}
return SortOrderUtils.parseSortOrderToJson(ast);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Member

@deniskuzZ deniskuzZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some minor things

SYNTAX:
ALTER TABLE table_name SET WRITE ORDERED BY column_name sort_direction NULLS FIRST/LAST, ...

EXAMPLE:
ALTER TABLE table_order SET WRITE ORDERED BY id desc nulls first, name asc nulls last;
@sonarqubecloud
Copy link

Copy link
Member

@deniskuzZ deniskuzZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants