-
Notifications
You must be signed in to change notification settings - Fork 2
Added support for add column and drop column #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
subkanthi
wants to merge
4
commits into
master
Choose a base branch
from
39-add-a-column-to-an-existing-table
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+232
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d7a0d6
Added support for add column and drop column
subkanthi a4746bd
Merged changes from main.
subkanthi 701a98e
Changed add-column, drop-column to alter-table that takes a JSON list…
subkanthi 26e15c1
Removed AddColumn, DropColumn classes.
subkanthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
ice/src/main/java/com/altinity/ice/cli/internal/cmd/AlterTable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package com.altinity.ice.cli.internal.cmd; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.Transaction; | ||
import org.apache.iceberg.UpdateSchema; | ||
import org.apache.iceberg.catalog.Catalog; | ||
import org.apache.iceberg.catalog.TableIdentifier; | ||
import org.apache.iceberg.types.Types; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class AlterTable { | ||
private static final Logger logger = LoggerFactory.getLogger(AlterTable.class); | ||
|
||
private AlterTable() {} | ||
|
||
public enum OperationType { | ||
ADD("add"), | ||
DROP("drop"); | ||
|
||
private final String key; | ||
|
||
OperationType(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public static OperationType fromKey(String key) { | ||
for (OperationType type : values()) { | ||
if (type.key.equals(key)) { | ||
return type; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unsupported operation type: " + key); | ||
} | ||
} | ||
|
||
public static void run( | ||
Catalog catalog, TableIdentifier tableId, List<Map<String, String>> operations) | ||
throws IOException { | ||
|
||
Table table = catalog.loadTable(tableId); | ||
|
||
// Apply schema changes | ||
Transaction transaction = table.newTransaction(); | ||
UpdateSchema updateSchema = transaction.updateSchema(); | ||
|
||
for (Map<String, String> operation : operations) { | ||
validateOperation(operation); | ||
|
||
OperationType operationType = getOperationType(operation); | ||
|
||
switch (operationType) { | ||
case ADD: | ||
String columnDefinition = operation.get(OperationType.ADD.getKey()); | ||
ColumnSpec columnSpec = parseColumnDefinition(columnDefinition); | ||
updateSchema.addColumn(columnSpec.name, columnSpec.type, columnSpec.comment); | ||
logger.info("Adding column '{}' to table: {}", columnSpec.name, tableId); | ||
break; | ||
case DROP: | ||
String columnName = operation.get(OperationType.DROP.getKey()); | ||
// Validate that the column exists | ||
if (table.schema().findField(columnName) == null) { | ||
throw new IllegalArgumentException( | ||
"Column '" + columnName + "' does not exist in table: " + tableId); | ||
} | ||
updateSchema.deleteColumn(columnName); | ||
logger.info("Dropping column '{}' from table: {}", columnName, tableId); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unsupported operation type: " + operationType); | ||
} | ||
} | ||
|
||
updateSchema.commit(); | ||
transaction.commitTransaction(); | ||
|
||
logger.info("Successfully applied {} operations to table: {}", operations.size(), tableId); | ||
} | ||
|
||
private static void validateOperation(Map<String, String> operation) { | ||
if (operation == null || operation.isEmpty()) { | ||
throw new IllegalArgumentException("Operation cannot be null or empty"); | ||
} | ||
|
||
Set<String> keys = operation.keySet(); | ||
if (keys.size() != 1) { | ||
throw new IllegalArgumentException( | ||
"Each operation must contain exactly one key. Found keys: " + keys); | ||
} | ||
|
||
String key = keys.iterator().next(); | ||
try { | ||
OperationType.fromKey(key); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException( | ||
"Invalid operation. Supported operations: " | ||
+ java.util.Arrays.stream(OperationType.values()) | ||
.map(OperationType::getKey) | ||
.reduce((a, b) -> a + ", " + b) | ||
.orElse("none")); | ||
} | ||
} | ||
|
||
private static OperationType getOperationType(Map<String, String> operation) { | ||
String key = operation.keySet().iterator().next(); | ||
return OperationType.fromKey(key); | ||
} | ||
|
||
private static ColumnSpec parseColumnDefinition(String columnDefinition) { | ||
String[] parts = columnDefinition.split(":"); | ||
if (parts.length < 2) { | ||
throw new IllegalArgumentException( | ||
"Invalid column definition format. Expected: name:type[:comment] (e.g. 'age:int:User age')"); | ||
} | ||
|
||
String columnName = parts[0]; | ||
String columnType = parts[1]; | ||
String comment = parts.length > 2 ? parts[2] : null; | ||
|
||
Types.NestedField field = parseColumnType(columnName, columnType, comment); | ||
|
||
return new ColumnSpec(columnName, field.type(), comment); | ||
} | ||
|
||
private static Types.NestedField parseColumnType(String name, String type, String comment) { | ||
Types.NestedField field; | ||
|
||
switch (type.toLowerCase()) { | ||
case "string": | ||
case "varchar": | ||
field = Types.NestedField.optional(-1, name, Types.StringType.get(), comment); | ||
break; | ||
case "int": | ||
case "integer": | ||
field = Types.NestedField.optional(-1, name, Types.IntegerType.get(), comment); | ||
break; | ||
case "long": | ||
case "bigint": | ||
field = Types.NestedField.optional(-1, name, Types.LongType.get(), comment); | ||
break; | ||
case "double": | ||
field = Types.NestedField.optional(-1, name, Types.DoubleType.get(), comment); | ||
break; | ||
case "float": | ||
field = Types.NestedField.optional(-1, name, Types.FloatType.get(), comment); | ||
break; | ||
case "boolean": | ||
field = Types.NestedField.optional(-1, name, Types.BooleanType.get(), comment); | ||
break; | ||
case "date": | ||
field = Types.NestedField.optional(-1, name, Types.DateType.get(), comment); | ||
break; | ||
case "timestamp": | ||
field = Types.NestedField.optional(-1, name, Types.TimestampType.withoutZone(), comment); | ||
break; | ||
case "timestamptz": | ||
field = Types.NestedField.optional(-1, name, Types.TimestampType.withZone(), comment); | ||
break; | ||
case "binary": | ||
field = Types.NestedField.optional(-1, name, Types.BinaryType.get(), comment); | ||
break; | ||
default: | ||
throw new IllegalArgumentException( | ||
"Unsupported column type: " | ||
+ type | ||
+ ". Supported types: string, int, long, double, float, boolean, date, timestamp, timestamptz, binary"); | ||
} | ||
|
||
return field; | ||
} | ||
|
||
private static class ColumnSpec { | ||
final String name; | ||
final org.apache.iceberg.types.Type type; | ||
final String comment; | ||
|
||
ColumnSpec(String name, org.apache.iceberg.types.Type type, String comment) { | ||
this.name = name; | ||
this.type = type; | ||
this.comment = comment; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be best to stick to yaml/json for this as column names may contain colons, future operations may not be so easy to express in colon-separated values, etc.
It would also be nice if operation/files names would follow https://iceberg.apache.org/docs/latest/spark-ddl/#alter-table-rename-column naming.