-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
CreateTableStatement.java
565 lines (464 loc) · 21.5 KB
/
CreateTableStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.cql3.statements.schema;
import java.util.*;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.cassandra.audit.AuditLogContext;
import org.apache.cassandra.audit.AuditLogEntryType;
import org.apache.cassandra.auth.DataResource;
import org.apache.cassandra.auth.IResource;
import org.apache.cassandra.auth.Permission;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.cql3.*;
import org.apache.cassandra.db.Keyspace;
import org.apache.cassandra.db.guardrails.Guardrails;
import org.apache.cassandra.db.marshal.*;
import org.apache.cassandra.exceptions.AlreadyExistsException;
import org.apache.cassandra.schema.*;
import org.apache.cassandra.schema.Keyspaces.KeyspacesDiff;
import org.apache.cassandra.service.ClientState;
import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
import org.apache.cassandra.transport.Event.SchemaChange;
import org.apache.cassandra.transport.Event.SchemaChange.Change;
import org.apache.cassandra.transport.Event.SchemaChange.Target;
import static java.util.Comparator.comparing;
import static com.google.common.collect.Iterables.concat;
public final class CreateTableStatement extends AlterSchemaStatement
{
private static final Logger logger = LoggerFactory.getLogger(CreateTableStatement.class);
private final String tableName;
private final Map<ColumnIdentifier, CQL3Type.Raw> rawColumns;
private final Set<ColumnIdentifier> staticColumns;
private final List<ColumnIdentifier> partitionKeyColumns;
private final List<ColumnIdentifier> clusteringColumns;
private final LinkedHashMap<ColumnIdentifier, Boolean> clusteringOrder;
private final TableAttributes attrs;
private final boolean ifNotExists;
private final boolean useCompactStorage;
public CreateTableStatement(String keyspaceName,
String tableName,
Map<ColumnIdentifier, CQL3Type.Raw> rawColumns,
Set<ColumnIdentifier> staticColumns,
List<ColumnIdentifier> partitionKeyColumns,
List<ColumnIdentifier> clusteringColumns,
LinkedHashMap<ColumnIdentifier, Boolean> clusteringOrder,
TableAttributes attrs,
boolean ifNotExists,
boolean useCompactStorage)
{
super(keyspaceName);
this.tableName = tableName;
this.rawColumns = rawColumns;
this.staticColumns = staticColumns;
this.partitionKeyColumns = partitionKeyColumns;
this.clusteringColumns = clusteringColumns;
this.clusteringOrder = clusteringOrder;
this.attrs = attrs;
this.ifNotExists = ifNotExists;
this.useCompactStorage = useCompactStorage;
}
public Keyspaces apply(Keyspaces schema)
{
KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
if (null == keyspace)
throw ire("Keyspace '%s' doesn't exist", keyspaceName);
if (keyspace.hasTable(tableName))
{
if (ifNotExists)
return schema;
throw new AlreadyExistsException(keyspaceName, tableName);
}
TableMetadata table = builder(keyspace.types).build();
table.validate();
if (keyspace.createReplicationStrategy().hasTransientReplicas()
&& table.params.readRepair != ReadRepairStrategy.NONE)
{
throw ire("read_repair must be set to 'NONE' for transiently replicated keyspaces");
}
if (!table.params.compression.isEnabled())
Guardrails.uncompressedTablesEnabled.ensureEnabled(state);
return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.tables.with(table)));
}
@Override
public void validate(ClientState state)
{
super.validate(state);
// Guardrail on table properties
Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);
// Guardrail on columns per table
Guardrails.columnsPerTable.guard(rawColumns.size(), tableName, false, state);
// Guardrail on number of tables
if (Guardrails.tables.enabled(state))
{
int totalUserTables = Schema.instance.getUserKeyspaces()
.stream()
.map(ksm -> ksm.name)
.map(Keyspace::open)
.mapToInt(keyspace -> keyspace.getColumnFamilyStores().size())
.sum();
Guardrails.tables.guard(totalUserTables + 1, tableName, false, state);
}
// Guardrail to check whether creation of new COMPACT STORAGE tables is allowed
if (useCompactStorage)
Guardrails.compactTablesEnabled.ensureEnabled(state);
validateDefaultTimeToLive(attrs.asNewTableParams());
}
SchemaChange schemaChangeEvent(KeyspacesDiff diff)
{
return new SchemaChange(Change.CREATED, Target.TABLE, keyspaceName, tableName);
}
public void authorize(ClientState client)
{
client.ensureAllTablesPermission(keyspaceName, Permission.CREATE);
}
@Override
Set<IResource> createdResources(KeyspacesDiff diff)
{
return ImmutableSet.of(DataResource.table(keyspaceName, tableName));
}
@Override
public AuditLogContext getAuditLogContext()
{
return new AuditLogContext(AuditLogEntryType.CREATE_TABLE, keyspaceName, tableName);
}
public String toString()
{
return String.format("%s (%s, %s)", getClass().getSimpleName(), keyspaceName, tableName);
}
public TableMetadata.Builder builder(Types types)
{
attrs.validate();
TableParams params = attrs.asNewTableParams();
// use a TreeMap to preserve ordering across JDK versions (see CASSANDRA-9492) - important for stable unit tests
Map<ColumnIdentifier, CQL3Type> columns = new TreeMap<>(comparing(o -> o.bytes));
rawColumns.forEach((column, type) -> columns.put(column, type.prepare(keyspaceName, types)));
// check for nested non-frozen UDTs or collections in a non-frozen UDT
columns.forEach((column, type) ->
{
if (type.isUDT() && type.getType().isMultiCell())
{
((UserType) type.getType()).fieldTypes().forEach(field ->
{
if (field.isMultiCell())
throw ire("Non-frozen UDTs with nested non-frozen collections are not supported");
});
}
});
/*
* Deal with PRIMARY KEY columns
*/
HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();
concat(partitionKeyColumns, clusteringColumns).forEach(column ->
{
CQL3Type type = columns.get(column);
if (null == type)
throw ire("Unknown column '%s' referenced in PRIMARY KEY for table '%s'", column, tableName);
if (!primaryKeyColumns.add(column))
throw ire("Duplicate column '%s' in PRIMARY KEY clause for table '%s'", column, tableName);
if (type.getType().isMultiCell())
{
if (type.isCollection())
throw ire("Invalid non-frozen collection type %s for PRIMARY KEY column '%s'", type, column);
else
throw ire("Invalid non-frozen user-defined type %s for PRIMARY KEY column '%s'", type, column);
}
if (type.getType().isCounter())
throw ire("counter type is not supported for PRIMARY KEY column '%s'", column);
if (type.getType().referencesDuration())
throw ire("duration type is not supported for PRIMARY KEY column '%s'", column);
if (staticColumns.contains(column))
throw ire("Static column '%s' cannot be part of the PRIMARY KEY", column);
});
List<AbstractType<?>> partitionKeyTypes = new ArrayList<>();
List<AbstractType<?>> clusteringTypes = new ArrayList<>();
partitionKeyColumns.forEach(column ->
{
CQL3Type type = columns.remove(column);
partitionKeyTypes.add(type.getType());
});
clusteringColumns.forEach(column ->
{
CQL3Type type = columns.remove(column);
boolean reverse = !clusteringOrder.getOrDefault(column, true);
clusteringTypes.add(reverse ? ReversedType.getInstance(type.getType()) : type.getType());
});
if (clusteringOrder.size() > clusteringColumns.size())
throw ire("Only clustering columns can be defined in CLUSTERING ORDER directive");
int n = 0;
for (ColumnIdentifier id : clusteringOrder.keySet())
{
ColumnIdentifier c = clusteringColumns.get(n);
if (!id.equals(c))
{
if (clusteringOrder.containsKey(c))
throw ire("The order of columns in the CLUSTERING ORDER directive must match that of the clustering columns (%s must appear before %s)", c, id);
else
throw ire("Missing CLUSTERING ORDER for column %s", c);
}
++n;
}
// For COMPACT STORAGE, we reject any "feature" that we wouldn't be able to translate back to thrift.
if (useCompactStorage)
{
validateCompactTable(clusteringTypes, columns);
}
else
{
// Static columns only make sense if we have at least one clustering column. Otherwise everything is static anyway
if (clusteringColumns.isEmpty() && !staticColumns.isEmpty())
throw ire("Static columns are only useful (and thus allowed) if the table has at least one clustering column");
}
/*
* Counter table validation
*/
boolean hasCounters = rawColumns.values().stream().anyMatch(CQL3Type.Raw::isCounter);
if (hasCounters)
{
// We've handled anything that is not a PRIMARY KEY so columns only contains NON-PK columns. So
// if it's a counter table, make sure we don't have non-counter types
if (columns.values().stream().anyMatch(t -> !t.getType().isCounter()))
throw ire("Cannot mix counter and non counter columns in the same table");
if (params.defaultTimeToLive > 0)
throw ire("Cannot set %s on a table with counters", TableParams.Option.DEFAULT_TIME_TO_LIVE);
}
/*
* Create the builder
*/
TableMetadata.Builder builder = TableMetadata.builder(keyspaceName, tableName);
if (attrs.hasProperty(TableAttributes.ID))
builder.id(attrs.getId());
builder.isCounter(hasCounters)
.params(params);
for (int i = 0; i < partitionKeyColumns.size(); i++)
builder.addPartitionKeyColumn(partitionKeyColumns.get(i), partitionKeyTypes.get(i));
for (int i = 0; i < clusteringColumns.size(); i++)
builder.addClusteringColumn(clusteringColumns.get(i), clusteringTypes.get(i));
if (useCompactStorage)
{
fixupCompactTable(clusteringTypes, columns, hasCounters, builder);
}
else
{
columns.forEach((column, type) -> {
if (staticColumns.contains(column))
builder.addStaticColumn(column, type.getType());
else
builder.addRegularColumn(column, type.getType());
});
}
return builder;
}
private void validateCompactTable(List<AbstractType<?>> clusteringTypes,
Map<ColumnIdentifier, CQL3Type> columns)
{
boolean isDense = !clusteringTypes.isEmpty();
if (columns.values().stream().anyMatch(c -> c.getType().isMultiCell()))
throw ire("Non-frozen collections and UDTs are not supported with COMPACT STORAGE");
if (!staticColumns.isEmpty())
throw ire("Static columns are not supported in COMPACT STORAGE tables");
if (clusteringTypes.isEmpty())
{
// It's a thrift "static CF" so there should be some columns definition
if (columns.isEmpty())
throw ire("No definition found that is not part of the PRIMARY KEY");
}
if (isDense)
{
// We can have no columns (only the PK), but we can't have more than one.
if (columns.size() > 1)
throw ire(String.format("COMPACT STORAGE with composite PRIMARY KEY allows no more than one column not part of the PRIMARY KEY (got: %s)", StringUtils.join(columns.keySet(), ", ")));
}
else
{
// we are in the "static" case, so we need at least one column defined. For non-compact however, having
// just the PK is fine.
if (columns.isEmpty())
throw ire("COMPACT STORAGE with non-composite PRIMARY KEY require one column not part of the PRIMARY KEY, none given");
}
}
private void fixupCompactTable(List<AbstractType<?>> clusteringTypes,
Map<ColumnIdentifier, CQL3Type> columns,
boolean hasCounters,
TableMetadata.Builder builder)
{
Set<TableMetadata.Flag> flags = EnumSet.noneOf(TableMetadata.Flag.class);
boolean isDense = !clusteringTypes.isEmpty();
boolean isCompound = clusteringTypes.size() > 1;
if (isDense)
flags.add(TableMetadata.Flag.DENSE);
if (isCompound)
flags.add(TableMetadata.Flag.COMPOUND);
if (hasCounters)
flags.add(TableMetadata.Flag.COUNTER);
boolean isStaticCompact = !isDense && !isCompound;
builder.flags(flags);
columns.forEach((name, type) -> {
// Note that for "static" no-clustering compact storage we use static for the defined columns
if (staticColumns.contains(name) || isStaticCompact)
builder.addStaticColumn(name, type.getType());
else
builder.addRegularColumn(name, type.getType());
});
DefaultNames names = new DefaultNames(builder.columnNames());
// Compact tables always have a clustering and a single regular value.
if (isStaticCompact)
{
builder.addClusteringColumn(names.defaultClusteringName(), UTF8Type.instance);
builder.addRegularColumn(names.defaultCompactValueName(), hasCounters ? CounterColumnType.instance : BytesType.instance);
}
else if (!builder.hasRegularColumns())
{
// Even for dense, we might not have our regular column if it wasn't part of the declaration. If
// that's the case, add it but with a specific EmptyType so we can recognize that case later
builder.addRegularColumn(names.defaultCompactValueName(), EmptyType.instance);
}
}
@Override
public Set<String> clientWarnings(KeyspacesDiff diff)
{
// this threshold is deprecated, it will be replaced by the guardrail used in #validate(ClientState)
int tableCount = Schema.instance.getNumberOfTables();
if (tableCount > DatabaseDescriptor.tableCountWarnThreshold())
{
String msg = String.format("Cluster already contains %d tables in %d keyspaces. Having a large number of tables will significantly slow down schema dependent cluster operations.",
tableCount,
Schema.instance.getKeyspaces().size());
logger.warn(msg);
return ImmutableSet.of(msg);
}
return ImmutableSet.of();
}
private static class DefaultNames
{
private static final String DEFAULT_CLUSTERING_NAME = "column";
private static final String DEFAULT_COMPACT_VALUE_NAME = "value";
private final Set<String> usedNames;
private int clusteringIndex = 1;
private int compactIndex = 0;
private DefaultNames(Set<String> usedNames)
{
this.usedNames = usedNames;
}
public String defaultClusteringName()
{
while (true)
{
String candidate = DEFAULT_CLUSTERING_NAME + clusteringIndex;
++clusteringIndex;
if (usedNames.add(candidate))
return candidate;
}
}
public String defaultCompactValueName()
{
while (true)
{
String candidate = compactIndex == 0 ? DEFAULT_COMPACT_VALUE_NAME : DEFAULT_COMPACT_VALUE_NAME + compactIndex;
++compactIndex;
if (usedNames.add(candidate))
return candidate;
}
}
}
public static TableMetadata.Builder parse(String cql, String keyspace)
{
return CQLFragmentParser.parseAny(CqlParser::createTableStatement, cql, "CREATE TABLE")
.keyspace(keyspace)
.prepare(null) // works around a messy ClientState/QueryProcessor class init deadlock
.builder(Types.none());
}
public final static class Raw extends CQLStatement.Raw
{
private final QualifiedName name;
private final boolean ifNotExists;
private boolean useCompactStorage = false;
private final Map<ColumnIdentifier, CQL3Type.Raw> rawColumns = new HashMap<>();
private final Set<ColumnIdentifier> staticColumns = new HashSet<>();
private final List<ColumnIdentifier> clusteringColumns = new ArrayList<>();
private List<ColumnIdentifier> partitionKeyColumns;
private final LinkedHashMap<ColumnIdentifier, Boolean> clusteringOrder = new LinkedHashMap<>();
public final TableAttributes attrs = new TableAttributes();
public Raw(QualifiedName name, boolean ifNotExists)
{
this.name = name;
this.ifNotExists = ifNotExists;
}
public CreateTableStatement prepare(ClientState state)
{
String keyspaceName = name.hasKeyspace() ? name.getKeyspace() : state.getKeyspace();
if (null == partitionKeyColumns)
throw ire("No PRIMARY KEY specifed for table '%s' (exactly one required)", name);
return new CreateTableStatement(keyspaceName,
name.getName(),
rawColumns,
staticColumns,
partitionKeyColumns,
clusteringColumns,
clusteringOrder,
attrs,
ifNotExists,
useCompactStorage);
}
public String keyspace()
{
return name.getKeyspace();
}
public Raw keyspace(String keyspace)
{
name.setKeyspace(keyspace, true);
return this;
}
public String table()
{
return name.getName();
}
public void addColumn(ColumnIdentifier column, CQL3Type.Raw type, boolean isStatic)
{
if (null != rawColumns.put(column, type))
throw ire("Duplicate column '%s' declaration for table '%s'", column, name);
if (isStatic)
staticColumns.add(column);
}
public void setCompactStorage()
{
useCompactStorage = true;
}
public void setPartitionKeyColumn(ColumnIdentifier column)
{
setPartitionKeyColumns(Collections.singletonList(column));
}
public void setPartitionKeyColumns(List<ColumnIdentifier> columns)
{
if (null != partitionKeyColumns)
throw ire("Multiple PRIMARY KEY specified for table '%s' (exactly one required)", name);
partitionKeyColumns = columns;
}
public void markClusteringColumn(ColumnIdentifier column)
{
clusteringColumns.add(column);
}
public void extendClusteringOrder(ColumnIdentifier column, boolean ascending)
{
if (null != clusteringOrder.put(column, ascending))
throw ire("Duplicate column '%s' in CLUSTERING ORDER BY clause for table '%s'", column, name);
}
}
}