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 19, 2024
1 parent 3c62f53 commit edc6f0e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/src/main/java/com/datastrato/gravitino/rel/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.datastrato.gravitino.Auditable;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.datastrato.gravitino.rel.expressions.Indexes.Indexes;
import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
Expand Down Expand Up @@ -51,7 +52,7 @@ default Distribution distribution() {

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

/** @return The comment of the table. Null is returned if no comment is set. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ default Table createTable(
}

/**
* Create a partitioned table in the catalog.
* Create a table in the catalog.
*
* @param ident A table identifier.
* @param columns The columns of the new table.
Expand All @@ -222,7 +222,7 @@ default Table createTable(
* @param sortOrders The sort orders of the table
* @param partitions The table partitioning.
* @param indexes The table indexes.
* @return Fhe created table metadata.
* @return The created table metadata.
* @throws NoSuchSchemaException If the schema does not exist.
* @throws TableAlreadyExistsException If the table already exists.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package com.datastrato.gravitino.rel.expressions.Indexes;

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

IndexType type();
Expand All @@ -17,7 +20,9 @@ public interface Index {
String[][] fieldNames();

enum IndexType {
/** Primary key index. */
PRIMARY_KEY,
/** Unique key index. */
UNIQUE_KEY,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.datastrato.gravitino.rel.expressions.Indexes;

/** Helper methods to create index to pass into Gravitino. */
public class Indexes {

public static Index[] EMPTY_INDEXES = new Index[0];

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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.datastrato.gravitino.rel.TableCatalog;
import com.datastrato.gravitino.rel.TableChange;
import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.datastrato.gravitino.rel.expressions.Indexes.Indexes;
import com.datastrato.gravitino.rel.expressions.NamedReference;
import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
Expand Down Expand Up @@ -561,7 +562,8 @@ public Table createTable(
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException {
Preconditions.checkArgument(
null == indexes || indexes.length == 0, "hive-catalog does not support indexes");
indexes == Indexes.EMPTY_INDEXES,
"Hive-catalog does not support indexes,The current Gravitino hive-catalog only supports Hive 2.x");
NameIdentifier schemaIdent = NameIdentifier.of(tableIdent.namespace().levels());

validatePartitionForCreate(columns, partitioning);
Expand Down

0 comments on commit edc6f0e

Please sign in to comment.