Skip to content

Commit

Permalink
List tables command (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshrajamani authored Oct 8, 2024
1 parent 2641d2e commit ad246c6
Show file tree
Hide file tree
Showing 19 changed files with 789 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum CommandName {
FIND_ONE("findOne"),
INSERT_MANY("insertMany"),
INSERT_ONE("insertOne"),
LIST_TABLES("listTables"),
UPDATE_MANY("updateMany"),
UPDATE_ONE("updateOne"),
BEGIN_OFFLINE_SESSION("beginOfflineSession"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public enum CommandStatus {
/** Status for reporting existing collections. */
@JsonProperty("collections")
EXISTING_COLLECTIONS,
/** Status for reporting existing collections. */
@JsonProperty("tables")
EXISTING_TABLES,

/**
* List of response entries, one for each document we tried to insert with {@code insertMany}
* command. Each entry has 2 mandatory fields: {@code _id} (document id), and {@code status} (one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.stargate.sgv2.jsonapi.api.model.command.impl.CreateTableCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.DropTableCommand;
import io.stargate.sgv2.jsonapi.api.model.command.impl.ListTablesCommand;

/** Interface for all commands executed against a keyspace. */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value = CreateTableCommand.class),
@JsonSubTypes.Type(value = DropTableCommand.class)
@JsonSubTypes.Type(value = DropTableCommand.class),
@JsonSubTypes.Type(value = ListTablesCommand.class),
})
public interface TableOnlyCommand extends KeyspaceCommand {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.stargate.sgv2.jsonapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.stargate.sgv2.jsonapi.api.model.command.TableOnlyCommand;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Schema(description = "Command that lists all available tables in a namespace.")
@JsonTypeName("listTables")
public record ListTablesCommand(Options options) implements TableOnlyCommand {
public record Options(
@Schema(
description = "include table properties.",
type = SchemaType.BOOLEAN,
implementation = Boolean.class)
boolean explain) {}

/** {@inheritDoc} */
@Override
public CommandName commandName() {
return CommandName.LIST_TABLES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.stargate.sgv2.jsonapi.api.model.command.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.stargate.sgv2.jsonapi.api.model.command.table.definition.datatype.ColumnType;
import io.stargate.sgv2.jsonapi.api.model.command.table.definition.datatype.ComplexTypes;
import java.io.IOException;

/**
* Custom serializer to encode the column type to the JSON payload This is required because
* composite and custom column types may need additional properties to be serialized
*/
public class ColumnDefinitionSerializer extends JsonSerializer<ColumnType> {

@Override
public void serialize(
ColumnType columnType, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("type", columnType.getApiName());
if (columnType instanceof ComplexTypes.MapType mt) {
jsonGenerator.writeStringField("keyType", mt.keyTypeName());
jsonGenerator.writeStringField("valueType", mt.valueTypeName());
} else if (columnType instanceof ComplexTypes.ListType lt) {
jsonGenerator.writeStringField("valueType", lt.valueTypeName());
} else if (columnType instanceof ComplexTypes.SetType st) {
jsonGenerator.writeStringField("valueType", st.valueTypeName());
} else if (columnType instanceof ComplexTypes.VectorType vt) {
jsonGenerator.writeNumberField("dimension", vt.getDimension());
if (vt.getVectorConfig() != null)
jsonGenerator.writeObjectField("service", vt.getVectorConfig());
} else if (columnType instanceof ComplexTypes.UnsupportedType ut) {
jsonGenerator.writeObjectField(
"apiSupport", new ApiSupport(false, false, false, ut.cqlFormat()));
}
jsonGenerator.writeEndObject();
}

public record ApiSupport(
boolean createTable, boolean insert, boolean read, String cqlDefinition) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.stargate.sgv2.jsonapi.api.model.command.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.stargate.sgv2.jsonapi.api.model.command.table.definition.PrimaryKey;
import java.io.IOException;

/**
* Custom serializer to encode the column type to the JSON payload This is required because
* composite and custom column types may need additional properties to be serialized
*/
public class OrderingKeysSerializer extends JsonSerializer<PrimaryKey.OrderingKey[]> {

@Override
public void serialize(
PrimaryKey.OrderingKey[] orderingKeys,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
if (orderingKeys != null) {
for (PrimaryKey.OrderingKey orderingKey : orderingKeys) {
jsonGenerator.writeNumberField(
orderingKey.column(), orderingKey.order() == PrimaryKey.OrderingKey.Order.ASC ? 1 : -1);
}
}
jsonGenerator.writeEndObject();
}

/**
* This is used when a unsupported type column is present in a table. How to use this class will
* evolve as different unsupported types are analyzed.
*
* @param createTable
* @param insert
* @param read
* @param cqlDefinition
*/
private record ApiSupport(
boolean createTable, boolean insert, boolean read, String cqlDefinition) {}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.stargate.sgv2.jsonapi.api.model.command.table.definition;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.stargate.sgv2.jsonapi.api.model.command.deserializers.PrimaryKeyDeserializer;
import io.stargate.sgv2.jsonapi.api.model.command.serializer.OrderingKeysSerializer;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
Expand All @@ -16,9 +19,15 @@
// implementation = Object.class,
// description = "Represents the table primary key")
public record PrimaryKey(
@NotNull @Schema(description = "Columns that make the partition keys", type = SchemaType.ARRAY)
@NotNull
@Schema(description = "Columns that make the partition keys", type = SchemaType.ARRAY)
@JsonProperty("partitionBy")
@JsonInclude(JsonInclude.Include.NON_NULL)
String[] keys,
@Nullable @Schema(description = "Columns that make the ordering keys", type = SchemaType.ARRAY)
@Nullable
@Schema(description = "Columns that make the ordering keys", type = SchemaType.ARRAY)
@JsonProperty("partitionSort")
@JsonSerialize(using = OrderingKeysSerializer.class)
OrderingKey[] orderingKeys) {

public record OrderingKey(String column, Order order) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package io.stargate.sgv2.jsonapi.api.model.command.table.definition.datatype;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.stargate.sgv2.jsonapi.api.model.command.deserializers.ColumnDefinitionDeserializer;
import io.stargate.sgv2.jsonapi.api.model.command.impl.VectorizeConfig;
import io.stargate.sgv2.jsonapi.api.model.command.serializer.ColumnDefinitionSerializer;
import io.stargate.sgv2.jsonapi.exception.SchemaException;
import io.stargate.sgv2.jsonapi.service.schema.tables.ApiDataType;
import java.util.List;
import java.util.Map;

/** Interface for column types. This is used to define the type of a column in a table. */
@JsonDeserialize(using = ColumnDefinitionDeserializer.class)
@JsonSerialize(using = ColumnDefinitionSerializer.class)
public interface ColumnType {
// Returns api data type.
ApiDataType getApiDataType();

/*
Returns the name of the column type to be used in the API request.
*/
default String getApiName() {
return getApiDataType().getApiName();
}

static List<String> getSupportedTypes() {
return List.of(
"ascii",
Expand Down Expand Up @@ -46,42 +56,6 @@ static ColumnType fromString(
// TODO: the name of the type should be a part of the ColumnType interface, and use a map for
// the lookup
switch (type) {
case "ascii":
return PrimitiveTypes.ASCII;
case "bigint":
return PrimitiveTypes.BIGINT;
case "blob":
return PrimitiveTypes.BINARY;
case "boolean":
return PrimitiveTypes.BOOLEAN;
case "date":
return PrimitiveTypes.DATE;
case "decimal":
return PrimitiveTypes.DECIMAL;
case "double":
return PrimitiveTypes.DOUBLE;
case "duration":
return PrimitiveTypes.DURATION;
case "float":
return PrimitiveTypes.FLOAT;
case "inet":
return PrimitiveTypes.INET;
case "int":
return PrimitiveTypes.INT;
case "smallint":
return PrimitiveTypes.SMALLINT;
case "text":
return PrimitiveTypes.TEXT;
case "time":
return PrimitiveTypes.TIME;
case "timestamp":
return PrimitiveTypes.TIMESTAMP;
case "tinyint":
return PrimitiveTypes.TINYINT;
case "uuid":
return PrimitiveTypes.UUID;
case "varint":
return PrimitiveTypes.VARINT;
case "map":
{
if (keyType == null || valueType == null) {
Expand Down Expand Up @@ -134,6 +108,10 @@ static ColumnType fromString(
}
default:
{
ColumnType columnType = PrimitiveTypes.fromString(type);
if (columnType != null) {
return columnType;
}
Map<String, String> errorMessageFormattingValues =
Map.of(
"type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public ApiDataType getApiDataType() {
(PrimitiveApiDataType) keyType.getApiDataType(),
(PrimitiveApiDataType) valueType.getApiDataType());
}

public String keyTypeName() {
return keyType.getApiDataType().getApiName();
}

public String valueTypeName() {
return valueType.getApiDataType().getApiName();
}
}

/** A list type implementation */
Expand All @@ -38,6 +46,10 @@ public ListType(ColumnType valueType) {
public ApiDataType getApiDataType() {
return new ComplexApiDataType.ListType((PrimitiveApiDataType) valueType.getApiDataType());
}

public String valueTypeName() {
return valueType.getApiDataType().getApiName();
}
}

/** A set type implementation */
Expand All @@ -52,6 +64,10 @@ public SetType(ColumnType valueType) {
public ApiDataType getApiDataType() {
return new ComplexApiDataType.SetType((PrimitiveApiDataType) valueType.getApiDataType());
}

public String valueTypeName() {
return valueType.getApiDataType().getApiName();
}
}

/* Vector type */
Expand Down Expand Up @@ -81,4 +97,30 @@ public int getDimension() {
return vectorSize;
}
}

/**
* Unsupported type implementation, returned in response when cql table has unsupported format
* column
*/
public static class UnsupportedType implements ColumnType {
private final String cqlFormat;

public UnsupportedType(String cqlFormat) {
this.cqlFormat = cqlFormat;
}

@Override
public ApiDataType getApiDataType() {
throw new UnsupportedOperationException("Unsupported type");
}

@Override
public String getApiName() {
return "UNSUPPORTED";
}

public String cqlFormat() {
return cqlFormat;
}
}
}
Loading

0 comments on commit ad246c6

Please sign in to comment.