deniskuzZ commented on code in PR #6256:
URL: https://github.com/apache/hive/pull/6256#discussion_r2690141978
##########
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/order/AlterTableSetWriteOrderAnalyzer.java:
##########
@@ -56,32 +57,70 @@ 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 natural ORDERED BY
+ handleNaturalOrder(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 handleNaturalOrder(TableName tableName, Map<String, String>
partitionSpec, ASTNode orderNode)
+ throws SemanticException {
+ ASTNode sortColumnListNode = (ASTNode) orderNode.getChild(0);
+
+ // Parse and serialize to JSON using the utility
+ String sortOrderJson =
SortOrderUtils.parseSortOrderToJson(sortColumnListNode);
+ if (sortOrderJson == null) {
+ throw new SemanticException("Failed to serialize sort order
specification");
+ }
+
+ // Set the sort order JSON in table properties
+ // The metahook will detect this and convert to Iceberg format
+ Map<String, String> props = Map.of(
Review Comment:
also ImmutableMap
--
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]