Skip to content

Commit 8cc6812

Browse files
k-rusdjatnieks
authored andcommitted
CNDB-12683 validate table name length for not-internal (#1623)
Fixes riptano/cndb#12683 Table names are used in file names. Since the table names were not validated on its length, creating or flushing a table with too long name fails on too long file name. There are two cases with using table names in file names: - keyspace_name .table_name-controller-config.JSON - table_name-32chars_table_id The maximum allowed file name size is 255 chars. Thus, - keyspace and table names, which are together longer than 231 chars, will fail. - a table name, which is longer than 222 chars, will fail. This PR checks that creating new table name should not have too long table name. New tables are identified through the query's client state, which should not be internal. The limit is 222 chars for combined keyspace and table names. This is more restrictive than necessary, but is easier to explain in the documentation. The change is tested in CNDB that creating new tables with long names are prevented, but existing tables still work.
1 parent 0ba2404 commit 8cc6812

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.apache.cassandra.schema.Keyspaces;
6969
import org.apache.cassandra.schema.Keyspaces.KeyspacesDiff;
7070
import org.apache.cassandra.schema.Schema;
71+
import org.apache.cassandra.schema.SchemaConstants;
7172
import org.apache.cassandra.schema.TableMetadata;
7273
import org.apache.cassandra.schema.TableParams;
7374
import org.apache.cassandra.schema.Types;
@@ -162,6 +163,10 @@ public void validate(ClientState state)
162163
{
163164
super.validate(state);
164165

166+
if (!state.isInternal && tableName.length() > SchemaConstants.NAME_LENGTH - keyspaceName.length())
167+
throw ire("Keyspace and table names combined shouldn't be more than %s characters long (got keyspace of %s chars and table of %s chars for %s.%s)",
168+
SchemaConstants.NAME_LENGTH, keyspaceName.length(), tableName.length(), keyspaceName, tableName);
169+
165170
// Guardrail on table properties
166171
Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);
167172

test/unit/org/apache/cassandra/schema/CreateTableValidationTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.cassandra.schema;
2020

21+
import com.datastax.driver.core.exceptions.InvalidQueryException;
2122
import org.apache.cassandra.cql3.CQLTester;
2223
import org.apache.cassandra.cql3.UntypedResultSet;
2324
import org.apache.cassandra.cql3.functions.types.ParseUtils;
@@ -104,7 +105,20 @@ public void testCreateTableWithMissingClusteringColumn()
104105
}
105106

106107
@Test
107-
public void testCreatingTableWithLongName() throws Throwable
108+
public void failCreatingNewTableWithLongName()
109+
{
110+
String table = "test_create_k8yq1r75bpzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
111+
assertThatExceptionOfType(InvalidQueryException.class)
112+
.isThrownBy(() -> executeNet(String.format("CREATE TABLE \"%s\".%s (" +
113+
"key int PRIMARY KEY," +
114+
"val int)",
115+
KEYSPACE, table)))
116+
.withMessageContaining(String.format("Keyspace and table names combined shouldn't be more than %s characters long (got keyspace of %s chars and table of %s chars for %s.%s)",
117+
SchemaConstants.NAME_LENGTH, KEYSPACE.length(), table.length(), KEYSPACE, table));
118+
}
119+
120+
@Test
121+
public void testCreatingInternalTableWithLongName() throws Throwable
108122
{
109123
String keyspace = "\"38373639353166362d356631322d343864652d393063362d653862616534343165333764_tpch\"";
110124
String table = "test_create_k8yq1r75bpzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";

0 commit comments

Comments
 (0)