-
Notifications
You must be signed in to change notification settings - Fork 5
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
10 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 9651e88
Replaced semicolon logic to split value with JSON keys. changed opera…
subkanthi c4d10b1
Changed JSON for add/drop column to include operation JSON key.
subkanthi 0d01d9e
Replace hard coded strings in AlterTable
subkanthi f76cbf3
Fixed unit test.
subkanthi 530f16f
Fixed CLI description.
subkanthi 6ade046
Merge branch 'master' of github.com:Altinity/ice into 39-add-a-column…
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
121 changes: 121 additions & 0 deletions
121
ice/src/main/java/com/altinity/ice/cli/internal/cmd/AddColumn.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,121 @@ | ||
| /* | ||
| * 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 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 AddColumn { | ||
| private static final Logger logger = LoggerFactory.getLogger(AddColumn.class); | ||
|
|
||
| private AddColumn() {} | ||
|
|
||
| public static void run(Catalog catalog, TableIdentifier tableId, String columnDefinition) | ||
| throws IOException { | ||
|
|
||
| Table table = catalog.loadTable(tableId); | ||
|
|
||
| // Parse column definition | ||
| ColumnSpec columnSpec = parseColumnDefinition(columnDefinition); | ||
|
|
||
| // Apply schema change | ||
| Transaction transaction = table.newTransaction(); | ||
| UpdateSchema updateSchema = transaction.updateSchema(); | ||
|
|
||
| updateSchema.addColumn(columnSpec.name, columnSpec.type, columnSpec.comment); | ||
|
|
||
| updateSchema.commit(); | ||
| transaction.commitTransaction(); | ||
|
|
||
| logger.info("Successfully added column '{}' to table: {}", columnSpec.name, tableId); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
ice/src/main/java/com/altinity/ice/cli/internal/cmd/DropColumn.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,48 @@ | ||
| /* | ||
| * 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 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.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class DropColumn { | ||
| private static final Logger logger = LoggerFactory.getLogger(DropColumn.class); | ||
|
|
||
| private DropColumn() {} | ||
|
|
||
| public static void run(Catalog catalog, TableIdentifier tableId, String columnName) | ||
| throws IOException { | ||
|
|
||
| Table table = catalog.loadTable(tableId); | ||
|
|
||
| // Validate that the column exists | ||
| if (table.schema().findField(columnName) == null) { | ||
| throw new IllegalArgumentException( | ||
| "Column '" + columnName + "' does not exist in table: " + tableId); | ||
| } | ||
|
|
||
| // Apply schema change | ||
| Transaction transaction = table.newTransaction(); | ||
| UpdateSchema updateSchema = transaction.updateSchema(); | ||
|
|
||
| updateSchema.deleteColumn(columnName); | ||
|
|
||
| updateSchema.commit(); | ||
| transaction.commitTransaction(); | ||
|
|
||
| logger.info("Successfully dropped column '{}' from table: {}", columnName, tableId); | ||
| } | ||
| } |
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.
maybe this needs to be moved to a common class for field mapping.