/* * 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 "update_statement.hh" #include "raw/update_statement.hh" #include "raw/insert_statement.hh" #include "unimplemented.hh" #include "cql3/operation_impl.hh" namespace cql3 { namespace statements { update_statement::update_statement(statement_type type, uint32_t bound_terms, schema_ptr s, std::unique_ptr attrs, uint64_t* cql_stats_counter_ptr) : modification_statement{type, bound_terms, std::move(s), std::move(attrs), cql_stats_counter_ptr} { } bool update_statement::require_full_clustering_key() const { return true; } bool update_statement::allow_clustering_key_slices() const { return false; } void update_statement::add_update_for_key(mutation& m, const query::clustering_range& range, const update_parameters& params) { auto prefix = range.start() ? std::move(range.start()->value()) : clustering_key_prefix::make_empty(); if (s->is_dense()) { if (prefix.is_empty(*s) || prefix.components().front().empty()) { throw exceptions::invalid_request_exception(sprint("Missing PRIMARY KEY part %s", s->clustering_key_columns().begin()->name_as_text())); } // An empty name for the value is what we use to recognize the case where there is not column // outside the PK, see CreateStatement. // Since v3 schema we use empty_type instead, see schema.cc. auto rb = s->regular_begin(); if (rb->name().empty() || rb->type == empty_type) { // There is no column outside the PK. So no operation could have passed through validation assert(_column_operations.empty()); constants::setter(*s->regular_begin(), make_shared(constants::value(cql3::raw_value::make_value(bytes())))).execute(m, prefix, params); } else { // dense means we don't have a row marker, so don't accept to set only the PK. See CASSANDRA-5648. if (_column_operations.empty()) { throw exceptions::invalid_request_exception(sprint("Column %s is mandatory for this COMPACT STORAGE table", s->regular_begin()->name_as_text())); } } } else { // If there are static columns, there also must be clustering columns, in which // case empty prefix can only refer to the static row. bool is_static_prefix = s->has_static_columns() && prefix.is_empty(*s); if (type.is_insert() && !is_static_prefix && s->is_cql3_table()) { auto& row = m.partition().clustered_row(*s, prefix); row.apply(row_marker(params.timestamp(), params.ttl(), params.expiry())); } } for (auto&& update : _column_operations) { update->execute(m, prefix, params); } warn(unimplemented::cause::INDEXES); #if 0 SecondaryIndexManager indexManager = Keyspace.open(cfm.ksName).getColumnFamilyStore(cfm.cfId).indexManager; if (indexManager.hasIndexes()) { for (Cell cell : cf) { // Indexed values must be validated by any applicable index. See CASSANDRA-3057/4240/8081 for more details if (!indexManager.validate(cell)) throw new InvalidRequestException(String.format("Can't index column value of size %d for index %s on %s.%s", cell.value().remaining(), cfm.getColumnDefinition(cell.name()).getIndexName(), cfm.ksName, cfm.cfName)); } } } #endif } namespace raw { insert_statement::insert_statement( ::shared_ptr name, ::shared_ptr attrs, std::vector<::shared_ptr> column_names, std::vector<::shared_ptr> column_values, bool if_not_exists) : raw::modification_statement{std::move(name), std::move(attrs), conditions_vector{}, if_not_exists, false} , _column_names{std::move(column_names)} , _column_values{std::move(column_values)} { } ::shared_ptr insert_statement::prepare_internal(database& db, schema_ptr schema, ::shared_ptr bound_names, std::unique_ptr attrs, cql_stats& stats) { auto stmt = ::make_shared(statement_type::INSERT, bound_names->size(), schema, std::move(attrs), &stats.inserts); // Created from an INSERT if (stmt->is_counter()) { throw exceptions::invalid_request_exception("INSERT statement are not allowed on counter tables, use UPDATE instead"); } if (_column_names.size() != _column_values.size()) { throw exceptions::invalid_request_exception("Unmatched column names/values"); } if (_column_names.empty()) { throw exceptions::invalid_request_exception("No columns provided to INSERT"); } std::vector<::shared_ptr> relations; std::unordered_set column_ids; for (size_t i = 0; i < _column_names.size(); i++) { auto&& col = _column_names[i]; auto id = col->prepare_column_identifier(schema); auto def = get_column_definition(schema, *id); if (!def) { throw exceptions::invalid_request_exception(sprint("Unknown identifier %s", *id)); } if (column_ids.count(id->name())) { throw exceptions::invalid_request_exception(sprint("Multiple definitions found for column %s", *id)); } column_ids.emplace(id->name()); auto&& value = _column_values[i]; if (def->is_primary_key()) { relations.push_back(::make_shared(col, operator_type::EQ, value)); } else { auto operation = operation::set_value(value).prepare(db, keyspace(), *def); operation->collect_marker_specification(bound_names); stmt->add_operation(std::move(operation)); }; } stmt->process_where_clause(db, relations, std::move(bound_names)); return stmt; } update_statement::update_statement( ::shared_ptr name, ::shared_ptr attrs, std::vector, ::shared_ptr>> updates, std::vector where_clause, conditions_vector conditions) : raw::modification_statement(std::move(name), std::move(attrs), std::move(conditions), false, false) , _updates(std::move(updates)) , _where_clause(std::move(where_clause)) { } ::shared_ptr update_statement::prepare_internal(database& db, schema_ptr schema, ::shared_ptr bound_names, std::unique_ptr attrs, cql_stats& stats) { auto stmt = ::make_shared(statement_type::UPDATE, bound_names->size(), schema, std::move(attrs), &stats.updates); for (auto&& entry : _updates) { auto id = entry.first->prepare_column_identifier(schema); auto def = get_column_definition(schema, *id); if (!def) { throw exceptions::invalid_request_exception(sprint("Unknown identifier %s", *entry.first)); } auto operation = entry.second->prepare(db, keyspace(), *def); operation->collect_marker_specification(bound_names); if (def->is_primary_key()) { throw exceptions::invalid_request_exception(sprint("PRIMARY KEY part %s found in SET part", *entry.first)); } stmt->add_operation(std::move(operation)); } stmt->process_where_clause(db, _where_clause, std::move(bound_names)); return stmt; } } } }