Skip to content

Allow use of table options not registered in TableOption and extend known TableOptions #1586

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

Closed
Closed
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 @@ -44,6 +44,7 @@
* @author John Blum
* @author Vagif Zeynalov
* @author Mikhail Polivakha
* @author Seungho Kang
*/
public class CassandraAdminTemplate extends CassandraTemplate implements CassandraAdminOperations {

Expand Down Expand Up @@ -115,8 +116,10 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e

if (!CollectionUtils.isEmpty(optionsByName)) {
optionsByName.forEach((key, value) -> {
TableOption tableOption = TableOption.valueOfIgnoreCase(key);
if (tableOption.requiresValue()) {
TableOption tableOption = TableOption.findByNameIgnoreCase(key);
if (tableOption == null) {
addRawTableOption(key, value, createTableSpecification);
} else if (tableOption.requiresValue()) {
createTableSpecification.with(tableOption, value);
} else {
createTableSpecification.with(tableOption);
Expand All @@ -127,6 +130,14 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
getCqlOperations().execute(CqlGenerator.toCql(createTableSpecification));
}

private void addRawTableOption(String key, Object value, CreateTableSpecification createTableSpecification) {
if (value instanceof String) {
createTableSpecification.with(key, value, true, true);
return;
}
createTableSpecification.with(key, value, false, false);
}
Copy link
Contributor Author

@seungh0 seungh0 Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current situation, if users manually apply escaping and quoting, it could result in double escaping/quoting if the option is later added to TableOption in a future version.

So I considered two options:

  1. Apply escaping and quoting only when the option is not part of TableOption and the value is a string — as implemented in the PR.
  2. Enhance CqlStringUtils to detect whether the value is already escaped or quoted, and skip escaping if so.

I chose option 1, considering the potential side effects.
What do you think is the better approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing values as strings in a map is convenient and doing so might cause a moment of surprise when an option (e.g. bloom_filter_fp_chance) was not defined in TableOptions and the value of 0.3 gets quoted to 0.3. However, Cassandra seems not to care whether a value is quoted. Let's keep only the quoting and expect escaping by the caller.

Having the framework doing something that you cannot disable causes more inconvenience than doing less in the framework and allowing the caller to update their inputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback.


@Override
public void dropTable(Class<?> entityClass) {
dropTable(getTableName(entityClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Matthew T. Adams
* @author Mark Paluch
* @author Mikhail Polivakha
* @author Seungho Kang
* @see CompactionOption
* @see CompressionOption
* @see CachingOption
Expand Down Expand Up @@ -77,7 +78,39 @@ public enum TableOption implements Option {
/**
* {@code gc_grace_seconds}
*/
GC_GRACE_SECONDS("gc_grace_seconds", Long.class, true, false, false);
GC_GRACE_SECONDS("gc_grace_seconds", Long.class, true, false, false),
/**
* {@code default_time_to_live}
*/
DEFAULT_TIME_TO_LIVE("default_time_to_live", Long.class, true, false, false),
/**
* {@code cdc}
*/
CDC("cdc", Boolean.class, true, false, false),
/**
* {@code speculative_retry}
*/
SPECULATIVE_RETRY("speculative_retry", String.class, true, true, true),
/**
* {@code memtable_flush_period_in_ms}
*/
MEMTABLE_FLUSH_PERIOD_IN_MS("memtable_flush_period_in_ms", Long.class, true, false, false),
/**
* {@code crc_check_chance}
*/
CRC_CHECK_CHANCE("crc_check_chance", Double.class, true, false, false),
/**
* {@code min_index_interval}
*/
MIN_INDEX_INTERVAL("min_index_interval", Long.class, true, false, false),
/**
* {@code max_index_interval}
*/
MAX_INDEX_INTERVAL("max_index_interval", Long.class, true, false, false),
/**
* {@code read_repair}
*/
READ_REPAIR("read_repair", String.class, true, true, true);

private Option delegate;

Expand All @@ -102,6 +135,23 @@ public static TableOption valueOfIgnoreCase(String optionName) {
throw new IllegalArgumentException(String.format("Unable to recognize specified Table option '%s'", optionName));
}

/**
* Look up {@link TableOption} by name using case-insensitive lookups.
*
* @param optionName name of the option.
* @return the matching {@link TableOption}, or {@code null} if no match is found
* @since 4.5.2
*/
@Nullable
public static TableOption findByNameIgnoreCase(String optionName) {
for (TableOption value : values()) {
if (value.getName().equalsIgnoreCase(optionName)) {
return value;
}
}
return null;
}

@Override
public Class<?> getType() {
return this.delegate.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
*
* @author Mark Paluch
* @author Mikhail Polivakha
* @author Seungho Kang
*/
class CassandraAdminTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTests {

Expand All @@ -66,22 +67,84 @@ private KeyspaceMetadata getKeyspaceMetadata() {
return getSession().getKeyspace().flatMap(metadata::getKeyspace).get();
}

@Test // GH-359
@Test // GH-359, GH-1584
void shouldApplyTableOptions() {

Map<String, Object> options = Map.of(TableOption.COMMENT.getName(), "This is comment for table", //
TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3");
TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3", //
TableOption.DEFAULT_TIME_TO_LIVE.getName(), "864000", //
TableOption.CDC.getName(), true, //
TableOption.SPECULATIVE_RETRY.getName(), "90percentile", //
TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS.getName(), "1000", //
TableOption.CRC_CHECK_CHANCE.getName(), "0.9", //
TableOption.MIN_INDEX_INTERVAL.getName(), "128", //
TableOption.MAX_INDEX_INTERVAL.getName(), "2048", //
TableOption.READ_REPAIR.getName(), "BLOCKING");

CqlIdentifier tableName = CqlIdentifier.fromCql("someTable");
cassandraAdminTemplate.createTable(true, tableName, SomeTable.class, options);

TableMetadata someTable = getKeyspaceMetadata().getTables().get(tableName);

assertThat(someTable).isNotNull();
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName())))
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))) //
.isEqualTo("This is comment for table");
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))) //
.isEqualTo(0.3);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.DEFAULT_TIME_TO_LIVE.getName()))) //
.isEqualTo(864_000);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.SPECULATIVE_RETRY.getName()))) //
.isEqualTo("90p");
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS.getName()))) //
.isEqualTo(1000);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.CRC_CHECK_CHANCE.getName()))) //
.isEqualTo(0.9);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MIN_INDEX_INTERVAL.getName()))) //
.isEqualTo(128);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MAX_INDEX_INTERVAL.getName()))) //
.isEqualTo(2048);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.READ_REPAIR.getName()))) //
.isEqualTo("BLOCKING");
}

@Test // GH-359, GH-1584
void shouldApplyTableOptions_with_raw() {

Map<String, Object> options = Map.of(TableOption.COMMENT.getName(), "This is comment for table", //
"bloom_filter_fp_chance", "0.3", //
"default_time_to_live", "864000", //
"cdc", true, //
"speculative_retry", "90percentile", //
"memtable_flush_period_in_ms", "1000", //
"crc_check_chance", "0.9", //
"min_index_interval", "128", //
"max_index_interval", "2048", //
"read_repair", "BLOCKING");

CqlIdentifier tableName = CqlIdentifier.fromCql("someTable");
cassandraAdminTemplate.createTable(true, tableName, SomeTable.class, options);

TableMetadata someTable = getKeyspaceMetadata().getTables().get(tableName);

assertThat(someTable).isNotNull();
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))) //
.isEqualTo("This is comment for table");
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName())))
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))) //
.isEqualTo(0.3);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.DEFAULT_TIME_TO_LIVE.getName()))) //
.isEqualTo(864_000);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.SPECULATIVE_RETRY.getName()))) //
.isEqualTo("90p");
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS.getName()))) //
.isEqualTo(1000);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.CRC_CHECK_CHANCE.getName()))) //
.isEqualTo(0.9);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MIN_INDEX_INTERVAL.getName()))) //
.isEqualTo(128);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.MAX_INDEX_INTERVAL.getName()))) //
.isEqualTo(2048);
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.READ_REPAIR.getName()))) //
.isEqualTo("BLOCKING");
}

@Test // GH-1388
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTests;
import org.springframework.data.util.Version;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata;
Expand All @@ -41,6 +42,7 @@
* Integration tests tests for {@link AlterTableCqlGenerator}.
*
* @author Mark Paluch
* @author Seungho Kang
*/
class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTests {

Expand Down Expand Up @@ -165,6 +167,39 @@ void alterTableAddCaching() {
assertThat(getTableMetadata("users").getOptions().toString()).contains("caching").contains("keys").contains("NONE");
}

@Test // GH-1584
void alterTableWithAllOptionsTest() {

session.execute("CREATE TABLE users (user_name varchar PRIMARY KEY);");
AlterTableSpecification spec = SpecificationBuilder.alterTable("users") //
.with(TableOption.GC_GRACE_SECONDS, 86400L).with(TableOption.DEFAULT_TIME_TO_LIVE, 36000L)
.with(TableOption.CDC, true).with(TableOption.SPECULATIVE_RETRY, "95PERCENTILE")
.with(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS, 20000L).with(TableOption.CRC_CHECK_CHANCE, 0.85d)
.with(TableOption.MIN_INDEX_INTERVAL, 256L).with(TableOption.MAX_INDEX_INTERVAL, 1048L)
.with(TableOption.READ_REPAIR, "NONE");

execute(spec);

TableMetadata meta = getTableMetadata("users");

assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.GC_GRACE_SECONDS.getName()), 86400);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.DEFAULT_TIME_TO_LIVE.getName()), 36000);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.SPECULATIVE_RETRY.getName()), "95p");
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS.getName()), 20000);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.CRC_CHECK_CHANCE.getName()), 0.85);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MIN_INDEX_INTERVAL.getName()), 256);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MAX_INDEX_INTERVAL.getName()), 1048);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.READ_REPAIR.getName()), "NONE");
}

private void execute(AlterTableSpecification spec) {
session.execute(CqlGenerator.toCql(spec));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Matthew T. Adams
* @author David Webb
* @author Mark Paluch
* @author Seungho Kang
*/
class AlterTableCqlGeneratorUnitTests {

Expand Down Expand Up @@ -123,6 +124,23 @@ void alterTableAddCaching() {
.isEqualTo("ALTER TABLE users WITH caching = { 'keys' : 'none', 'rows_per_partition' : '15' };");
}

@Test // GH-1584
void alterTableSetDefaultTimeToLive() {

AlterTableSpecification spec = SpecificationBuilder.alterTable("users")
.with(TableOption.DEFAULT_TIME_TO_LIVE, 3600)
.with(TableOption.CDC, true)
.with(TableOption.SPECULATIVE_RETRY, "90percentile")
.with(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS, 1000L)
.with(TableOption.CRC_CHECK_CHANCE, 0.9)
.with(TableOption.MIN_INDEX_INTERVAL, 128L)
.with(TableOption.MAX_INDEX_INTERVAL, 2048L)
.with(TableOption.READ_REPAIR, "BLOCKING");

assertThat(toCql(spec))
.isEqualTo("ALTER TABLE users WITH default_time_to_live = 3600 AND cdc = true AND speculative_retry = '90percentile' AND memtable_flush_period_in_ms = 1000 AND crc_check_chance = 0.9 AND min_index_interval = 128 AND max_index_interval = 2048 AND read_repair = 'BLOCKING';");
}

private String toCql(AlterTableSpecification spec) {
return CqlGenerator.toCql(spec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Matthew T. Adams
* @author Oliver Gierke
* @author Mark Paluch
* @author Seungho Kang
*/
class CreateTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTests {

Expand Down Expand Up @@ -108,4 +109,39 @@ void shouldGenerateTableInOtherKeyspace() {
assertThat(person.getPartitionKey()).hasSize(1);
assertThat(person.getClusteringColumns()).hasSize(1);
}

@Test // GH-1584
void shouldGenerateTableWithOptions() {

CreateTableSpecification spec = CreateTableSpecification.createTable("person")
.partitionKeyColumn("id", DataTypes.INT) //
.clusteredKeyColumn("date_of_birth", DataTypes.DATE, Ordering.ASCENDING) //
.column("name", DataTypes.ASCII) //
.with(TableOption.GC_GRACE_SECONDS, 86400L).with(TableOption.DEFAULT_TIME_TO_LIVE, 3600L)
.with(TableOption.CDC, true).with(TableOption.SPECULATIVE_RETRY, "99PERCENTILE")
.with(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS, 10000L).with(TableOption.CRC_CHECK_CHANCE, 0.9d)
.with(TableOption.MIN_INDEX_INTERVAL, 128L).with(TableOption.MAX_INDEX_INTERVAL, 2048L)
.with(TableOption.READ_REPAIR, "BLOCKING");

session.execute(CqlGenerator.toCql(spec));

TableMetadata meta = session.getMetadata().getKeyspace(getKeyspace()).flatMap(it -> it.getTable("person")).get();
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.GC_GRACE_SECONDS.getName()), 86400);

assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.DEFAULT_TIME_TO_LIVE.getName()), 3600);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.SPECULATIVE_RETRY.getName()), "99p");
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MEMTABLE_FLUSH_PERIOD_IN_MS.getName()), 10000);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.CRC_CHECK_CHANCE.getName()), 0.9);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MIN_INDEX_INTERVAL.getName()), 128);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.MAX_INDEX_INTERVAL.getName()), 2048);
assertThat(meta.getOptions()) //
.containsEntry(CqlIdentifier.fromCql(TableOption.READ_REPAIR.getName()), "BLOCKING");
}
}
Loading