Skip to content

Commit

Permalink
[#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 9, 2024
1 parent ad6e2b9 commit 9adb618
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 4 deletions.
6 changes: 6 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 @@ -8,6 +8,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.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
Expand Down Expand Up @@ -48,6 +49,11 @@ default Distribution distribution() {
return Distributions.NONE;
}

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

/** @return The comment of the table. Null is returned if no comment is set. */
@Nullable
default String comment() {
Expand Down
32 changes: 31 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 @@ -27,6 +27,7 @@
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
import com.datastrato.gravitino.exceptions.NoSuchTableException;
import com.datastrato.gravitino.exceptions.TableAlreadyExistsException;
import com.datastrato.gravitino.rel.expressions.Indexes.Index;
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 @@ -197,14 +198,43 @@ 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, new Index[0]);
}

/**
* Create a partitioned 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 Fhe 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/

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

/** Represents an index on a table. */
public interface Index {

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

/** @return Returns the field name under the table contained in the index. eg: table.id */
String[][] fieldNames();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.datastrato.gravitino.dto.rel.partitions.Partitioning;
import com.datastrato.gravitino.rel.Column;
import com.datastrato.gravitino.rel.Table;
import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
Expand Down Expand Up @@ -42,6 +43,9 @@ public class TableDTO implements Table {
@JsonProperty("partitioning")
private Partitioning[] partitioning;

@JsonProperty("indexes")
private Index[] indexes;

private TableDTO() {}

/**
Expand All @@ -53,6 +57,7 @@ private TableDTO() {}
* @param properties The properties associated with the table.
* @param audit The audit information for the table.
* @param partitioning The partitioning of the table.
* @param indexes Teh indexes of the table.
*/
private TableDTO(
String name,
Expand All @@ -62,7 +67,8 @@ private TableDTO(
AuditDTO audit,
Partitioning[] partitioning,
DistributionDTO distribution,
SortOrderDTO[] sortOrderDTOs) {
SortOrderDTO[] sortOrderDTOs,
Index[] indexes) {
this.name = name;
this.comment = comment;
this.columns = columns;
Expand All @@ -71,6 +77,7 @@ private TableDTO(
this.distribution = distribution;
this.sortOrders = sortOrderDTOs;
this.partitioning = partitioning;
this.indexes = indexes;
}

@Override
Expand Down Expand Up @@ -113,6 +120,11 @@ public Distribution distribution() {
return distribution;
}

@Override
public Index[] index() {
return indexes;
}

/**
* Creates a new Builder to build a Table DTO.
*
Expand All @@ -136,6 +148,7 @@ public static class Builder<S extends Builder> {
protected SortOrderDTO[] sortOrderDTOs;
protected DistributionDTO distributionDTO;
protected Partitioning[] Partitioning;
protected Index[] indexes;

public Builder() {}

Expand Down Expand Up @@ -209,6 +222,11 @@ public S withPartitioning(Partitioning[] Partitioning) {
return (S) this;
}

public S withIndex(Index[] indexes) {
this.indexes = indexes;
return (S) this;
}

/**
* Builds a Table DTO based on the provided builder parameters.
*
Expand All @@ -222,7 +240,15 @@ public TableDTO build() {
Preconditions.checkArgument(audit != null, "audit cannot be null");

return new TableDTO(
name, comment, columns, properties, audit, Partitioning, distributionDTO, sortOrderDTOs);
name,
comment,
columns,
properties,
audit,
Partitioning,
distributionDTO,
sortOrderDTOs,
indexes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.dto.rel.indexes;

import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.google.common.base.Preconditions;

public class PrimaryKeyDTO implements Index {

private final String name;
private final String fieldName;

public PrimaryKeyDTO(String name, String fieldName) {
this.name = name;
this.fieldName = fieldName;
}

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

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

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

public static class Builder<S extends PrimaryKeyDTO.Builder> {

protected String name;
protected String fieldName;

public Builder() {}

public S withName(String name) {
this.name = name;
return (S) this;
}

public S withFieldName(String fieldName) {
this.fieldName = fieldName;
return (S) this;
}

public PrimaryKeyDTO build() {
Preconditions.checkNotNull(name, "Index name cannot be null");
Preconditions.checkNotNull(fieldName, "Primary key must specify the field name");
return new PrimaryKeyDTO(name, fieldName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.dto.rel.indexes;

import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.google.common.base.Preconditions;

public class UniqueDTO implements Index {

private final String name;
private final String fieldName;

public UniqueDTO(String name, String fieldName) {
this.name = name;
this.fieldName = fieldName;
}

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

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

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

public static class Builder<S extends Builder> {

protected String name;
protected String fieldName;

public Builder() {}

public S withName(String name) {
this.name = name;
return (S) this;
}

public S withFieldName(String fieldName) {
this.fieldName = fieldName;
return (S) this;
}

public PrimaryKeyDTO build() {
Preconditions.checkNotNull(name, "Index name cannot be null");
Preconditions.checkNotNull(fieldName, "Unique index must specify the field name");
return new PrimaryKeyDTO(name, fieldName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.datastrato.gravitino.dto.rel.SortOrderDTO;
import com.datastrato.gravitino.dto.rel.expressions.FunctionArg;
import com.datastrato.gravitino.dto.rel.partitions.Partitioning;
import com.datastrato.gravitino.rel.expressions.Indexes.Index;
import com.datastrato.gravitino.rest.RESTRequest;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -57,6 +58,10 @@ public class TableCreateRequest implements RESTRequest {
@JsonProperty("partitioning")
private final Partitioning[] partitioning;

@Nullable
@JsonProperty("indexes")
private final Index[] indexes;

public TableCreateRequest() {
this(null, null, null, null, null, null, null);
}
Expand All @@ -73,21 +78,34 @@ public TableCreateRequest(
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, new Index[0]);
}

public TableCreateRequest(
String name,
@Nullable String comment,
ColumnDTO[] columns,
@Nullable Map<String, String> properties,
@Nullable SortOrderDTO[] sortOrders,
@Nullable DistributionDTO distribution,
@Nullable Partitioning[] partitioning) {
@Nullable Partitioning[] partitioning,
@Nullable Index[] indexes) {
this.name = name;
this.columns = columns;
this.comment = comment;
this.properties = properties;
this.sortOrders = sortOrders;
this.distribution = distribution;
this.partitioning = partitioning;
this.indexes = indexes;
}

@Override
Expand Down Expand Up @@ -122,5 +140,9 @@ public void validate() throws IllegalArgumentException {
autoIncrementCols.size() <= 1,
"Only one column can be auto-incremented. There are multiple auto-increment columns in your table: "
+ autoIncrementColsStr);

if (indexes != null && indexes.length > 0) {
throw new UnsupportedOperationException("Support for indexing is currently not implemented");
}
}
}
Loading

0 comments on commit 9adb618

Please sign in to comment.