use of org.apache.cassandra.db.marshal.MapType in project cassandra by apache.
the class SSTableHeaderFixTest method freezeUdt.
private AbstractType<?> freezeUdt(AbstractType<?> type) {
if (type instanceof CollectionType) {
if (type.getClass() == ListType.class) {
ListType<?> cHeader = (ListType<?>) type;
return ListType.getInstance(freezeUdt(cHeader.getElementsType()), cHeader.isMultiCell());
} else if (type.getClass() == SetType.class) {
SetType<?> cHeader = (SetType<?>) type;
return SetType.getInstance(freezeUdt(cHeader.getElementsType()), cHeader.isMultiCell());
} else if (type.getClass() == MapType.class) {
MapType<?, ?> cHeader = (MapType<?, ?>) type;
return MapType.getInstance(freezeUdt(cHeader.getKeysType()), freezeUdt(cHeader.getValuesType()), cHeader.isMultiCell());
}
} else if (type instanceof AbstractCompositeType) {
if (type.getClass() == CompositeType.class) {
CompositeType cHeader = (CompositeType) type;
return CompositeType.getInstance(cHeader.types.stream().map(this::freezeUdt).collect(Collectors.toList()));
}
} else if (type instanceof TupleType) {
if (type.getClass() == UserType.class) {
UserType cHeader = (UserType) type;
cHeader = cHeader.freeze();
return new UserType(cHeader.keyspace, cHeader.name, cHeader.fieldNames(), cHeader.allTypes().stream().map(this::freezeUdt).collect(Collectors.toList()), cHeader.isMultiCell());
}
}
return type;
}
use of org.apache.cassandra.db.marshal.MapType in project cassandra by apache.
the class SSTableHeaderFixTest method assertFrozenUdt.
private void assertFrozenUdt(String name, AbstractType<?> type, boolean frozen, boolean checkInner) {
if (type instanceof CompositeType) {
if (checkInner)
for (AbstractType<?> component : ((CompositeType) type).types) assertFrozenUdt(name, component, frozen, true);
} else if (type instanceof CollectionType) {
if (checkInner) {
if (type instanceof MapType) {
MapType map = (MapType) type;
// only descend for non-frozen types (checking frozen in frozen is just stupid)
if (map.isMultiCell()) {
assertFrozenUdt(name + "<map-key>", map.getKeysType(), frozen, true);
assertFrozenUdt(name + "<map-value>", map.getValuesType(), frozen, true);
}
} else if (type instanceof SetType) {
SetType set = (SetType) type;
// only descend for non-frozen types (checking frozen in frozen is just stupid)
if (set.isMultiCell())
assertFrozenUdt(name + "<set>", set.getElementsType(), frozen, true);
} else if (type instanceof ListType) {
ListType list = (ListType) type;
// only descend for non-frozen types (checking frozen in frozen is just stupid)
if (list.isMultiCell())
assertFrozenUdt(name + "<list>", list.getElementsType(), frozen, true);
}
}
} else if (type instanceof TupleType) {
if (checkInner) {
TupleType tuple = (TupleType) type;
// only descend for non-frozen types (checking frozen in frozen is just stupid)
if (tuple.isMultiCell())
for (AbstractType<?> component : tuple.allTypes()) assertFrozenUdt(name + "<tuple>", component, frozen, true);
}
}
if (type instanceof UserType) {
String typeString = type.toString();
assertEquals(name + ": " + typeString, frozen, !type.isMultiCell());
if (typeString.startsWith(UserType.class.getName() + '('))
if (frozen)
fail(name + ": " + typeString);
if (typeString.startsWith(FrozenType.class.getName() + '(' + UserType.class.getName() + '('))
if (!frozen)
fail(name + ": " + typeString);
}
}
use of org.apache.cassandra.db.marshal.MapType in project cassandra by apache.
the class CreateIndexStatement method validate.
public void validate(ClientState state) throws RequestValidationException {
TableMetadata table = Schema.instance.validateTable(keyspace(), columnFamily());
if (table.isCounter())
throw new InvalidRequestException("Secondary indexes are not supported on counter tables");
if (table.isView())
throw new InvalidRequestException("Secondary indexes are not supported on materialized views");
if (table.isCompactTable() && !table.isStaticCompactTable())
throw new InvalidRequestException("Secondary indexes are not supported on COMPACT STORAGE tables that have clustering columns");
List<IndexTarget> targets = new ArrayList<>(rawTargets.size());
for (IndexTarget.Raw rawTarget : rawTargets) targets.add(rawTarget.prepare(table));
if (targets.isEmpty() && !properties.isCustom)
throw new InvalidRequestException("Only CUSTOM indexes can be created without specifying a target column");
if (targets.size() > 1)
validateTargetsForMultiColumnIndex(targets);
for (IndexTarget target : targets) {
ColumnMetadata cd = table.getColumn(target.column);
if (cd == null)
throw new InvalidRequestException("No column definition found for column " + target.column);
if (cd.type.referencesDuration()) {
checkFalse(cd.type.isCollection(), "Secondary indexes are not supported on collections containing durations");
checkFalse(cd.type.isTuple(), "Secondary indexes are not supported on tuples containing durations");
checkFalse(cd.type.isUDT(), "Secondary indexes are not supported on UDTs containing durations");
throw invalidRequest("Secondary indexes are not supported on duration columns");
}
// TODO: we could lift that limitation
if (table.isCompactTable() && cd.isPrimaryKeyColumn())
throw new InvalidRequestException("Secondary indexes are not supported on PRIMARY KEY columns in COMPACT STORAGE tables");
if (cd.kind == ColumnMetadata.Kind.PARTITION_KEY && table.partitionKeyColumns().size() == 1)
throw new InvalidRequestException(String.format("Cannot create secondary index on partition key column %s", target.column));
boolean isMap = cd.type instanceof MapType;
boolean isFrozenCollection = cd.type.isCollection() && !cd.type.isMultiCell();
if (isFrozenCollection) {
validateForFrozenCollection(target);
} else {
validateNotFullIndex(target);
validateIsSimpleIndexIfTargetColumnNotCollection(cd, target);
validateTargetColumnIsMapIfIndexInvolvesKeys(isMap, target);
}
}
if (!Strings.isNullOrEmpty(indexName)) {
if (Schema.instance.getKeyspaceMetadata(keyspace()).existingIndexNames(null).contains(indexName)) {
if (ifNotExists)
return;
else
throw new InvalidRequestException(String.format("Index %s already exists", indexName));
}
}
properties.validate();
}
Aggregations