2019-11-17 11:57:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
#include <Access/Common/RowPolicyDefs.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2019-11-17 11:57:02 +00:00
|
|
|
#include <Core/UUID.h>
|
2021-11-18 13:04:42 +00:00
|
|
|
#include <boost/container/flat_set.hpp>
|
2020-02-12 23:59:49 +00:00
|
|
|
#include <boost/smart_ptr/atomic_shared_ptr.hpp>
|
2019-11-17 11:57:02 +00:00
|
|
|
#include <unordered_map>
|
2020-05-07 02:45:27 +00:00
|
|
|
#include <memory>
|
2019-11-17 11:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class IAST;
|
|
|
|
using ASTPtr = std::shared_ptr<IAST>;
|
|
|
|
|
|
|
|
|
|
|
|
/// Provides fast access to row policies' conditions for a specific user and tables.
|
2020-03-07 17:37:38 +00:00
|
|
|
class EnabledRowPolicies
|
2019-11-17 11:57:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-07 17:37:38 +00:00
|
|
|
struct Params
|
|
|
|
{
|
|
|
|
UUID user_id;
|
2020-04-29 19:35:56 +00:00
|
|
|
boost::container::flat_set<UUID> enabled_roles;
|
2020-03-07 17:37:38 +00:00
|
|
|
|
|
|
|
auto toTuple() const { return std::tie(user_id, enabled_roles); }
|
|
|
|
friend bool operator ==(const Params & lhs, const Params & rhs) { return lhs.toTuple() == rhs.toTuple(); }
|
|
|
|
friend bool operator !=(const Params & lhs, const Params & rhs) { return !(lhs == rhs); }
|
|
|
|
friend bool operator <(const Params & lhs, const Params & rhs) { return lhs.toTuple() < rhs.toTuple(); }
|
|
|
|
friend bool operator >(const Params & lhs, const Params & rhs) { return rhs < lhs; }
|
|
|
|
friend bool operator <=(const Params & lhs, const Params & rhs) { return !(rhs < lhs); }
|
|
|
|
friend bool operator >=(const Params & lhs, const Params & rhs) { return !(lhs < rhs); }
|
|
|
|
};
|
2019-11-17 11:57:02 +00:00
|
|
|
|
2021-07-15 08:55:51 +00:00
|
|
|
EnabledRowPolicies();
|
2020-03-07 17:37:38 +00:00
|
|
|
~EnabledRowPolicies();
|
2019-11-17 11:57:02 +00:00
|
|
|
|
|
|
|
/// Returns prepared filter for a specific table and operations.
|
|
|
|
/// The function can return nullptr, that means there is no filters applied.
|
|
|
|
/// The returned filter can be a combination of the filters defined by multiple row policies.
|
2021-11-18 13:04:42 +00:00
|
|
|
ASTPtr getFilter(const String & database, const String & table_name, RowPolicyFilterType filter_type) const;
|
|
|
|
ASTPtr getFilter(const String & database, const String & table_name, RowPolicyFilterType filter_type, const ASTPtr & combine_with_expr) const;
|
2020-02-12 20:47:37 +00:00
|
|
|
|
2019-11-17 11:57:02 +00:00
|
|
|
private:
|
2020-03-07 17:37:38 +00:00
|
|
|
friend class RowPolicyCache;
|
|
|
|
EnabledRowPolicies(const Params & params_);
|
2019-11-17 11:57:02 +00:00
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
struct MixedFiltersKey
|
2020-05-07 02:45:27 +00:00
|
|
|
{
|
|
|
|
std::string_view database;
|
|
|
|
std::string_view table_name;
|
2021-11-18 13:04:42 +00:00
|
|
|
RowPolicyFilterType filter_type;
|
2020-05-07 02:45:27 +00:00
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
auto toTuple() const { return std::tie(database, table_name, filter_type); }
|
|
|
|
friend bool operator==(const MixedFiltersKey & left, const MixedFiltersKey & right) { return left.toTuple() == right.toTuple(); }
|
|
|
|
friend bool operator!=(const MixedFiltersKey & left, const MixedFiltersKey & right) { return left.toTuple() != right.toTuple(); }
|
2020-05-07 02:45:27 +00:00
|
|
|
};
|
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
struct MixedFiltersResult
|
2019-11-17 11:57:02 +00:00
|
|
|
{
|
2021-11-18 13:04:42 +00:00
|
|
|
ASTPtr ast;
|
|
|
|
std::shared_ptr<const std::pair<String, String>> database_and_table_name;
|
2019-11-17 11:57:02 +00:00
|
|
|
};
|
2020-05-07 02:45:27 +00:00
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
struct Hash
|
2019-11-17 11:57:02 +00:00
|
|
|
{
|
2021-11-18 13:04:42 +00:00
|
|
|
size_t operator()(const MixedFiltersKey & key) const;
|
2019-11-17 11:57:02 +00:00
|
|
|
};
|
2021-11-18 13:04:42 +00:00
|
|
|
|
|
|
|
using MixedFiltersMap = std::unordered_map<MixedFiltersKey, MixedFiltersResult, Hash>;
|
2019-11-17 11:57:02 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
const Params params;
|
2021-11-18 13:04:42 +00:00
|
|
|
mutable boost::atomic_shared_ptr<const MixedFiltersMap> mixed_filters;
|
2019-11-17 11:57:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|