#pragma once #include #include #include #include #include #include namespace DB { class IAST; using ASTPtr = std::shared_ptr; /// Provides fast access to row policies' conditions for a specific user and tables. class EnabledRowPolicies { public: struct Params { UUID user_id; std::vector enabled_roles; 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); } }; ~EnabledRowPolicies(); using ConditionType = RowPolicy::ConditionType; /// 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. ASTPtr getCondition(const String & database, const String & table_name, ConditionType type) const; ASTPtr getCondition(const String & database, const String & table_name, ConditionType type, const ASTPtr & extra_condition) const; /// Returns IDs of all the policies used by the current user. std::vector getCurrentPolicyIDs() const; /// Returns IDs of the policies used by a concrete table. std::vector getCurrentPolicyIDs(const String & database, const String & table_name) const; private: friend class RowPolicyCache; EnabledRowPolicies(const Params & params_); using DatabaseAndTableName = std::pair; using DatabaseAndTableNameRef = std::pair; struct Hash { size_t operator()(const DatabaseAndTableNameRef & database_and_table_name) const; }; static constexpr size_t MAX_CONDITION_TYPE = RowPolicy::MAX_CONDITION_TYPE; using ParsedConditions = std::array; struct MixedConditions { std::unique_ptr database_and_table_name_keeper; ParsedConditions mixed_conditions; std::vector policy_ids; }; using MapOfMixedConditions = std::unordered_map; const Params params; mutable boost::atomic_shared_ptr map_of_mixed_conditions; }; }