Skip to content
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

[#1576] feat(api): add partition management API #1577

Merged
merged 10 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.exceptions;

/** Exception thrown when a partition with specified name is not existed. */
public class NoSuchPartitionException extends NotFoundException {

public NoSuchPartitionException(String message) {
super(message);
}

public NoSuchPartitionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.exceptions;

/** Exception thrown when a partition with specified name already exists. */
public class PartitionAlreadyExistsException extends AlreadyExistsException {

public PartitionAlreadyExistsException(String message) {
super(message);
}

public PartitionAlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel;

import com.datastrato.gravitino.exceptions.NoSuchPartitionException;
import com.datastrato.gravitino.exceptions.PartitionAlreadyExistsException;
import com.datastrato.gravitino.rel.partitions.Partition;

public interface SupportsPartitions {

/**
* List all partition names of the table
*
* @return The list of partition names
*/
String[] listPartitionNames();
mchades marked this conversation as resolved.
Show resolved Hide resolved

/**
* List all partitions
*
* @return The list of partitions
*/
Partition[] listPartitions();
mchades marked this conversation as resolved.
Show resolved Hide resolved
mchades marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get a partition by partition name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd better add more examples about how to get list partition or range partition as we discussed offline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

*
* @param partitionName the name of the partition
* @return the partition
* @throws NoSuchPartitionException if the partition does not exist
*/
Partition getPartition(String partitionName) throws NoSuchPartitionException;

/**
* Check if a partition exists.
*
* @param partitionName The name of the partition.
FANNG1 marked this conversation as resolved.
Show resolved Hide resolved
* @return True if the partition exists, false otherwise.
*/
default boolean partitionExists(String partitionName) {
try {
getPartition(partitionName);
return true;
} catch (NoSuchPartitionException e) {
return false;
}
}

/**
* Add a partition with specified name and properties to the table.
*
* @param partition The partition to add.
* @return The created partition.
* @throws PartitionAlreadyExistsException If the partition already exists.
*/
Partition addPartition(Partition partition) throws PartitionAlreadyExistsException;
mchades marked this conversation as resolved.
Show resolved Hide resolved

/**
* Drop a partition with specified name.
*
* @param partitionName The identifier of the partition.
* @return true if a partition was deleted, false if the partition did not exist.
*/
boolean dropPartition(String partitionName);

/**
* If the table supports purging, drop a partition with specified name and completely remove
* partition data by skipping a trash.
*
* @param partitionName The name of the partition.
* @return true if a partition was deleted, false if the partition did not exist.
* @throws NoSuchPartitionException If the partition does not exist.
* @throws UnsupportedOperationException If partition purging is not supported.
*/
default boolean purgePartition(String partitionName)
throws NoSuchPartitionException, UnsupportedOperationException {
throw new UnsupportedOperationException("Partition purging is not supported");
}
}
11 changes: 11 additions & 0 deletions api/src/main/java/com/datastrato/gravitino/rel/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ default String comment() {
default Map<String, String> properties() {
return Collections.emptyMap();
}

/**
* Table method for working with partitions. If the table does not support partition operations,
* an {@link UnsupportedOperationException} is thrown.
*
* @return The partition support table.
* @throws UnsupportedOperationException If the table does not support partition operations.
*/
default SupportsPartitions supportPartitions() throws UnsupportedOperationException {
FANNG1 marked this conversation as resolved.
Show resolved Hide resolved
throw new UnsupportedOperationException("Table does not support partition operations.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel.partitions;

import com.datastrato.gravitino.rel.expressions.literals.Literal;

/**
* An identity partition represents a result of identity partitioning. For example, for Hive
* partition
*
* <pre>`PARTITION (dt='2008-08-08',country='us')`</pre>
*
* its partition name is "dt=2008-08-08/country=us", field names are [["dt"], ["country"]] and
* values are ["2008-08-08", "us"].
*/
public interface IdentityPartition extends Partition {

/** @return The field names of the identity partition. */
String[][] fieldNames();

/**
* @return The values of the identity partition. The values are in the same order as the field
* names.
*/
Literal<?>[] values();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel.partitions;

import com.datastrato.gravitino.rel.expressions.literals.Literal;

/**
* A list partition represents a result of list partitioning. For example, for list partition
*
* <pre>
* `PARTITION p202204_California VALUES IN (
* ("2022-04-01", "Los Angeles"),
* ("2022-04-01", "San Francisco")
* )`
* </pre>
*
* its name is "p202204_California" and lists are [["2022-04-01","Los Angeles"], ["2022-04-01", "San
* Francisco"]].
*/
public interface ListPartition extends Partition {

/** @return The values of the list partition. */
Literal<?>[][] lists();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel.partitions;

import com.datastrato.gravitino.rel.Table;
import java.util.Map;

/**
* A partition represents a result of partitioning a table. The partition can be either a {@link
* IdentityPartition}, {@link ListPartition} or {@link RangePartition}. It depends on the {@link
* Table#partitioning()}.
*/
public interface Partition {

/** @return The name of the partition. */
String name();

/** @return The properties of the partition, such as statistics, location, etc. */
Map<String, String> properties();
}
Loading
Loading