cql3: Replace uses of single_column_restriction with restriction

single_column_restriction is a class used to represent
restrictions in a single column.

The class is very simple - it's basically an expression
with some additional information.

As a step towards removing all restriction classes
all uses of this class are replaced by uses of
the generic restriction class.

All functionality of this class has been implemented
using free standing functions operating on expressions.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
This commit is contained in:
Jan Ciolek
2022-06-28 08:09:31 +02:00
parent afc482f0a5
commit 3e3d2f939c
5 changed files with 42 additions and 34 deletions

View File

@@ -95,7 +95,7 @@ void validate_single_column_relation(const column_value& lhs, oper_t oper, const
const schema& schema) {
validate_single_column_relation(lhs_col, oper, schema, false);
::shared_ptr<restrictions::restriction> r = ::make_shared<restrictions::single_column_restriction>(*lhs_col.col);
::shared_ptr<restrictions::restriction> r = ::make_shared<restrictions::restriction>();
r->expression = binary_operator(std::move(lhs_col), oper, std::move(prepared_rhs), order);
return r;
}
@@ -108,7 +108,7 @@ void validate_single_column_relation(const column_value& lhs, oper_t oper, const
const column_value& sub_col = get_subscripted_column(lhs_sub);
validate_single_column_relation(sub_col, oper, schema, true);
::shared_ptr<restrictions::restriction> r = ::make_shared<restrictions::single_column_restriction>(*sub_col.col);
::shared_ptr<restrictions::restriction> r = ::make_shared<restrictions::restriction>();
r->expression = binary_operator(std::move(lhs_sub), oper, std::move(prepared_rhs));
return r;
}

View File

@@ -86,7 +86,7 @@ public:
if (!this_cdef) {
throw exceptions::invalid_request_exception(format("Base column {} not found in view index schema", other_cdef->name_as_text()));
}
auto r = ::make_shared<single_column_restriction>(*this_cdef);
auto r = ::make_shared<restriction>(*this_cdef);
r->expression = replace_column_def(entry.second->expression, this_cdef);
_restrictions->add_restriction(r);
}
@@ -96,10 +96,10 @@ public:
return _restrictions->is_all_eq();
}
void do_merge_with(::shared_ptr<single_column_restriction> restriction) {
void do_merge_with(const ::shared_ptr<restriction>& single_column_restriction) {
if (!_restrictions->empty() && !_allow_filtering) {
auto last_column = *_restrictions->last_column();
auto new_column = restriction->get_column_def();
auto new_column = *get_the_only_column(single_column_restriction->expression).col;
if (has_slice(this->expression) && _schema->position(new_column) > _schema->position(last_column)) {
throw exceptions::invalid_request_exception(format("Clustering column \"{}\" cannot be restricted (preceding column \"{}\" is restricted by a non-EQ relation)",
@@ -107,14 +107,14 @@ public:
}
if (_schema->position(new_column) < _schema->position(last_column)) {
if (has_slice(restriction->expression)) {
if (has_slice(single_column_restriction->expression)) {
throw exceptions::invalid_request_exception(format("PRIMARY KEY column \"{}\" cannot be restricted (preceding column \"{}\" is restricted by a non-EQ relation)",
last_column.name_as_text(), new_column.name_as_text()));
}
}
}
_restrictions->add_restriction(restriction);
this->expression = make_conjunction(std::move(this->expression), restriction->expression);
_restrictions->add_restriction(single_column_restriction);
this->expression = make_conjunction(std::move(this->expression), single_column_restriction->expression);
}
virtual void merge_with(::shared_ptr<restriction> restriction) override {
@@ -129,7 +129,7 @@ public:
format("Columns \"{}\" cannot be restricted by both a normal relation and a token relation",
join(", ", get_column_defs())));
}
do_merge_with(::static_pointer_cast<single_column_restriction>(restriction));
do_merge_with(restriction);
}
std::vector<ValueType> values_as_keys(const query_options& options) const {
@@ -223,11 +223,11 @@ inline unsigned single_column_primary_key_restrictions<clustering_key>::num_pref
unsigned int count = 0;
for (const auto& restriction : restrictions() | boost::adaptors::map_values) {
if (find_needs_filtering(restriction->expression)
|| position != restriction->get_column_def().id) {
|| position != get_the_only_column(restriction->expression).col->id) {
return count;
}
if (!has_slice(restriction->expression)) {
position = restriction->get_column_def().id + 1;
position = get_the_only_column(restriction->expression).col->id + 1;
}
count++;
}

View File

@@ -44,7 +44,7 @@ private:
* The _restrictions per column.
*/
public:
using restrictions_map = std::map<const column_definition*, ::shared_ptr<single_column_restriction>, column_definition_comparator>;
using restrictions_map = std::map<const column_definition*, ::shared_ptr<restriction>, column_definition_comparator>;
private:
restrictions_map _restrictions;
bool _is_all_eq = true;
@@ -91,7 +91,7 @@ public:
* @param column_def the column definition
* @return the restriction associated to the specified column
*/
::shared_ptr<single_column_restriction> get_restriction(const column_definition& column_def) const {
::shared_ptr<restriction> get_restriction(const column_definition& column_def) const {
auto i = _restrictions.find(&column_def);
if (i == _restrictions.end()) {
return {};
@@ -113,14 +113,14 @@ public:
* @param restriction the restriction to add
* @throws InvalidRequestException if the new restriction cannot be added
*/
void add_restriction(::shared_ptr<single_column_restriction> restriction) {
void add_restriction(::shared_ptr<restriction> restriction) {
if (!find(restriction->expression, expr::oper_t::EQ)) {
_is_all_eq = false;
}
auto i = _restrictions.find(&restriction->get_column_def());
auto i = _restrictions.find(get_the_only_column(restriction->expression).col);
if (i == _restrictions.end()) {
_restrictions.emplace_hint(i, &restriction->get_column_def(), std::move(restriction));
_restrictions.emplace_hint(i, get_the_only_column(restriction->expression).col, std::move(restriction));
} else {
auto& e = i->second->expression;
e = make_conjunction(std::move(e), restriction->expression);

View File

@@ -439,8 +439,8 @@ statement_restrictions::statement_restrictions(data_dictionary::database db,
} else if (has_token(restriction->expression)) {
_partition_key_restrictions = _partition_key_restrictions->merge_to(_schema, restriction);
} else {
auto single = ::static_pointer_cast<single_column_restriction>(restriction);
auto& def = single->get_column_def();
auto single = restriction;
auto& def = *get_the_only_column(single->expression).col;
if (def.is_partition_key()) {
// View definition allows PK slices, because it's not a performance problem.
if (has_slice(restriction->expression) && !allow_filtering && !for_view) {
@@ -629,20 +629,22 @@ std::vector<const column_definition*> statement_restrictions::get_column_defs_fo
if (need_filtering()) {
auto& sim = db.find_column_family(_schema).get_index_manager();
auto opt_idx = std::get<0>(find_idx(sim));
auto column_uses_indexing = [&opt_idx] (const column_definition* cdef, ::shared_ptr<single_column_restriction> restr) {
return opt_idx && restr && is_supported_by(restr->expression, *opt_idx);
auto column_uses_indexing = [&opt_idx] (const column_definition* cdef, const expr::expression* single_col_restr) {
return opt_idx && single_col_restr && is_supported_by(*single_col_restr, *opt_idx);
};
auto single_pk_restrs = dynamic_pointer_cast<single_column_partition_key_restrictions>(_partition_key_restrictions);
if (_partition_key_restrictions->needs_filtering(*_schema)) {
for (auto&& cdef : _partition_key_restrictions->get_column_defs()) {
::shared_ptr<single_column_restriction> restr;
const expr::expression* single_col_restr = nullptr;
if (single_pk_restrs) {
auto it = single_pk_restrs->restrictions().find(cdef);
if (it != single_pk_restrs->restrictions().end()) {
restr = dynamic_pointer_cast<single_column_restriction>(it->second);
if (is_single_column_restriction(it->second->expression)) {
single_col_restr = &it->second->expression;
}
}
}
if (!column_uses_indexing(cdef, restr)) {
if (!column_uses_indexing(cdef, single_col_restr)) {
column_defs_for_filtering.emplace_back(cdef);
}
}
@@ -653,21 +655,27 @@ std::vector<const column_definition*> statement_restrictions::get_column_defs_fo
column_id first_filtering_id = pk_has_unrestricted_components ? 0 : _schema->clustering_key_columns().begin()->id +
_clustering_columns_restrictions->num_prefix_columns_that_need_not_be_filtered();
for (auto&& cdef : _clustering_columns_restrictions->get_column_defs()) {
::shared_ptr<single_column_restriction> restr;
const expr::expression* single_col_restr = nullptr;
if (single_ck_restrs) {
auto it = single_ck_restrs->restrictions().find(cdef);
if (it != single_ck_restrs->restrictions().end()) {
restr = dynamic_pointer_cast<single_column_restriction>(it->second);
if (is_single_column_restriction(it->second->expression)) {
single_col_restr = &it->second->expression;
}
}
}
if (cdef->id >= first_filtering_id && !column_uses_indexing(cdef, restr)) {
if (cdef->id >= first_filtering_id && !column_uses_indexing(cdef, single_col_restr)) {
column_defs_for_filtering.emplace_back(cdef);
}
}
}
for (auto&& cdef : _nonprimary_key_restrictions->get_column_defs()) {
auto restr = dynamic_pointer_cast<single_column_restriction>(_nonprimary_key_restrictions->get_restriction(*cdef));
if (!column_uses_indexing(cdef, restr)) {
::shared_ptr<restriction> cur_restr = _nonprimary_key_restrictions->get_restriction(*cdef);
const expr::expression* single_col_restr = nullptr;
if (is_single_column_restriction(cur_restr->expression)) {
single_col_restr = &cur_restr->expression;
}
if (!column_uses_indexing(cdef, single_col_restr)) {
column_defs_for_filtering.emplace_back(cdef);
}
}

View File

@@ -454,11 +454,11 @@ bool result_set_builder::restrictions_filter::do_filter(const selection& selecti
if (restr_it == non_pk_restrictions_map.end()) {
continue;
}
restrictions::single_column_restriction& restriction = *restr_it->second;
restrictions::restriction& single_col_restriction = *restr_it->second;
// FIXME: push to upper layer so it happens once per row
auto static_and_regular_columns = expr::get_non_pk_values(selection, static_row, row);
bool regular_restriction_matches = expr::is_satisfied_by(
restriction.expression,
single_col_restriction.expression,
expr::evaluation_inputs{
.partition_key = &partition_key,
.clustering_key = &clustering_key,
@@ -481,9 +481,9 @@ bool result_set_builder::restrictions_filter::do_filter(const selection& selecti
if (restr_it == partition_key_restrictions_map.end()) {
continue;
}
restrictions::single_column_restriction& restriction = *restr_it->second;
restrictions::restriction& single_col_restriction = *restr_it->second;
if (!expr::is_satisfied_by(
restriction.expression,
single_col_restriction.expression,
expr::evaluation_inputs{
.partition_key = &partition_key,
.clustering_key = &clustering_key,
@@ -508,9 +508,9 @@ bool result_set_builder::restrictions_filter::do_filter(const selection& selecti
if (clustering_key.empty()) {
return false;
}
restrictions::single_column_restriction& restriction = *restr_it->second;
restrictions::restriction& single_col_restriction = *restr_it->second;
if (!expr::is_satisfied_by(
restriction.expression,
single_col_restriction.expression,
expr::evaluation_inputs{
.partition_key = &partition_key,
.clustering_key = &clustering_key,