cql3: single_column_relation: convert term::raw to expressions

Change term::raw in single_column_relation to expressions. Because a single
raw class is used to represent multiple shapes (IN ? and IN (x, y, z)),
some of the expressions are optional, corresponding to nullables before the
conversion.

to_term() is not converted, since it's part of the larger relation
hierarchy.
This commit is contained in:
Avi Kivity
2021-08-09 15:37:55 +03:00
parent 793aca8e4e
commit 4809cf7ff3
4 changed files with 35 additions and 32 deletions

View File

@@ -1656,19 +1656,19 @@ relationType returns [cql3::expr::oper_t op]
relation[std::vector<cql3::relation_ptr>& clauses]
@init{ cql3::expr::oper_t rt; }
: name=cident type=relationType t=term { $clauses.emplace_back(::make_shared<cql3::single_column_relation>(std::move(name), type, std::move(t))); }
: name=cident type=relationType t=term { $clauses.emplace_back(::make_shared<cql3::single_column_relation>(std::move(name), type, cql3::expr::as_expression(std::move(t)))); }
| K_TOKEN l=tupleOfIdentifiers type=relationType t=term
{ $clauses.emplace_back(::make_shared<cql3::token_relation>(std::move(l), type, std::move(t))); }
| name=cident K_IS K_NOT K_NULL {
$clauses.emplace_back(make_shared<cql3::single_column_relation>(std::move(name), cql3::expr::oper_t::IS_NOT, cql3::expr::as_term_raw(cql3::expr::null()))); }
$clauses.emplace_back(::make_shared<cql3::single_column_relation>(std::move(name), cql3::expr::oper_t::IS_NOT, cql3::expr::null())); }
| name=cident K_IN marker=inMarker
{ $clauses.emplace_back(make_shared<cql3::single_column_relation>(std::move(name), cql3::expr::oper_t::IN, std::move(marker))); }
{ $clauses.emplace_back(::make_shared<cql3::single_column_relation>(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::vector<cql3::expr::expression>>(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<cql3::single_column_relation>(std::move(name), rt, std::move(t))); }
| name=cident '[' key=term ']' type=relationType t=term { $clauses.emplace_back(make_shared<cql3::single_column_relation>(std::move(name), std::move(key), type, std::move(t))); }
t=term { $clauses.emplace_back(::make_shared<cql3::single_column_relation>(std::move(name), rt, cql3::expr::as_expression(std::move(t)))); }
| name=cident '[' key=term ']' type=relationType t=term { $clauses.emplace_back(::make_shared<cql3::single_column_relation>(std::move(name), cql3::expr::as_expression(std::move(key)), type, cql3::expr::as_expression(std::move(t)))); }
| ids=tupleOfIdentifiers
( K_IN
( '(' ')'

View File

@@ -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<expr::term_raw_expr>(raw_term);
return raw_term_expr && std::holds_alternative<expr::null>(raw_term_expr->as_expression());
}());
assert(std::holds_alternative<expr::null>(*r->get_value()));
auto col_id = r->get_entity()->prepare_column_identifier(*schema);
const auto *cd = get_column_definition(*schema, *col_id);

View File

@@ -76,13 +76,16 @@ single_column_relation::new_EQ_restriction(database& db, schema_ptr schema, prep
}
if (!_map_key) {
auto r = ::make_shared<restrictions::single_column_restriction>(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<restrictions::single_column_restriction>(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<single_column_restriction>(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<std::vector<::shared_ptr<term::raw>>>(_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<single_column_restriction>(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<restrictions::single_column_restriction>(column_def);
r->expression = binary_operator{&column_def, expr::oper_t::LIKE, std::move(term)};
return r;

View File

@@ -64,12 +64,12 @@ namespace cql3 {
class single_column_relation final : public relation {
private:
::shared_ptr<column_identifier::raw> _entity;
::shared_ptr<term::raw> _map_key;
::shared_ptr<term::raw> _value;
std::vector<::shared_ptr<term::raw>> _in_values;
std::optional<expr::expression> _map_key;
std::optional<expr::expression> _value;
std::vector<expr::expression> _in_values;
public:
single_column_relation(::shared_ptr<column_identifier::raw> entity, ::shared_ptr<term::raw> map_key,
expr::oper_t type, ::shared_ptr<term::raw> value, std::vector<::shared_ptr<term::raw>> in_values)
single_column_relation(::shared_ptr<column_identifier::raw> entity, std::optional<expr::expression> map_key,
expr::oper_t type, std::optional<expr::expression> value, std::vector<expr::expression> 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<column_identifier::raw> entity, ::shared_ptr<term::raw> map_key,
expr::oper_t type, ::shared_ptr<term::raw> value)
single_column_relation(::shared_ptr<column_identifier::raw> entity, std::optional<expr::expression> map_key,
expr::oper_t type, std::optional<expr::expression> 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<column_identifier::raw> entity, expr::oper_t type, ::shared_ptr<term::raw> value)
single_column_relation(::shared_ptr<column_identifier::raw> entity, expr::oper_t type, expr::expression value)
: single_column_relation(std::move(entity), {}, type, std::move(value))
{ }
static ::shared_ptr<single_column_relation> create_in_relation(::shared_ptr<column_identifier::raw> entity,
std::vector<::shared_ptr<term::raw>> in_values) {
return ::make_shared<single_column_relation>(std::move(entity), nullptr, expr::oper_t::IN, nullptr, std::move(in_values));
std::vector<expr::expression> in_values) {
return ::make_shared<single_column_relation>(std::move(entity), std::nullopt, expr::oper_t::IN, std::nullopt, std::move(in_values));
}
::shared_ptr<column_identifier::raw> get_entity() {
return _entity;
}
::shared_ptr<term::raw> get_value() {
const std::optional<expr::expression>& 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<restrictions::single_column_restriction>(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<restrictions::single_column_restriction>(column_def);
r->expression = expr::binary_operator{
&column_def, is_key ? expr::oper_t::CONTAINS_KEY : expr::oper_t::CONTAINS, std::move(term)};