Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support empty options for DDL commands #1663

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

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

@Schema(description = "Command that alters the column definition in a table.")
@JsonTypeName("alterTable")
public record AlterTableCommand(AlterTableOperation operation) implements CollectionCommand {
public record AlterTableCommand(AlterTableOperation operation)
implements CollectionCommand, NoOptionsCommand {

@Override
public CommandName commandName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public record Options(

// This is index command option irrespective of column definition.
public record Options(
@Schema(
@Nullable
@Schema(
description = "Flag to ignore if index already exists",
defaultValue = "false",
type = SchemaType.BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.stargate.sgv2.jsonapi.api.model.command.TableOnlyCommand;
import io.stargate.sgv2.jsonapi.api.model.command.table.definition.TableDefinitionDesc;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import javax.annotation.Nullable;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

Expand All @@ -34,7 +34,8 @@ public record CreateTableCommand(
implements TableOnlyCommand {

public record Options(
@Schema(
@Nullable
@Schema(
description = "Flag to ignore if table already exists",
defaultValue = "false",
type = SchemaType.BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public record Options(

// This is index command option irrespective of column definition.
public record Options(
@Schema(
@Nullable
@Schema(
description = "Flag to ignore if index already exists",
defaultValue = "false",
type = SchemaType.BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public record DropIndexCommand(
implements NoOptionsCommand, KeyspaceCommand {

public record Options(
@Schema(
@Nullable
@Schema(
description = "Flag to ignore if index doesn't exists",
defaultValue = "false",
type = SchemaType.BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public record DropTableCommand(
implements NoOptionsCommand, KeyspaceCommand {

public record Options(
@Schema(
@Nullable
@Schema(
description = "Flag to ignore if table doesn't exists",
defaultValue = "false",
type = SchemaType.BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class CreateIndexCommandResolver implements CommandResolver<CreateIndexCo
public Class<CreateIndexCommand> getCommandClass() {
return CreateIndexCommand.class;
}
;

@Override
public Operation resolveTableCommand(
Expand Down Expand Up @@ -77,11 +76,11 @@ public Operation resolveTableCommand(
}

// Command level option for ifNotExists
boolean ifNotExists = false;
final CreateIndexCommand.Options commandOptions = command.options();
if (commandOptions != null && commandOptions.ifNotExists() != null) {
ifNotExists = commandOptions.ifNotExists();
}
boolean ifNotExists =
Optional.ofNullable(command.options())
.map(CreateIndexCommand.Options::ifNotExists)
.orElse(false);

final SchemaAttempt.SchemaRetryPolicy schemaRetryPolicy =
new SchemaAttempt.SchemaRetryPolicy(
ctx.getConfig(OperationsConfig.class).databaseConfig().ddlRetries(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,7 +31,10 @@ public Operation resolveKeyspaceCommand(
CommandContext<KeyspaceSchemaObject> ctx, CreateTableCommand command) {

String tableName = command.name();
boolean ifNotExists = command.options() != null ? command.options().ifNotExists() : false;
boolean ifNotExists =
Optional.ofNullable(command.options())
.map(CreateTableCommand.Options::ifNotExists)
.orElse(false);

// TODO: AARON: this is where the bad user column types like list of map will be caught and
// thrown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ public Operation resolveTableCommand(
}

// Command level option for ifNotExists
boolean ifNotExists = false;
final CreateVectorIndexCommand.Options commandOptions = command.options();
if (commandOptions != null && commandOptions.ifNotExists() != null) {
ifNotExists = commandOptions.ifNotExists();
}
boolean ifNotExists =
Optional.ofNullable(command.options())
.map(CreateVectorIndexCommand.Options::ifNotExists)
.orElse(false);

// Default Similarity Function to COSINE
if (similarityFunction == null && sourceModel == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import java.time.Duration;
import java.util.List;
import java.util.Optional;

/** Resolver for the {@link DropIndexCommand}. */
@ApplicationScoped
Expand All @@ -34,8 +35,10 @@ public Operation resolveKeyspaceCommand(
ctx.getConfig(OperationsConfig.class).databaseConfig().ddlRetries(),
Duration.ofMillis(
ctx.getConfig(OperationsConfig.class).databaseConfig().ddlRetryDelayMillis()));
final DropIndexCommand.Options options = command.options();
final boolean ifExists = (options != null) && options.ifExists();
final boolean ifExists =
Optional.ofNullable(command.options())
.map(DropIndexCommand.Options::ifExists)
.orElse(false);
CQLOption<Drop> cqlOption = null;
if (ifExists) {
cqlOption = CQLOption.ForDrop.ifExists();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.stargate.sgv2.jsonapi.service.resolver;

import com.datastax.oss.driver.api.querybuilder.schema.Drop;
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.impl.DropTableCommand;
import io.stargate.sgv2.jsonapi.config.DebugModeConfig;
Expand All @@ -16,6 +17,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import java.time.Duration;
import java.util.List;
import java.util.Optional;

/** Resolver for the {@link DropTableCommand}. */
@ApplicationScoped
Expand All @@ -28,14 +30,16 @@ public Class<DropTableCommand> getCommandClass() {
@Override
public Operation resolveKeyspaceCommand(
CommandContext<KeyspaceSchemaObject> ctx, DropTableCommand command) {
final DropTableCommand.Options options = command.options();
final boolean ifExists = (options != null) && options.ifExists();
final boolean ifExists =
Optional.ofNullable(command.options())
.map(DropTableCommand.Options::ifExists)
.orElse(false);
final SchemaAttempt.SchemaRetryPolicy schemaRetryPolicy =
new SchemaAttempt.SchemaRetryPolicy(
ctx.getConfig(OperationsConfig.class).databaseConfig().ddlRetries(),
Duration.ofMillis(
ctx.getConfig(OperationsConfig.class).databaseConfig().ddlRetryDelayMillis()));
CQLOption cqlOption = null;
CQLOption<Drop> cqlOption = null;
if (ifExists) {
cqlOption = CQLOption.ForDrop.ifExists();
}
Expand Down
Loading