Skip to content

Commit

Permalink
[#4968] improvement(api): Unify the modification behavior of the comm…
Browse files Browse the repository at this point in the history
…ent field (#4968)
  • Loading branch information
chenzeping.ricco committed Oct 27, 2024
1 parent be305fa commit 53f45db
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 93 deletions.
10 changes: 9 additions & 1 deletion api/src/main/java/org/apache/gravitino/file/FilesetChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ static FilesetChange removeProperty(String property) {
* Creates a new fileset change to remove comment from the fileset.
*
* @return The fileset change.
* @deprecated Use {@link #updateComment(String)} with null value as the argument instead.
*/
@Deprecated
static FilesetChange removeComment() {
return RemoveComment.getInstance();
}
Expand Down Expand Up @@ -310,7 +312,13 @@ public String toString() {
}
}

/** A fileset change to remove comment from the fileset. */
/**
* A fileset change to remove comment from the fileset. The remove operation has been replaced by
* the update operation. Please use the update operation.
*
* @deprecated Use {@link UpdateFilesetComment} with null value as the argument instead.
*/
@Deprecated
final class RemoveComment implements FilesetChange {
private static final RemoveComment INSTANCE = new RemoveComment();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ public void testRemoveFilesetComment() throws IOException {
String name = "fileset27";
Fileset fileset = createFileset(name, schemaName, comment, Fileset.Type.MANAGED, null, null);

FilesetChange change1 = FilesetChange.removeComment();
FilesetChange change1 = FilesetChange.updateComment(null);
try (SecureHadoopCatalogOperations ops = new SecureHadoopCatalogOperations(store)) {
ops.initialize(Maps.newHashMap(), randomCatalogInfo(), HADOOP_PROPERTIES_METADATA);
NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public void testFilesetRemoveComment() throws IOException {
catalog
.asFilesetCatalog()
.alterFileset(
NameIdentifier.of(schemaName, filesetName), FilesetChange.removeComment());
NameIdentifier.of(schemaName, filesetName), FilesetChange.updateComment(null));
assertFilesetExists(filesetName);

// verify fileset is updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public void testCatalog() throws ExecutionException, InterruptedException {
Assertions.assertEquals("new comment", alteredCatalog.comment());
Assertions.assertFalse(alteredCatalog.properties().containsKey("key1"));

Catalog updateNullComment =
metalake.alterCatalog(catalogName, CatalogChange.updateComment(null));
Assertions.assertNull(updateNullComment.comment());

// test drop catalog
boolean dropped = metalake.dropCatalog(catalogName, true);
Assertions.assertTrue(dropped);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public void testAlterFileset() throws JsonProcessingException {
assertFileset(mockFileset3, res3);

// Test remove fileset comment
FilesetUpdateRequest req4 = new FilesetUpdateRequest.RemoveFilesetCommentRequest();
FilesetUpdateRequest req4 = new FilesetUpdateRequest.UpdateFilesetCommentRequest(null);
FilesetDTO mockFileset4 =
mockFilesetDTO("new name", Fileset.Type.MANAGED, null, "mock location", ImmutableMap.of());
FilesetResponse resp4 = new FilesetResponse(mockFileset4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void testCatalogAvailable() {
CatalogNotInUseException.class,
() ->
filesetOps.alterFileset(
NameIdentifier.of("dummy", "dummy"), FilesetChange.removeComment()));
NameIdentifier.of("dummy", "dummy"), FilesetChange.updateComment(null)));

Assertions.assertTrue(metalake.dropCatalog(catalogName), "catalog should be dropped");
Assertions.assertFalse(metalake.dropCatalog(catalogName), "catalog should be non-existent");
Expand Down Expand Up @@ -399,6 +399,10 @@ void testUpdateCatalogWithNullableComment() {
metalake.alterCatalog(catalogName, CatalogChange.updateComment("new catalog comment"));
Assertions.assertEquals("new catalog comment", updatedCatalog.comment());

Catalog updateNullComment =
metalake.alterCatalog(catalogName, CatalogChange.updateComment(null));
Assertions.assertNull(updateNullComment.comment());

metalake.disableCatalog(catalogName);
metalake.dropCatalog(catalogName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -219,6 +220,10 @@ public void testUpdateMetalakeWithNullableComment() {
new MetalakeChange[] {MetalakeChange.updateComment("new metalake comment")};
GravitinoMetalake updatedMetalake = client.alterMetalake(metalakeNameA, changes);
assertEquals("new metalake comment", updatedMetalake.comment());

GravitinoMetalake updateNullComment = client.alterMetalake(metalakeNameA, MetalakeChange.updateComment(null));
assertNull(updateNullComment.comment());

assertTrue(client.dropMetalake(metalakeNameA, true));
}

Expand Down Expand Up @@ -317,7 +322,7 @@ public void testMetalakeAvailable() {
MetalakeNotInUseException.class,
() ->
filesetOps.alterFileset(
NameIdentifier.of("dummy", "dummy"), FilesetChange.removeComment()));
NameIdentifier.of("dummy", "dummy"), FilesetChange.updateComment(null)));

Assertions.assertThrows(
NonEmptyMetalakeException.class, () -> client.dropMetalake(metalakeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public void testCreateAndAlterTag() {
Assertions.assertEquals("new comment1", alteredTag5.comment());
Assertions.assertEquals(ImmutableMap.of("k5", "v5"), alteredTag5.properties());
Assertions.assertFalse(alteredTag5.inherited().isPresent());

// Test update nullable comment
Tag alteredTag6 = metalake.alterTag(tagName1, TagChange.updateComment(null));
Assertions.assertNull(alteredTag6.comment());
}

@Test
Expand Down
11 changes: 9 additions & 2 deletions clients/client-python/gravitino/api/fileset_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ def remove_comment():
Returns:
The fileset change.
Deprecated:
Please use `update_comment(str)` with null value as the argument instead.
"""
return FilesetChange.RemoveComment()
return FilesetChange.UpdateFilesetComment(None)

@dataclass
class RenameFileset:
Expand Down Expand Up @@ -279,7 +282,11 @@ def __str__(self):

@dataclass
class RemoveComment:
"""A fileset change to remove comment from the fileset."""
"""A fileset change to remove comment from the fileset.
Deprecated:
Please use `UpdateFilesetComment(str)` with null value as the argument instead.
"""

def __eq__(self, other) -> bool:
"""Compares this RemoveComment instance with another object for equality.
Expand Down
2 changes: 1 addition & 1 deletion clients/client-python/gravitino/catalog/fileset_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,5 @@ def to_fileset_update_request(change: FilesetChange):
if isinstance(change, FilesetChange.RemoveProperty):
return FilesetUpdateRequest.RemoveFilesetPropertyRequest(change.property())
if isinstance(change, FilesetChange.RemoveComment):
return FilesetUpdateRequest.RemoveFilesetCommentRequest()
return FilesetUpdateRequest.UpdateFilesetCommentRequest(None)
raise ValueError(f"Unknown change type: {type(change).__name__}")
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def catalog_change(self):
return CatalogChange.update_comment(self._new_comment)

def validate(self):
if not self._new_comment:
raise ValueError('"newComment" field is required and cannot be empty')
"""Validates the fields of the request. Always pass."""
pass

@dataclass
class SetCatalogPropertyRequest(CatalogUpdateRequestBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,8 @@ def __init__(self, new_comment: str):
self._new_comment = new_comment

def validate(self):
"""Validates the fields of the request.
Raises:
IllegalArgumentException if the new comment is not set.
"""
if not self._new_comment:
raise ValueError('"new_comment" field is required and cannot be empty')
"""Validates the fields of the request. Always pass."""
pass

def fileset_change(self):
"""Returns the fileset change"""
Expand Down Expand Up @@ -149,7 +144,11 @@ def fileset_change(self):

@dataclass
class RemoveFilesetCommentRequest(FilesetUpdateRequestBase):
"""Represents a request to remove comment from a Fileset."""
"""Represents a request to remove comment from a Fileset.
Deprecated:
Please use `UpdateFilesetCommentRequest` with null value as the argument instead.
"""

def __init__(self):
super().__init__("removeComment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ def __init__(self, new_comment: str):
self._new_comment = new_comment

def validate(self):
"""Validates the fields of the request.
Raises:
IllegalArgumentException if the new comment is not set.
"""
if not self._new_comment:
raise ValueError('"newComment" field is required and cannot be empty')
"""Validates the fields of the request. Always pass."""
pass

def metalake_change(self):
return MetalakeChange.update_comment(self._new_comment)
Expand Down
4 changes: 4 additions & 0 deletions clients/client-python/tests/integration/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def test_alter_catalog(self):
catalog.properties().get(catalog_properties_new_key),
catalog_properties_new_value,
)
# test nullable comment
null_comment_catalog = self.gravitino_client.alter_catalog(self.catalog_name, CatalogChange.update_comment(None))
self.assertIsNone(null_comment_catalog.comment())

self.catalog_name = self.catalog_name + "_new"

def test_drop_catalog(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_alter_fileset(self):
self.assertEqual(fileset_new.comment(), fileset_new_comment)

fileset_comment_removed = catalog.as_fileset_catalog().alter_fileset(
self.fileset_ident, FilesetChange.remove_comment()
self.fileset_ident, FilesetChange.update_comment(None)
)
self.assertEqual(fileset_comment_removed.name(), self.fileset_name)
self.assertIsNone(fileset_comment_removed.comment())
Expand Down
3 changes: 3 additions & 0 deletions clients/client-python/tests/integration/test_metalake.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def test_alter_metalake(self):
)
self.assertTrue(self.metalake_properties_key1 not in metalake.properties())

null_comment_metalake = self.gravitino_admin_client.alter_metalake(self.metalake_name, MetalakeChange.update_comment(None))
self.assertIsNone(null_comment_metalake.comment())

def drop_metalake(self, metalake_name: str) -> bool:
return self.gravitino_admin_client.drop_metalake(metalake_name, True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,9 @@ public UpdateCatalogCommentRequest() {
this(null);
}

/**
* Validates the fields of the request.
*
* @throws IllegalArgumentException if the new comment is not set.
*/
/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment),
"\"newComment\" field is required and cannot be empty");
}
public void validate() throws IllegalArgumentException {}

@Override
public CatalogChange catalogChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,9 @@ public FilesetChange filesetChange() {
return FilesetChange.updateComment(newComment);
}

/**
* Validates the request.
*
* @throws IllegalArgumentException if the request is invalid.
*/
/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment),
"\"newComment\" field is required and cannot be empty");
}
public void validate() throws IllegalArgumentException {}
}

/** The fileset update request for setting the properties of a fileset. */
Expand Down Expand Up @@ -189,6 +181,7 @@ public void validate() throws IllegalArgumentException {
@EqualsAndHashCode
@NoArgsConstructor(force = true)
@ToString
@Deprecated
class RemoveFilesetCommentRequest implements FilesetUpdateRequest {

/** @return The fileset change. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,9 @@ public UpdateMetalakeCommentRequest() {
this(null);
}

/**
* Validates the fields of the request.
*
* @throws IllegalArgumentException if the new comment is not set.
*/
/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment),
"\"newComment\" field is required and cannot be empty");
}
public void validate() throws IllegalArgumentException {}

@Override
public MetalakeChange metalakeChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,9 @@ public UpdateTableCommentRequest() {
this(null);
}

/**
* Validates the request.
*
* @throws IllegalArgumentException If the request is invalid, this exception is thrown.
*/
/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment),
"\"newComment\" field is required and cannot be empty");
}
public void validate() throws IllegalArgumentException {}

/**
* Returns the table change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ public TagChange tagChange() {
return TagChange.updateComment(newComment);
}

/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment), "\"newComment\" must not be blank");
}
public void validate() throws IllegalArgumentException {}
}

/** The tag update request for setting a tag property. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,9 @@ public UpdateTopicCommentRequest() {
this(null);
}

/**
* Validates the request.
*
* @throws IllegalArgumentException If the request is invalid, this exception is thrown.
*/
/** Validates the fields of the request. Always pass. */
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(
StringUtils.isNotBlank(newComment),
"\"newComment\" field is required and cannot be empty");
}
public void validate() throws IllegalArgumentException {}

/**
* Returns the topic change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void testCreateAndAlterFileset() {
Assertions.assertEquals(fileset1.name(), alteredFileset2.name());
Assertions.assertEquals("new comment", alteredFileset2.comment());

FilesetChange[] changes3 = new FilesetChange[] {FilesetChange.removeComment()};
FilesetChange[] changes3 = new FilesetChange[] {FilesetChange.updateComment(null)};

Fileset alteredFileset3 = filesetOperationDispatcher.alterFileset(filesetIdent1, changes3);
Assertions.assertEquals(fileset1.name(), alteredFileset3.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes)
filesets.remove(ident);
} else if (change instanceof FilesetChange.UpdateFilesetComment) {
newComment = ((FilesetChange.UpdateFilesetComment) change).getNewComment();
} else if (change instanceof FilesetChange.RemoveComment) {
newComment = null;
} else {
throw new IllegalArgumentException("Unsupported fileset change: " + change);
}
Expand Down
Loading

0 comments on commit 53f45db

Please sign in to comment.