Search in sources :

Example 1 with Guardrails

use of org.apache.cassandra.db.guardrails.Guardrails in project cassandra by apache.

the class CreateIndexStatement method apply.

public Keyspaces apply(Keyspaces schema) {
    attrs.validate();
    if (attrs.isCustom && attrs.customClass.equals(SASIIndex.class.getName()) && !DatabaseDescriptor.getSASIIndexesEnabled())
        throw new InvalidRequestException("SASI indexes are disabled. Enable in cassandra.yaml to use.");
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    if (null == keyspace)
        throw ire("Keyspace '%s' doesn't exist", keyspaceName);
    TableMetadata table = keyspace.getTableOrViewNullable(tableName);
    if (null == table)
        throw ire("Table '%s' doesn't exist", tableName);
    if (null != indexName && keyspace.hasIndex(indexName)) {
        if (ifNotExists)
            return schema;
        throw ire("Index '%s' already exists", indexName);
    }
    if (table.isCounter())
        throw ire("Secondary indexes on counter tables aren't supported");
    if (table.isView())
        throw ire("Secondary indexes on materialized views aren't supported");
    if (Keyspace.open(table.keyspace).getReplicationStrategy().hasTransientReplicas())
        throw new InvalidRequestException("Secondary indexes are not supported on transiently replicated keyspaces");
    // guardrails to limit number of secondary indexes per table.
    Guardrails.secondaryIndexesPerTable.guard(table.indexes.size() + 1, Strings.isNullOrEmpty(indexName) ? String.format("on table %s", table.name) : String.format("%s on table %s", indexName, table.name), state);
    List<IndexTarget> indexTargets = Lists.newArrayList(transform(rawIndexTargets, t -> t.prepare(table)));
    if (indexTargets.isEmpty() && !attrs.isCustom)
        throw ire("Only CUSTOM indexes can be created without specifying a target column");
    if (indexTargets.size() > 1) {
        if (!attrs.isCustom)
            throw ire("Only CUSTOM indexes support multiple columns");
        Set<ColumnIdentifier> columns = new HashSet<>();
        for (IndexTarget target : indexTargets) if (!columns.add(target.column))
            throw ire("Duplicate column '%s' in index target list", target.column);
    }
    indexTargets.forEach(t -> validateIndexTarget(table, t));
    String name = null == indexName ? generateIndexName(keyspace, indexTargets) : indexName;
    IndexMetadata.Kind kind = attrs.isCustom ? IndexMetadata.Kind.CUSTOM : IndexMetadata.Kind.COMPOSITES;
    Map<String, String> options = attrs.isCustom ? attrs.getOptions() : Collections.emptyMap();
    IndexMetadata index = IndexMetadata.fromIndexTargets(indexTargets, name, kind, options);
    // check to disallow creation of an index which duplicates an existing one in all but name
    IndexMetadata equalIndex = tryFind(table.indexes, i -> i.equalsWithoutName(index)).orNull();
    if (null != equalIndex) {
        if (ifNotExists)
            return schema;
        throw ire("Index %s is a duplicate of existing index %s", index.name, equalIndex.name);
    }
    TableMetadata newTable = table.withSwapped(table.indexes.with(index));
    newTable.validate();
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.tables.withSwapped(newTable)));
}
Also used : AuditLogContext(org.apache.cassandra.audit.AuditLogContext) Change(org.apache.cassandra.transport.Event.SchemaChange.Change) java.util(java.util) Iterables.transform(com.google.common.collect.Iterables.transform) Iterables.tryFind(com.google.common.collect.Iterables.tryFind) Permission(org.apache.cassandra.auth.Permission) CQLStatement(org.apache.cassandra.cql3.CQLStatement) QualifiedName(org.apache.cassandra.cql3.QualifiedName) Strings(com.google.common.base.Strings) Guardrails(org.apache.cassandra.db.guardrails.Guardrails) Lists(com.google.common.collect.Lists) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) Keyspace(org.apache.cassandra.db.Keyspace) SASIIndex(org.apache.cassandra.index.sasi.SASIIndex) Type(org.apache.cassandra.cql3.statements.schema.IndexTarget.Type) ImmutableSet(com.google.common.collect.ImmutableSet) SchemaChange(org.apache.cassandra.transport.Event.SchemaChange) ClientState(org.apache.cassandra.service.ClientState) AuditLogEntryType(org.apache.cassandra.audit.AuditLogEntryType) MapType(org.apache.cassandra.db.marshal.MapType) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Target(org.apache.cassandra.transport.Event.SchemaChange.Target) org.apache.cassandra.schema(org.apache.cassandra.schema) SASIIndex(org.apache.cassandra.index.sasi.SASIIndex) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 2 with Guardrails

use of org.apache.cassandra.db.guardrails.Guardrails in project cassandra by apache.

the class CreateViewStatement method apply.

public Keyspaces apply(Keyspaces schema) {
    if (!DatabaseDescriptor.getMaterializedViewsEnabled())
        throw ire("Materialized views are disabled. Enable in cassandra.yaml to use.");
    /*
         * Basic dependency validations
         */
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    if (null == keyspace)
        throw ire("Keyspace '%s' doesn't exist", keyspaceName);
    if (keyspace.createReplicationStrategy().hasTransientReplicas())
        throw new InvalidRequestException("Materialized views are not supported on transiently replicated keyspaces");
    TableMetadata table = keyspace.tables.getNullable(tableName);
    if (null == table)
        throw ire("Base table '%s' doesn't exist", tableName);
    if (keyspace.hasTable(viewName))
        throw ire("Cannot create materialized view '%s' - a table with the same name already exists", viewName);
    if (keyspace.hasView(viewName)) {
        if (ifNotExists)
            return schema;
        throw new AlreadyExistsException(keyspaceName, viewName);
    }
    if (table.isCounter())
        throw ire("Materialized views are not supported on counter tables");
    if (table.isView())
        throw ire("Materialized views cannot be created against other materialized views");
    // Guardrails on table properties
    Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);
    // Guardrail to limit number of mvs per table
    Iterable<ViewMetadata> tableViews = keyspace.views.forTable(table.id);
    Guardrails.materializedViewsPerTable.guard(Iterables.size(tableViews) + 1, String.format("%s on table %s", viewName, table.name), state);
    if (table.params.gcGraceSeconds == 0) {
        throw ire("Cannot create materialized view '%s' for base table " + "'%s' with gc_grace_seconds of 0, since this value is " + "used to TTL undelivered updates. Setting gc_grace_seconds" + " too low might cause undelivered updates to expire " + "before being replayed.", viewName, tableName);
    }
    /*
         * Process SELECT clause
         */
    Set<ColumnIdentifier> selectedColumns = new HashSet<>();
    if (// SELECT *
    rawColumns.isEmpty())
        table.columns().forEach(c -> selectedColumns.add(c.name));
    rawColumns.forEach(selector -> {
        if (null != selector.alias)
            throw ire("Cannot use aliases when defining a materialized view (got %s)", selector);
        if (!(selector.selectable instanceof Selectable.RawIdentifier))
            throw ire("Can only select columns by name when defining a materialized view (got %s)", selector.selectable);
        // will throw IRE if the column doesn't exist in the base table
        ColumnMetadata column = (ColumnMetadata) selector.selectable.prepare(table);
        selectedColumns.add(column.name);
    });
    selectedColumns.stream().map(table::getColumn).filter(ColumnMetadata::isStatic).findAny().ifPresent(c -> {
        throw ire("Cannot include static column '%s' in materialized view '%s'", c, viewName);
    });
    if (partitionKeyColumns.isEmpty())
        throw ire("Must provide at least one partition key column for materialized view '%s'", viewName);
    HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();
    concat(partitionKeyColumns, clusteringColumns).forEach(name -> {
        ColumnMetadata column = table.getColumn(name);
        if (null == column || !selectedColumns.contains(name))
            throw ire("Unknown column '%s' referenced in PRIMARY KEY for materialized view '%s'", name, viewName);
        if (!primaryKeyColumns.add(name))
            throw ire("Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'", name, viewName);
        AbstractType<?> type = column.type;
        if (type.isMultiCell()) {
            if (type.isCollection())
                throw ire("Invalid non-frozen collection type '%s' for PRIMARY KEY column '%s'", type, name);
            else
                throw ire("Invalid non-frozen user-defined type '%s' for PRIMARY KEY column '%s'", type, name);
        }
        if (type.isCounter())
            throw ire("counter type is not supported for PRIMARY KEY column '%s'", name);
        if (type.referencesDuration())
            throw ire("duration type is not supported for PRIMARY KEY column '%s'", name);
    });
    // If we give a clustering order, we must explicitly do so for all aliases and in the order of the PK
    if (!clusteringOrder.isEmpty() && !clusteringColumns.equals(new ArrayList<>(clusteringOrder.keySet())))
        throw ire("Clustering key columns must exactly match columns in CLUSTERING ORDER BY directive");
    /*
         * We need to include all of the primary key columns from the base table in order to make sure that we do not
         * overwrite values in the view. We cannot support "collapsing" the base table into a smaller number of rows in
         * the view because if we need to generate a tombstone, we have no way of knowing which value is currently being
         * used in the view and whether or not to generate a tombstone. In order to not surprise our users, we require
         * that they include all of the columns. We provide them with a list of all of the columns left to include.
         */
    List<ColumnIdentifier> missingPrimaryKeyColumns = Lists.newArrayList(filter(transform(table.primaryKeyColumns(), c -> c.name), c -> !primaryKeyColumns.contains(c)));
    if (!missingPrimaryKeyColumns.isEmpty()) {
        throw ire("Cannot create materialized view '%s' without primary key columns %s from base table '%s'", viewName, join(", ", transform(missingPrimaryKeyColumns, ColumnIdentifier::toString)), tableName);
    }
    Set<ColumnIdentifier> regularBaseTableColumnsInViewPrimaryKey = new HashSet<>(primaryKeyColumns);
    transform(table.primaryKeyColumns(), c -> c.name).forEach(regularBaseTableColumnsInViewPrimaryKey::remove);
    if (regularBaseTableColumnsInViewPrimaryKey.size() > 1) {
        throw ire("Cannot include more than one non-primary key column in materialized view primary key (got %s)", join(", ", transform(regularBaseTableColumnsInViewPrimaryKey, ColumnIdentifier::toString)));
    }
    /*
         * Process WHERE clause
         */
    if (whereClause.containsTokenRelations())
        throw new InvalidRequestException("Cannot use token relation when defining a materialized view");
    if (whereClause.containsCustomExpressions())
        throw ire("WHERE clause for materialized view '%s' cannot contain custom index expressions", viewName);
    StatementRestrictions restrictions = new StatementRestrictions(StatementType.SELECT, table, whereClause, VariableSpecifications.empty(), false, false, true, true);
    List<ColumnIdentifier> nonRestrictedPrimaryKeyColumns = Lists.newArrayList(filter(primaryKeyColumns, name -> !restrictions.isRestricted(table.getColumn(name))));
    if (!nonRestrictedPrimaryKeyColumns.isEmpty()) {
        throw ire("Primary key columns %s must be restricted with 'IS NOT NULL' or otherwise", join(", ", transform(nonRestrictedPrimaryKeyColumns, ColumnIdentifier::toString)));
    }
    // See CASSANDRA-13798
    Set<ColumnMetadata> restrictedNonPrimaryKeyColumns = restrictions.nonPKRestrictedColumns(false);
    if (!restrictedNonPrimaryKeyColumns.isEmpty() && !Boolean.getBoolean("cassandra.mv.allow_filtering_nonkey_columns_unsafe")) {
        throw ire("Non-primary key columns can only be restricted with 'IS NOT NULL' (got: %s restricted illegally)", join(",", transform(restrictedNonPrimaryKeyColumns, ColumnMetadata::toString)));
    }
    /*
         * Validate WITH params
         */
    attrs.validate();
    if (attrs.hasOption(TableParams.Option.DEFAULT_TIME_TO_LIVE)) {
        throw ire("Cannot set default_time_to_live for a materialized view. " + "Data in a materialized view always expire at the same time than " + "the corresponding data in the parent table.");
    }
    /*
         * Build the thing
         */
    TableMetadata.Builder builder = TableMetadata.builder(keyspaceName, viewName);
    if (attrs.hasProperty(TableAttributes.ID))
        builder.id(attrs.getId());
    builder.params(attrs.asNewTableParams()).kind(TableMetadata.Kind.VIEW);
    partitionKeyColumns.forEach(name -> builder.addPartitionKeyColumn(name, getType(table, name)));
    clusteringColumns.forEach(name -> builder.addClusteringColumn(name, getType(table, name)));
    selectedColumns.stream().filter(name -> !primaryKeyColumns.contains(name)).forEach(name -> builder.addRegularColumn(name, getType(table, name)));
    ViewMetadata view = new ViewMetadata(table.id, table.name, rawColumns.isEmpty(), whereClause, builder.build());
    view.metadata.validate();
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.views.with(view)));
}
Also used : AuditLogContext(org.apache.cassandra.audit.AuditLogContext) Change(org.apache.cassandra.transport.Event.SchemaChange.Change) java.util(java.util) Iterables(com.google.common.collect.Iterables) Iterables.transform(com.google.common.collect.Iterables.transform) Permission(org.apache.cassandra.auth.Permission) AbstractType(org.apache.cassandra.db.marshal.AbstractType) org.apache.cassandra.cql3(org.apache.cassandra.cql3) AlreadyExistsException(org.apache.cassandra.exceptions.AlreadyExistsException) View(org.apache.cassandra.db.view.View) Guardrails(org.apache.cassandra.db.guardrails.Guardrails) Lists(com.google.common.collect.Lists) String.join(java.lang.String.join) Iterables.concat(com.google.common.collect.Iterables.concat) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ImmutableSet(com.google.common.collect.ImmutableSet) SchemaChange(org.apache.cassandra.transport.Event.SchemaChange) ClientState(org.apache.cassandra.service.ClientState) StatementRestrictions(org.apache.cassandra.cql3.restrictions.StatementRestrictions) AuditLogEntryType(org.apache.cassandra.audit.AuditLogEntryType) StatementType(org.apache.cassandra.cql3.statements.StatementType) Selectable(org.apache.cassandra.cql3.selection.Selectable) ReversedType(org.apache.cassandra.db.marshal.ReversedType) Target(org.apache.cassandra.transport.Event.SchemaChange.Target) RawSelector(org.apache.cassandra.cql3.selection.RawSelector) Iterables.filter(com.google.common.collect.Iterables.filter) org.apache.cassandra.schema(org.apache.cassandra.schema) AlreadyExistsException(org.apache.cassandra.exceptions.AlreadyExistsException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) StatementRestrictions(org.apache.cassandra.cql3.restrictions.StatementRestrictions)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables.transform (com.google.common.collect.Iterables.transform)2 Lists (com.google.common.collect.Lists)2 java.util (java.util)2 AuditLogContext (org.apache.cassandra.audit.AuditLogContext)2 AuditLogEntryType (org.apache.cassandra.audit.AuditLogEntryType)2 Permission (org.apache.cassandra.auth.Permission)2 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)2 Guardrails (org.apache.cassandra.db.guardrails.Guardrails)2 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)2 org.apache.cassandra.schema (org.apache.cassandra.schema)2 KeyspacesDiff (org.apache.cassandra.schema.Keyspaces.KeyspacesDiff)2 ClientState (org.apache.cassandra.service.ClientState)2 SchemaChange (org.apache.cassandra.transport.Event.SchemaChange)2 Change (org.apache.cassandra.transport.Event.SchemaChange.Change)2 Target (org.apache.cassandra.transport.Event.SchemaChange.Target)2 Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Iterables.concat (com.google.common.collect.Iterables.concat)1 Iterables.filter (com.google.common.collect.Iterables.filter)1