before this change, we rely on `using namespace seastar` to use
`seastar::format()` without qualifying the `format()` with its
namespace. this works fine until we changed the parameter type
of format string `seastar::format()` from `const char*` to
`fmt::format_string<...>`. this change practically invited
`seastar::format()` to the club of `std::format()` and `fmt::format()`,
where all members accept a templated parameter as its `fmt`
parameter. and `seastar::format()` is not the best candidate anymore.
despite that argument-dependent lookup (ADT for short) favors the
function which is in the same namespace as its parameter, but
`using namespace` makes `seastar::format()` more competitive,
so both `std::format()` and `seastar::format()` are considered
as the condidates.
that is what is happening scylladb in quite a few caller sites of
`format()`, hence ADT is not able to tell which function the winner
in the name lookup:
```
/__w/scylladb/scylladb/mutation/mutation_fragment_stream_validator.cc:265:12: error: call to 'format' is ambiguous
265 | return format("{} ({}.{} {})", _name_view, s.ks_name(), s.cf_name(), s.id());
| ^~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/format:4290:5: note: candidate function [with _Args = <const std::basic_string_view<char> &, const seastar::basic_sstring<char, unsigned int, 15> &, const seastar::basic_sstring<char, unsigned int, 15> &, const utils::tagged_uuid<table_id_tag> &>]
4290 | format(format_string<_Args...> __fmt, _Args&&... __args)
| ^
/__w/scylladb/scylladb/seastar/include/seastar/core/print.hh:143:1: note: candidate function [with A = <const std::basic_string_view<char> &, const seastar::basic_sstring<char, unsigned int, 15> &, const seastar::basic_sstring<char, unsigned int, 15> &, const utils::tagged_uuid<table_id_tag> &>]
143 | format(fmt::format_string<A...> fmt, A&&... a) {
| ^
```
in this change, we
change all `format()` to either `fmt::format()` or `seastar::format()`
with following rules:
- if the caller expects an `sstring` or `std::string_view`, change to
`seastar::format()`
- if the caller expects an `std::string`, change to `fmt::format()`.
because, `sstring::operator std::basic_string` would incur a deep
copy.
we will need another change to enable scylladb to compile with the
latest seastar. namely, to pass the format string as a templated
parameter down to helper functions which format their parameters.
to miminize the scope of this change, let's include that change when
bumping up the seastar submodule. as that change will depend on
the seastar change.
Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
166 lines
6.5 KiB
C++
166 lines
6.5 KiB
C++
/*
|
|
* Copyright (C) 2019-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#include "cql3/statements/function_statement.hh"
|
|
#include "cql3/functions/functions.hh"
|
|
#include "cql3/functions/user_function.hh"
|
|
#include "data_dictionary/data_dictionary.hh"
|
|
#include "gms/feature_service.hh"
|
|
#include "service/storage_proxy.hh"
|
|
#include "cql3/query_processor.hh"
|
|
|
|
namespace cql3 {
|
|
namespace statements {
|
|
|
|
future<> function_statement::check_access(query_processor& qp, const service::client_state& state) const { return make_ready_future<>(); }
|
|
|
|
future<> create_function_statement_base::check_access(query_processor& qp, const service::client_state& state) const {
|
|
co_await state.has_functions_access(_name.keyspace, auth::permission::CREATE);
|
|
if (_or_replace) {
|
|
create_arg_types(qp);
|
|
sstring encoded_signature = auth::encode_signature(_name.name, _arg_types);
|
|
|
|
co_await state.has_function_access(_name.keyspace, encoded_signature, auth::permission::ALTER);
|
|
}
|
|
}
|
|
|
|
future<> drop_function_statement_base::check_access(query_processor& qp, const service::client_state& state) const
|
|
{
|
|
create_arg_types(qp);
|
|
shared_ptr<functions::function> func;
|
|
if (_args_present) {
|
|
func = functions::instance().find(_name, _arg_types);
|
|
if (!func) {
|
|
return make_ready_future<>();
|
|
}
|
|
} else {
|
|
auto funcs = functions::instance().find(_name);
|
|
if (!funcs.empty()) {
|
|
auto b = funcs.begin();
|
|
auto i = b;
|
|
if (++i != funcs.end()) {
|
|
return make_ready_future<>();
|
|
}
|
|
func = b->second;
|
|
} else {
|
|
return make_ready_future<>();
|
|
}
|
|
}
|
|
|
|
sstring encoded_signature = auth::encode_signature(_name.name, func->arg_types());
|
|
|
|
try {
|
|
return state.has_function_access(_name.keyspace, encoded_signature, auth::permission::DROP);
|
|
} catch (exceptions::invalid_request_exception&) {
|
|
return make_ready_future();
|
|
}
|
|
}
|
|
|
|
function_statement::function_statement(
|
|
functions::function_name name, std::vector<shared_ptr<cql3_type::raw>> raw_arg_types)
|
|
: _name(std::move(name)), _raw_arg_types(std::move(raw_arg_types)) {}
|
|
|
|
shared_ptr<cql_transport::event::schema_change> function_statement::create_schema_change(
|
|
const functions::function& func, bool created) {
|
|
using namespace cql_transport;
|
|
std::vector<sstring> options{func.name().name};
|
|
for (const auto& type : func.arg_types()) {
|
|
options.push_back(type->as_cql3_type().to_string());
|
|
}
|
|
auto change = created ? event::schema_change::change_type::CREATED : event::schema_change::change_type::DROPPED;
|
|
auto type = dynamic_cast<const functions::user_function*>(&func) ? event::schema_change::target_type::FUNCTION
|
|
: event::schema_change::target_type::AGGREGATE;
|
|
return ::make_shared<event::schema_change>(change, type, func.name().keyspace, std::move(options));
|
|
}
|
|
|
|
data_type function_statement::prepare_type(query_processor& qp, cql3_type::raw& t) const {
|
|
if (t.is_user_type() && t.is_frozen()) {
|
|
throw exceptions::invalid_request_exception("User defined argument and return types should not be frozen");
|
|
}
|
|
auto&& db = qp.db();
|
|
// At the CQL level the argument and return types should not have
|
|
// the frozen keyword.
|
|
// We and cassandra 3 support only frozen UDT arguments and
|
|
// returns, so freeze it here.
|
|
// It looks like cassandra 4 has updated the type checking to
|
|
// allow both frozen and non frozen arguments to a UDF (which is
|
|
// still declared without the frozen keyword).
|
|
auto prepared = t.prepare(db, _name.keyspace).get_type();
|
|
if (t.is_user_type()) {
|
|
return prepared->freeze();
|
|
}
|
|
return prepared;
|
|
}
|
|
|
|
void function_statement::create_arg_types(query_processor& qp) const {
|
|
if (!qp.proxy().features().user_defined_functions) {
|
|
throw exceptions::invalid_request_exception("User defined functions are disabled. Set enable_user_defined_functions and experimental_features:udf to enable them");
|
|
}
|
|
|
|
if (_arg_types.empty()) {
|
|
for (const auto& arg_type : _raw_arg_types) {
|
|
_arg_types.push_back(prepare_type(qp, *arg_type));
|
|
}
|
|
}
|
|
}
|
|
|
|
void function_statement::prepare_keyspace(const service::client_state& state) {
|
|
if (!_name.has_keyspace()) {
|
|
_name.keyspace = state.get_keyspace();
|
|
}
|
|
}
|
|
|
|
create_function_statement_base::create_function_statement_base(functions::function_name name,
|
|
std::vector<shared_ptr<cql3_type::raw>> raw_arg_types, bool or_replace, bool if_not_exists)
|
|
: function_statement(std::move(name), std::move(raw_arg_types)), _or_replace(or_replace), _if_not_exists(if_not_exists) {}
|
|
|
|
seastar::future<shared_ptr<functions::function>> create_function_statement_base::validate_while_executing(query_processor& qp) const {
|
|
create_arg_types(qp);
|
|
auto old = functions::instance().find(_name, _arg_types);
|
|
if (!old || _or_replace) {
|
|
co_return co_await create(qp, old.get());
|
|
}
|
|
if (!_if_not_exists) {
|
|
throw exceptions::invalid_request_exception(format("The function '{}' already exists", old));
|
|
}
|
|
co_return nullptr;
|
|
}
|
|
|
|
drop_function_statement_base::drop_function_statement_base(functions::function_name name,
|
|
std::vector<shared_ptr<cql3_type::raw>> arg_types, bool args_present, bool if_exists)
|
|
: function_statement(std::move(name), std::move(arg_types)), _args_present(args_present), _if_exists(if_exists) {}
|
|
|
|
seastar::future<shared_ptr<db::functions::function>> drop_function_statement_base::validate_while_executing(query_processor& qp) const {
|
|
create_arg_types(qp);
|
|
shared_ptr<functions::function> func;
|
|
if (_args_present) {
|
|
func = functions::instance().find(_name, _arg_types);
|
|
if (!func && !_if_exists) {
|
|
throw exceptions::invalid_request_exception(seastar::format("User function {}({}) doesn't exist", _name, _arg_types));
|
|
}
|
|
} else {
|
|
auto funcs = functions::instance().find(_name);
|
|
if (!funcs.empty()) {
|
|
auto b = funcs.begin();
|
|
auto i = b;
|
|
if (++i != funcs.end()) {
|
|
throw exceptions::invalid_request_exception(format("There are multiple functions named {}", _name));
|
|
}
|
|
func = b->second;
|
|
} else {
|
|
if (!_if_exists) {
|
|
throw exceptions::invalid_request_exception(format("No function named {} found", _name));
|
|
}
|
|
}
|
|
}
|
|
|
|
co_return func;
|
|
}
|
|
}
|
|
}
|