/*
* 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.
*/
/*
* Copyright (C) 2015 ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see .
*/
#include "create_index_statement.hh"
#include "prepared_statement.hh"
#include "validation.hh"
#include "service/storage_proxy.hh"
#include "service/migration_manager.hh"
#include "service/storage_service.hh"
#include "schema.hh"
#include "schema_builder.hh"
#include "request_validations.hh"
#include
#include
namespace cql3 {
namespace statements {
create_index_statement::create_index_statement(::shared_ptr name,
::shared_ptr index_name,
std::vector<::shared_ptr> raw_targets,
::shared_ptr properties,
bool if_not_exists)
: schema_altering_statement(name)
, _index_name(index_name->get_idx())
, _raw_targets(raw_targets)
, _properties(properties)
, _if_not_exists(if_not_exists)
{
}
future<>
create_index_statement::check_access(const service::client_state& state) {
return state.has_column_family_access(keyspace(), column_family(), auth::permission::ALTER);
}
void
create_index_statement::validate(distributed& proxy, const service::client_state& state)
{
auto& db = proxy.local().get_db().local();
auto schema = validation::validate_column_family(db, keyspace(), column_family());
if (schema->is_counter()) {
throw exceptions::invalid_request_exception("Secondary indexes are not supported on counter tables");
}
if (schema->is_view()) {
throw exceptions::invalid_request_exception("Secondary indexes are not supported on materialized views");
}
std::vector<::shared_ptr> targets;
for (auto& raw_target : _raw_targets) {
targets.emplace_back(raw_target->prepare(schema));
}
if (targets.empty() && !_properties->is_custom) {
throw exceptions::invalid_request_exception("Only CUSTOM indexes can be created without specifying a target column");
}
if (targets.size() > 1) {
validate_targets_for_multi_column_index(targets);
}
for (auto& target : targets) {
auto cd = schema->get_column_definition(target->column->name());
if (cd == nullptr) {
throw exceptions::invalid_request_exception(
sprint("No column definition found for column %s", *target->column));
}
if (cd->type->references_duration()) {
using request_validations::check_false;
const auto& ty = *cd->type;
check_false(ty.is_collection(), "Secondary indexes are not supported on collections containing durations");
check_false(ty.is_tuple(), "Secondary indexes are not supported on tuples containing durations");
check_false(ty.is_user_type(), "Secondary indexes are not supported on UDTs containing durations");
// We're a duration.
throw exceptions::invalid_request_exception("Secondary indexes are not supported on duration columns");
}
// Origin TODO: we could lift that limitation
if ((schema->is_dense() || !schema->thrift().has_compound_comparator()) &&
cd->kind != column_kind::regular_column) {
throw exceptions::invalid_request_exception(
"Secondary indexes are not supported on PRIMARY KEY columns in COMPACT STORAGE tables");
}
if (cd->kind == column_kind::partition_key && cd->is_on_all_components()) {
throw exceptions::invalid_request_exception(
sprint(
"Cannot create secondary index on partition key column %s",
*target->column));
}
bool is_map = dynamic_cast(cd->type.get()) != nullptr
&& dynamic_cast(cd->type.get())->is_map();
bool is_frozen_collection = cd->type->is_collection() && !cd->type->is_multi_cell();
if (is_frozen_collection) {
validate_for_frozen_collection(target);
} else {
validate_not_full_index(target);
validate_is_values_index_if_target_column_not_collection(cd, target);
validate_target_column_is_map_if_index_involves_keys(is_map, target);
}
}
if (db.existing_index_names(keyspace()).count(_index_name) > 0) {
if (_if_not_exists) {
return;
} else {
throw exceptions::invalid_request_exception("Index already exists");
}
}
_properties->validate();
}
void create_index_statement::validate_for_frozen_collection(::shared_ptr target) const
{
if (target->type != index_target::target_type::full) {
throw exceptions::invalid_request_exception(
sprint("Cannot create index on %s of frozen