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 f145099 commit 137dd1a
Show file tree
Hide file tree
Showing 17 changed files with 608 additions and 224 deletions.
10 changes: 10 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 @@ -12,6 +12,8 @@
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -48,6 +50,14 @@ default Distribution distribution() {
return Distributions.NONE;
}

/**
* @return The indexes of the table. If no indexes are specified, Indexes.EMPTY_INDEXES is
* returned.
*/
default Index[] index() {
return Indexes.EMPTY_INDEXES;
}

/** @return The comment of the table. Null is returned if no comment is set. */
@Nullable
default String comment() {
Expand Down
40 changes: 39 additions & 1 deletion api/src/main/java/com/datastrato/gravitino/rel/TableCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.indexes.Index;
import com.datastrato.gravitino.rel.indexes.Indexes;
import java.util.Map;

/**
Expand Down Expand Up @@ -197,14 +199,50 @@ default Table createTable(
* @throws NoSuchSchemaException If the schema does not exist.
* @throws TableAlreadyExistsException If the table already exists.
*/
Table createTable(
default Table createTable(
NameIdentifier ident,
Column[] columns,
String comment,
Map<String, String> properties,
Transform[] partitions,
Distribution distribution,
SortOrder[] sortOrders)
throws NoSuchSchemaException, TableAlreadyExistsException {
return createTable(
ident,
columns,
comment,
properties,
partitions,
distribution,
sortOrders,
Indexes.EMPTY_INDEXES);
}

/**
* Create a table in the catalog.
*
* @param ident A table identifier.
* @param columns The columns of the new table.
* @param comment The table comment.
* @param properties The table properties.
* @param distribution The distribution of the table
* @param sortOrders The sort orders of the table
* @param partitions The table partitioning.
* @param indexes The table indexes.
* @return The created table metadata.
* @throws NoSuchSchemaException If the schema does not exist.
* @throws TableAlreadyExistsException If the table already exists.
*/
Table createTable(
NameIdentifier ident,
Column[] columns,
String comment,
Map<String, String> properties,
Transform[] partitions,
Distribution distribution,
SortOrder[] sortOrders,
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException;

/**
Expand Down
29 changes: 29 additions & 0 deletions api/src/main/java/com/datastrato/gravitino/rel/indexes/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/

package com.datastrato.gravitino.rel.indexes;

/**
* The Index interface defines methods for implementing table index columns. Currently, settings for
* PRIMARY_KEY and UNIQUE_KEY are provided.
*/
public interface Index {

/** @return The type of the index. eg: PRIMARY_KEY and UNIQUE_KEY. */
IndexType type();

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

/** @return The field name under the table contained in the index. eg: table.id */
String[][] fieldNames();

enum IndexType {
/** Primary key index. */
PRIMARY_KEY,
/** Unique key index. */
UNIQUE_KEY,
}
}
104 changes: 104 additions & 0 deletions api/src/main/java/com/datastrato/gravitino/rel/indexes/Indexes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.rel.indexes;

/** Helper methods to create index to pass into Gravitino. */
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);
}

public static Index of(Index.IndexType indexType, String name, String[][] fieldNames) {
return IndexImpl.builder()
.withIndexType(indexType)
.withName(name)
.withFieldNames(fieldNames)
.build();
}

public static final class IndexImpl implements Index {
private final IndexType indexType;
private final String name;
private final String[][] fieldNames;

public IndexImpl(IndexType indexType, String name, String[][] fieldNames) {
this.indexType = indexType;
this.name = name;
this.fieldNames = fieldNames;
}

@Override
public IndexType type() {
return indexType;
}

@Override
public String name() {
return name;
}

@Override
public String[][] fieldNames() {
return fieldNames;
}

public static Builder builder() {
return new Builder();
}

/** Builder to create a index. */
public static class Builder {
protected IndexType indexType;
protected String name;
protected String[][] fieldNames;

public Indexes.IndexImpl.Builder withIndexType(IndexType indexType) {
this.indexType = indexType;
return this;
}

public Indexes.IndexImpl.Builder withName(String name) {
this.name = name;
return this;
}

public Indexes.IndexImpl.Builder withFieldNames(String[][] fieldNames) {
this.fieldNames = fieldNames;
return this;
}

public Index build() {
return new IndexImpl(indexType, name, fieldNames);
}
}
}
}
Loading

0 comments on commit 137dd1a

Please sign in to comment.