Skip to content

Commit

Permalink
[apache#1339] feat(table): Add index for tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clearvive authored and Clearvive committed Jan 22, 2024
1 parent eaf44fa commit ae172e7
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 72 deletions.
18 changes: 15 additions & 3 deletions api/src/main/java/com/datastrato/gravitino/rel/indexes/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ public interface Index {
/** @return The name of the index. */
String name();

/** @return The field name under the table contained in the index. eg: table.id */
/**
* @return The field name under the table contained in the index. it is the column names, could be
* "a.b.c" for nested column, but normally it could only be "a".
*/
String[][] fieldNames();

enum IndexType {
/** Primary key index. */
/**
* Primary key index. Primary key in a relational database is a field or a combination of fields
* that uniquely identifies each record in a table. It serves as a unique identifier for each
* row, ensuring that no two rows have the same key.
*/
PRIMARY_KEY,
/** Unique key index. */
/**
* Unique key index. A unique key in a relational database is a field or a combination of fields
* that ensures each record in a table has a distinct value or combination of values. Unlike a
* primary key, a unique key allows for the presence of null values, but it still enforces the
* constraint that no two records can have the same unique key value(s).
*/
UNIQUE_KEY,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,11 @@
public class Indexes {

public static final Index[] EMPTY_INDEXES = new Index[0];
private static final String UNIQUE_KEY_FORMAT = "%s_uk";
private static final String PRIMARY_KEY_FORMAT = "%s_pk";

public static Index unique(String fieldName) {
String[] fieldNames = {fieldName};
return unique(new String[][] {fieldNames});
}

public static Index unique(String[][] fieldNames) {
return unique(String.format(UNIQUE_KEY_FORMAT, fieldNames[0][0]), fieldNames);
}

public static Index unique(String name, String[][] fieldNames) {
return of(Index.IndexType.UNIQUE_KEY, name, fieldNames);
}

public static Index primary(String fieldName) {
String[] fieldNames = {fieldName};
return primary(new String[][] {fieldNames});
}

public static Index primary(String[][] fieldNames) {
return primary(String.format(PRIMARY_KEY_FORMAT, fieldNames[0][0]), fieldNames);
}

public static Index primary(String name, String[][] fieldNames) {
return of(Index.IndexType.PRIMARY_KEY, name, fieldNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogExc
}

/**
* cre Creates a new schema with the provided identifier, comment, and metadata.
* Creates a new schema with the provided identifier, comment, and metadata.
*
* @param ident The identifier of the schema to create.
* @param comment The comment for the schema.
Expand Down Expand Up @@ -568,7 +568,7 @@ public Table createTable(
throws NoSuchSchemaException, TableAlreadyExistsException {
Preconditions.checkArgument(
indexes.length == 0,
"Hive-catalog does not support indexes, current Gravitino hive-catalog only supports Hive 2.x");
"Hive-catalog does not support indexes, since indexing was removed since 3.0");
NameIdentifier schemaIdent = NameIdentifier.of(tableIdent.namespace().levels());

validatePartitionForCreate(columns, partitioning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public Table createTable(
SortOrder[] sortOrders,
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException {
Preconditions.checkArgument(indexes.length == 0, "jdbc-catalog does not support indexes");
Preconditions.checkArgument(indexes.length == 0, "Jdbc-catalog does not support indexes");
Preconditions.checkArgument(
null == distribution || distribution == Distributions.NONE,
"jdbc-catalog does not support distribution");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public Table createTable(
SortOrder[] sortOrders,
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException {
Preconditions.checkArgument(indexes.length == 0, "iceberg-catalog does not support indexes");
Preconditions.checkArgument(indexes.length == 0, "Iceberg-catalog does not support indexes");
try {
if (!Distributions.NONE.equals(distribution)) {
throw new UnsupportedOperationException("Iceberg does not support distribution");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.datastrato.gravitino.rel.expressions.distributions.Strategy;
import com.datastrato.gravitino.rel.expressions.sorts.SortDirection;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.indexes.Indexes;
import com.datastrato.gravitino.rel.types.Type;
import com.datastrato.gravitino.rel.types.Types;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -360,7 +361,8 @@ public void testCreateTable() throws JsonProcessingException {
Collections.emptyMap(),
sortOrderDTOs,
DistributionDTO.NONE,
EMPTY_PARTITIONING);
EMPTY_PARTITIONING,
Indexes.EMPTY_INDEXES);
TableResponse resp = new TableResponse(expectedTable);
buildMockResource(Method.POST, tablePath, req, resp, SC_OK);

Expand Down Expand Up @@ -478,7 +480,8 @@ public void testCreatePartitionedTable() throws JsonProcessingException {
Collections.emptyMap(),
SortOrderDTO.EMPTY_SORT,
DistributionDTO.NONE,
EMPTY_PARTITIONING);
EMPTY_PARTITIONING,
Indexes.EMPTY_INDEXES);
TableResponse resp = new TableResponse(expectedTable);
buildMockResource(Method.POST, tablePath, req, resp, SC_OK);

Expand Down Expand Up @@ -510,7 +513,8 @@ public void testCreatePartitionedTable() throws JsonProcessingException {
Collections.emptyMap(),
SortOrderDTO.EMPTY_SORT,
DistributionDTO.NONE,
partitioning);
partitioning,
Indexes.EMPTY_INDEXES);
resp = new TableResponse(expectedTable);
buildMockResource(Method.POST, tablePath, req, resp, SC_OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import com.datastrato.gravitino.rel.indexes.Index;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

public class IndexDTO implements Index {

private final IndexType indexType;
private final String name;
private final String[][] fieldNames;
private IndexType indexType;
private String name;
private String[][] fieldNames;

public IndexDTO() {}

public IndexDTO(IndexType indexType, String name, String[][] fieldNames) {
this.indexType = indexType;
Expand Down Expand Up @@ -64,9 +67,10 @@ public S withFieldNames(String[][] fieldNames) {

public IndexDTO build() {
Preconditions.checkArgument(indexType != null, "Index type cannot be null");
Preconditions.checkArgument(name != null, "Index name cannot be null");
Preconditions.checkArgument(StringUtils.isNotBlank(name), "Index name cannot be blank");
Preconditions.checkArgument(
fieldNames != null, "The index must be set with corresponding column names");
fieldNames != null && fieldNames.length > 0,
"The index must be set with corresponding column names");
return new IndexDTO(indexType, name, fieldNames);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.datastrato.gravitino.dto.rel.expressions.FunctionArg;
import com.datastrato.gravitino.dto.rel.partitions.Partitioning;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
import com.datastrato.gravitino.rest.RESTRequest;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -64,38 +63,7 @@ public class TableCreateRequest implements RESTRequest {
private final Index[] indexes;

public TableCreateRequest() {
this(null, null, null, null, null, null, null);
}

public TableCreateRequest(
String name, String comment, ColumnDTO[] columns, Map<String, String> properties) {
this(
name,
comment,
columns,
properties,
new SortOrderDTO[0],
DistributionDTO.NONE,
new Partitioning[0]);
}

public TableCreateRequest(
String name,
String comment,
ColumnDTO[] columns,
Map<String, String> properties,
SortOrderDTO[] sortOrders,
DistributionDTO distribution,
Partitioning[] partitioning) {
this(
name,
comment,
columns,
properties,
sortOrders,
distribution,
partitioning,
Indexes.EMPTY_INDEXES);
this(null, null, null, null, null, null, null, null);
}

public TableCreateRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.datastrato.gravitino.rel.expressions.sorts.SortDirection;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.indexes.Indexes;
import com.datastrato.gravitino.rel.types.Type;
import com.datastrato.gravitino.rel.types.Types;
import com.datastrato.gravitino.rest.RESTUtils;
Expand Down Expand Up @@ -197,7 +198,8 @@ public void testCreateTable() {
ImmutableMap.of("k1", "v1"),
sortOrderDTOs,
distributionDTO,
Partitioning.EMPTY_PARTITIONING);
Partitioning.EMPTY_PARTITIONING,
Indexes.EMPTY_INDEXES);

Response resp =
target(tablePath(metalake, catalog, schema))
Expand Down Expand Up @@ -237,7 +239,8 @@ public void testCreateTable() {
ImmutableMap.of("k1", "v1"),
sortOrderDTOs,
distributionDTO,
Partitioning.EMPTY_PARTITIONING);
Partitioning.EMPTY_PARTITIONING,
Indexes.EMPTY_INDEXES);

resp =
target(tablePath(metalake, catalog, schema))
Expand Down Expand Up @@ -328,7 +331,8 @@ public void testCreatePartitionedTable() {
ImmutableMap.of("k1", "v1"),
SortOrderDTO.EMPTY_SORT,
DistributionDTO.NONE,
partitioning);
partitioning,
Indexes.EMPTY_INDEXES);

Response resp =
target(tablePath(metalake, catalog, schema))
Expand Down Expand Up @@ -376,7 +380,8 @@ public void testCreatePartitionedTable() {
ImmutableMap.of("k1", "v1"),
SortOrderDTO.EMPTY_SORT,
null,
new Partitioning[] {errorPartition});
new Partitioning[] {errorPartition},
Indexes.EMPTY_INDEXES);
resp =
target(tablePath(metalake, catalog, schema))
.request(MediaType.APPLICATION_JSON_TYPE)
Expand Down

0 comments on commit ae172e7

Please sign in to comment.