deniskuzZ commented on code in PR #6256:
URL: https://github.com/apache/hive/pull/6256#discussion_r2660928459
##########
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/order/AlterTableSetWriteOrderAnalyzer.java:
##########
@@ -56,32 +63,99 @@ protected void analyzeCommand(TableName tableName,
Map<String, String> partition
ASTNode orderNode = (ASTNode) command.getChild(0);
if (orderNode.getType() == HiveParser.TOK_WRITE_LOCALLY_ORDERED_BY_ZORDER)
{
// Handle Z-ORDER
- ASTNode columnListNode = (ASTNode) orderNode.getChild(0);
- List<String> columnNames = new ArrayList<>();
- for (int i = 0; i < columnListNode.getChildCount(); i++) {
- ASTNode child = (ASTNode) columnListNode.getChild(i);
- columnNames.add(unescapeIdentifier(child.getText()).toLowerCase());
- }
-
- if (columnNames.isEmpty()) {
- throw new SemanticException("Z-order requires at least one column");
- }
-
- // Set Z-order properties in table props sort.order=ZORDER and
sort.columns=col1,col2,...
- Map<String, String> props = Map.of(
- "sort.order", "ZORDER",
- "sort.columns", String.join(",", columnNames)
- );
-
- AlterTableSetWriteOrderDesc desc = new
AlterTableSetWriteOrderDesc(tableName, partitionSpec, props);
- addInputsOutputsAlterTable(tableName, partitionSpec, desc,
desc.getType(), false);
-
- rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(),
desc)));
+ handleZOrder(tableName, partitionSpec, orderNode);
} else if (orderNode.getType() == HiveParser.TOK_WRITE_LOCALLY_ORDERED) {
- // Regular ORDERED BY - to be implemented in future commit
- throw new SemanticException("Regular ORDERED BY is not yet supported.
Only ZORDER is supported.");
+ // Handle regular ORDERED BY
+ handleRegularOrder(tableName, partitionSpec, orderNode);
} else {
throw new SemanticException("Unexpected token type: " +
orderNode.getType());
}
}
+
+ /**
+ * Handles Z-ORDER syntax: ALTER TABLE ... SET WRITE ORDERED BY ZORDER(col1,
col2, ...)
+ */
+ private void handleZOrder(TableName tableName, Map<String, String>
partitionSpec, ASTNode orderNode)
+ throws SemanticException {
+ ASTNode columnListNode = (ASTNode) orderNode.getChild(0);
+ List<String> columnNames = new ArrayList<>();
+ for (int i = 0; i < columnListNode.getChildCount(); i++) {
+ ASTNode child = (ASTNode) columnListNode.getChild(i);
+ columnNames.add(unescapeIdentifier(child.getText()).toLowerCase());
+ }
+
+ if (columnNames.isEmpty()) {
+ throw new SemanticException("Z-order requires at least one column");
+ }
+
+ // Set Z-order properties: sort.order=ZORDER and sort.columns=col1,col2,...
+ Map<String, String> props = Map.of(
+ "sort.order", "ZORDER",
+ "sort.columns", String.join(",", columnNames)
+ );
+
+ createAndAddTask(tableName, partitionSpec, props);
+ }
+
+ /**
+ * Handles regular ORDERED BY syntax: ALTER TABLE ... SET WRITE ORDERED BY
(col1 ASC, col2 DESC NULLS LAST, ...)
+ * Creates a Hive-native SortFields JSON that will be converted to Iceberg
format by the metahook.
+ */
+ private void handleRegularOrder(TableName tableName, Map<String, String>
partitionSpec, ASTNode orderNode)
+ throws SemanticException {
+ ASTNode sortColumnListNode = (ASTNode) orderNode.getChild(0);
+ List<SortFieldDesc> sortFieldDescList = new ArrayList<>();
+
+ // Parse each sort column specification
+ for (int i = 0; i < sortColumnListNode.getChildCount(); i++) {
+ ASTNode sortColNode = (ASTNode) sortColumnListNode.getChild(i);
+
+ // Determine sort direction (ASC/DESC)
+ SortFieldDesc.SortDirection sortDirection =
+ sortColNode.getToken().getType() ==
HiveParser.TOK_TABSORTCOLNAMEDESC
+ ? SortFieldDesc.SortDirection.DESC
+ : SortFieldDesc.SortDirection.ASC;
+
+ // Get the column specification node
+ ASTNode colSpecNode = (ASTNode) sortColNode.getChild(0);
+ // Extract column name
+ String columnName =
unescapeIdentifier(colSpecNode.getChild(0).getText()).toLowerCase();
+ // Determine null ordering (NULLS FIRST/LAST)
+ NullOrdering nullOrder =
NullOrdering.fromToken(colSpecNode.getToken().getType());
+
+ sortFieldDescList.add(new SortFieldDesc(columnName, sortDirection,
nullOrder));
+ }
+
+ if (sortFieldDescList.isEmpty()) {
+ throw new SemanticException("ORDERED BY requires at least one column");
+ }
+
+ // Serialize to Hive-native SortFields JSON format
+ // The metahook will convert this to Iceberg's SortOrder format
+ SortFields sortFields = new SortFields(sortFieldDescList);
+ String sortOrderJson;
+ try {
+ sortOrderJson = JSON_OBJECT_MAPPER.writeValueAsString(sortFields);
Review Comment:
don't we already have code that handles natural order during create ? can we
reuse?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]