diff --git a/cql3/Cql.g b/cql3/Cql.g index 2993d6d6e9..91e36c38de 100644 --- a/cql3/Cql.g +++ b/cql3/Cql.g @@ -1656,19 +1656,19 @@ relationType returns [cql3::expr::oper_t op] relation[std::vector& clauses] @init{ cql3::expr::oper_t rt; } - : name=cident type=relationType t=term { $clauses.emplace_back(::make_shared(std::move(name), type, std::move(t))); } + : name=cident type=relationType t=term { $clauses.emplace_back(::make_shared(std::move(name), type, cql3::expr::as_expression(std::move(t)))); } | K_TOKEN l=tupleOfIdentifiers type=relationType t=term { $clauses.emplace_back(::make_shared(std::move(l), type, std::move(t))); } | name=cident K_IS K_NOT K_NULL { - $clauses.emplace_back(make_shared(std::move(name), cql3::expr::oper_t::IS_NOT, cql3::expr::as_term_raw(cql3::expr::null()))); } + $clauses.emplace_back(::make_shared(std::move(name), cql3::expr::oper_t::IS_NOT, cql3::expr::null())); } | name=cident K_IN marker=inMarker - { $clauses.emplace_back(make_shared(std::move(name), cql3::expr::oper_t::IN, std::move(marker))); } + { $clauses.emplace_back(::make_shared(std::move(name), cql3::expr::oper_t::IN, cql3::expr::as_expression(std::move(marker)))); } | name=cident K_IN in_values=singleColumnInValues - { $clauses.emplace_back(cql3::single_column_relation::create_in_relation(std::move(name), std::move(in_values))); } + { $clauses.emplace_back(cql3::single_column_relation::create_in_relation(std::move(name), boost::copy_range>(std::move(in_values) | boost::adaptors::transformed(cql3::expr::as_expression)))); } | name=cident K_CONTAINS { rt = cql3::expr::oper_t::CONTAINS; } (K_KEY { rt = cql3::expr::oper_t::CONTAINS_KEY; })? - t=term { $clauses.emplace_back(make_shared(std::move(name), rt, std::move(t))); } - | name=cident '[' key=term ']' type=relationType t=term { $clauses.emplace_back(make_shared(std::move(name), std::move(key), type, std::move(t))); } + t=term { $clauses.emplace_back(::make_shared(std::move(name), rt, cql3::expr::as_expression(std::move(t)))); } + | name=cident '[' key=term ']' type=relationType t=term { $clauses.emplace_back(::make_shared(std::move(name), cql3::expr::as_expression(std::move(key)), type, cql3::expr::as_expression(std::move(t)))); } | ids=tupleOfIdentifiers ( K_IN ( '(' ')' diff --git a/cql3/restrictions/statement_restrictions.cc b/cql3/restrictions/statement_restrictions.cc index ef5a75266d..c44e724333 100644 --- a/cql3/restrictions/statement_restrictions.cc +++ b/cql3/restrictions/statement_restrictions.cc @@ -415,11 +415,7 @@ statement_restrictions::statement_restrictions(database& db, } // currently, the grammar only allows the NULL argument to be // "IS NOT", so this assertion should not be able to fail - assert([&] { - auto raw_term = r->get_value(); - auto raw_term_expr = dynamic_pointer_cast(raw_term); - return raw_term_expr && std::holds_alternative(raw_term_expr->as_expression()); - }()); + assert(std::holds_alternative(*r->get_value())); auto col_id = r->get_entity()->prepare_column_identifier(*schema); const auto *cd = get_column_definition(*schema, *col_id); diff --git a/cql3/single_column_relation.cc b/cql3/single_column_relation.cc index 83b525b8cf..bd2a943274 100644 --- a/cql3/single_column_relation.cc +++ b/cql3/single_column_relation.cc @@ -76,13 +76,16 @@ single_column_relation::new_EQ_restriction(database& db, schema_ptr schema, prep } if (!_map_key) { auto r = ::make_shared(column_def); - auto term = to_term(to_receivers(*schema, column_def), *_value, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto term = to_term(to_receivers(*schema, column_def), *v_term, db, schema->ks_name(), ctx); r->expression = binary_operator{&column_def, expr::oper_t::EQ, std::move(term)}; return r; } auto&& receivers = to_receivers(*schema, column_def); - auto&& entry_key = to_term({receivers[0]}, *_map_key, db, schema->ks_name(), ctx); - auto&& entry_value = to_term({receivers[1]}, *_value, db, schema->ks_name(), ctx); + auto k_term = as_term_raw(*_map_key); + auto&& entry_key = to_term({receivers[0]}, *k_term, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto&& entry_value = to_term({receivers[1]}, *v_term, db, schema->ks_name(), ctx); auto r = make_shared(column_def); r->expression = binary_operator{ column_value(&column_def, std::move(entry_key)), oper_t::EQ, std::move(entry_value)}; @@ -100,12 +103,13 @@ single_column_relation::new_IN_restriction(database& db, schema_ptr schema, prep auto receivers = to_receivers(*schema, column_def); assert(_in_values.empty() || !_value); if (_value) { - auto term = to_term(receivers, *_value, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto term = to_term(receivers, *v_term, db, schema->ks_name(), ctx); auto r = ::make_shared(column_def); r->expression = binary_operator{&column_def, expr::oper_t::IN, std::move(term)}; return r; } - auto terms = to_terms(receivers, _in_values, db, schema->ks_name(), ctx); + auto terms = to_terms(receivers, boost::copy_range>>(_in_values | boost::adaptors::transformed(expr::as_term_raw)), db, schema->ks_name(), ctx); // Convert a single-item IN restriction to an EQ restriction if (terms.size() == 1) { auto r = ::make_shared(column_def); @@ -126,7 +130,8 @@ single_column_relation::new_LIKE_restriction( throw exceptions::invalid_request_exception( format("LIKE is allowed only on string types, which {} is not", column_def.name_as_text())); } - auto term = to_term(to_receivers(*schema, column_def), *_value, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto term = to_term(to_receivers(*schema, column_def), *v_term, db, schema->ks_name(), ctx); auto r = ::make_shared(column_def); r->expression = binary_operator{&column_def, expr::oper_t::LIKE, std::move(term)}; return r; diff --git a/cql3/single_column_relation.hh b/cql3/single_column_relation.hh index d30316dbd5..c56e32f1ea 100644 --- a/cql3/single_column_relation.hh +++ b/cql3/single_column_relation.hh @@ -64,12 +64,12 @@ namespace cql3 { class single_column_relation final : public relation { private: ::shared_ptr _entity; - ::shared_ptr _map_key; - ::shared_ptr _value; - std::vector<::shared_ptr> _in_values; + std::optional _map_key; + std::optional _value; + std::vector _in_values; public: - single_column_relation(::shared_ptr entity, ::shared_ptr map_key, - expr::oper_t type, ::shared_ptr value, std::vector<::shared_ptr> in_values) + single_column_relation(::shared_ptr entity, std::optional map_key, + expr::oper_t type, std::optional value, std::vector in_values) : relation(type) , _entity(std::move(entity)) , _map_key(std::move(map_key)) @@ -85,8 +85,8 @@ public: * @param type the type that describes how this entity relates to the value. * @param value the value being compared. */ - single_column_relation(::shared_ptr entity, ::shared_ptr map_key, - expr::oper_t type, ::shared_ptr value) + single_column_relation(::shared_ptr entity, std::optional map_key, + expr::oper_t type, std::optional value) : single_column_relation(std::move(entity), std::move(map_key), type, std::move(value), {}) { } @@ -97,20 +97,20 @@ public: * @param type the type that describes how this entity relates to the value. * @param value the value being compared. */ - single_column_relation(::shared_ptr entity, expr::oper_t type, ::shared_ptr value) + single_column_relation(::shared_ptr entity, expr::oper_t type, expr::expression value) : single_column_relation(std::move(entity), {}, type, std::move(value)) { } static ::shared_ptr create_in_relation(::shared_ptr entity, - std::vector<::shared_ptr> in_values) { - return ::make_shared(std::move(entity), nullptr, expr::oper_t::IN, nullptr, std::move(in_values)); + std::vector in_values) { + return ::make_shared(std::move(entity), std::nullopt, expr::oper_t::IN, std::nullopt, std::move(in_values)); } ::shared_ptr get_entity() { return _entity; } - ::shared_ptr get_value() { + const std::optional& get_value() { return _value; } @@ -134,14 +134,14 @@ protected: virtual sstring to_string() const override { auto entity_as_string = _entity->to_cql_string(); if (_map_key) { - entity_as_string = format("{}[{}]", std::move(entity_as_string), _map_key->to_string()); + entity_as_string = format("{}[{}]", std::move(entity_as_string), *_map_key); } if (is_IN()) { return format("{} IN ({})", entity_as_string, join(", ", _in_values)); } - return format("{} {} {}", entity_as_string, _relation_type, _value->to_string()); + return format("{} {} {}", entity_as_string, _relation_type, *_value); } protected: @@ -169,7 +169,8 @@ protected: throw exceptions::invalid_request_exception("Slice restrictions are not supported on duration columns"); } - auto term = to_term(to_receivers(*schema, column_def), *_value, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto term = to_term(to_receivers(*schema, column_def), *v_term, db, schema->ks_name(), ctx); auto r = ::make_shared(column_def); r->expression = expr::binary_operator{&column_def, _relation_type, std::move(term)}; return r; @@ -179,7 +180,8 @@ protected: prepare_context& ctx, bool is_key) override { auto&& column_def = to_column_definition(*schema, *_entity); - auto term = to_term(to_receivers(*schema, column_def), *_value, db, schema->ks_name(), ctx); + auto v_term = as_term_raw(*_value); + auto term = to_term(to_receivers(*schema, column_def), *v_term, db, schema->ks_name(), ctx); auto r = ::make_shared(column_def); r->expression = expr::binary_operator{ &column_def, is_key ? expr::oper_t::CONTAINS_KEY : expr::oper_t::CONTAINS, std::move(term)};