Files
scylladb/cql3/functions/abstract_function.hh
Kefu Chai c37f4e5252 treewide: use fmt::join() when appropriate
now that fmtlib provides fmt::join(). see
https://fmt.dev/latest/api.html#_CPPv4I0EN3fmt4joinE9join_viewIN6detail10iterator_tI5RangeEEN6detail10sentinel_tI5RangeEEERR5Range11string_view
there is not need to revent the wheel. so in this change, the homebrew
join() is replaced with fmt::join().

as fmt::join() returns an join_view(), this could improve the
performance under certain circumstances where the fully materialized
string is not needed.

please note, the goal of this change is to use fmt::join(), and this
change does not intend to improve the performance of existing
implementation based on "operator<<" unless the new implementation is
much more complicated. we will address the unnecessarily materialized
strings in a follow-up commit.

some noteworthy things related to this change:

* unlike the existing `join()`, `fmt::join()` returns a view. so we
  have to materialize the view if what we expect is a `sstring`
* `fmt::format()` does not accept a view, so we cannot pass the
  return value of `fmt::join()` to `fmt::format()`
* fmtlib does not format a typed pointer, i.e., it does not format,
  for instance, a `const std::string*`. but operator<<() always print
  a typed pointer. so if we want to format a typed pointer, we either
  need to cast the pointer to `void*` or use `fmt::ptr()`.
* fmtlib is not able to pick up the overload of
  `operator<<(std::ostream& os, const column_definition* cd)`, so we
  have to use a wrapper class of `maybe_column_definition` for printing
  a pointer to `column_definition`. since the overload is only used
  by the two overloads of
  `statement_restrictions::add_single_column_parition_key_restriction()`,
  the operator<< for `const column_definition*` is dropped.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
2023-03-16 20:34:18 +08:00

99 lines
2.3 KiB
C++

/*
* Copyright (C) 2014-present ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include "function.hh"
#include "types/types.hh"
#include "cql3/cql3_type.hh"
#include <vector>
#include <iosfwd>
#include <boost/functional/hash.hpp>
#include "cql3/functions/function_name.hh"
namespace std {
std::ostream& operator<<(std::ostream& os, const std::vector<data_type>& arg_types);
}
namespace cql3 {
namespace functions {
/**
* Base class for our native/hardcoded functions.
*/
class abstract_function : public virtual function {
protected:
function_name _name;
std::vector<data_type> _arg_types;
data_type _return_type;
abstract_function(function_name name, std::vector<data_type> arg_types, data_type return_type)
: _name(std::move(name)), _arg_types(std::move(arg_types)), _return_type(std::move(return_type)) {
}
public:
virtual bool requires_thread() const override;
virtual const function_name& name() const override {
return _name;
}
virtual const std::vector<data_type>& arg_types() const override {
return _arg_types;
}
virtual const data_type& return_type() const override {
return _return_type;
}
bool operator==(const abstract_function& x) const {
return _name == x._name
&& _arg_types == x._arg_types
&& _return_type == x._return_type;
}
virtual sstring column_name(const std::vector<sstring>& column_names) const override {
return format("{}({})", _name, fmt::join(column_names, ", "));
}
virtual void print(std::ostream& os) const override;
};
inline
void
abstract_function::print(std::ostream& os) const {
os << _name << " : (";
os << _arg_types;
os << ") -> " << _return_type->as_cql3_type().to_string();
}
}
}
namespace std {
template <>
struct hash<cql3::functions::abstract_function> {
size_t operator()(const cql3::functions::abstract_function& f) const {
using namespace cql3::functions;
size_t v = 0;
boost::hash_combine(v, std::hash<function_name>()(f.name()));
boost::hash_combine(v, boost::hash_value(f.arg_types()));
// FIXME: type hash
//boost::hash_combine(v, std::hash<shared_ptr<abstract_type>>()(f.return_type()));
return v;
}
};
}