Commit Graph

42 Commits

Author SHA1 Message Date
Avi Kivity
c72906a2ee cql3: expr: drop nested_expression
Now that expression can be nested in its component types
directly, we can remove nested_expression. Most of the patch
adjusts uses to drop the dereference that was needed for
nested_expression.
2021-09-28 23:49:21 +03:00
Avi Kivity
be44b579a1 cql3: expr: introduce as/as_if/is
Simple wrappers for std::get, std::get_if, std::holds_alternative.

The new names are shorter and IMO more readable.

Call sites are updated.

We will later replace the implementation.
2021-09-28 23:49:11 +03:00
Avi Kivity
e7db3def4f cql3: expr: introduce expr::visit, replacing std::visit
The new expr::visit() is just a wrapper around std::visit(),
but has better constraints. A call to expr::visit() with a
visitor that misses an overload will produce an error message
that points at the missing type. This is done using the new
invocable_on_expression concept. Note it lists the expression
types one by one rather than using template magic, since
otherwise we won't get the nice messages.

Later, we will change the implementation when expression becomes
our own type rather than std::variant.

Call sites are updated.
2021-09-28 23:48:42 +03:00
Jan Ciolek
48e5277b2f cql3: maps: Implement get_value_type in maps.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in mapshh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-21 16:13:36 +02:00
Jan Ciolek
5aae370928 cql3: sets: Implement get_value_type in sets.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in sets.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-21 16:13:36 +02:00
Jan Ciolek
6bf6b03d12 cql3: lists: Implement get_value_type in lists.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in lists.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-21 16:13:36 +02:00
Jan Ciolek
da7ca5a760 cql3: constants: Implement get_value_type in constants.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in constants.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-21 16:13:36 +02:00
Jan Ciolek
ad3d2ee47d cql3: expr: Add constant to expression
Adds constant to the expression variant:
struct constant {
    raw_value value;
    data_type type;
};

This struct will be used to represent constant values with known bytes and type.
This corresponds to the terminal from current design.

bool is removed from expression, now constant is used instead.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-21 16:05:09 +02:00
Avi Kivity
8c0f2f9e3d Revert "Merge 'cql3: Add expr::constant to replace terminal' from Jan Ciołek"
This reverts commit e9343fd382, reversing
changes made to 27138b215b. It causes a
regression in v2 serialization_format support:

collection_serialization_with_protocol_v2_test fails with: marshaling error: read_simple_bytes - not enough bytes (requested 1627390306, got 3)

Fixes #9360
2021-09-20 15:15:09 +03:00
Jan Ciolek
319b6608b0 cql3: maps: Implement get_value_type in maps.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in mapshh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-13 17:03:23 +02:00
Jan Ciolek
5a755cda2b cql3: sets: Implement get_value_type in sets.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in sets.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-13 17:03:23 +02:00
Jan Ciolek
0b3436598a cql3: lists: Implement get_value_type in lists.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in lists.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-13 17:03:23 +02:00
Jan Ciolek
60a34236ee cql3: constants: Implement get_value_type in constants.hh
To convert a terminal to expr::constant we need know the value type.
Implement getting value type for terminals in constants.hh.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-13 17:03:23 +02:00
Jan Ciolek
79cb268ada cql3: expr: Add constant to expression
Adds constant to the expression variant:
struct constant {
    raw_value value;
    data_type type;
};

This struct will be used to represent constant values with known bytes and type.
This corresponds to the terminal from current design.

bool is removed from expression, now constant is used instead.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-09-13 17:03:21 +02:00
Avi Kivity
7a798b44a2 cql3: expr: replace column_value_tuple by a composition of tuple_constructor and column_value
column_value_tuple overlaps both column_value and tuple_constructor
(in different respects) and can be replaced by a combination: a
tuple_constructor of column_value. The replacement is more expressive
(we can have a tuple of column_value and other expression types), though
the code (especially grammar) do not allow it yet.

So remove column_value_tuple and replace it everywhere with
tuple_constructor. Visitors get the merged behavior of the existing
tuple_constructor and column_value_tuple, which is usually trivial
since tuple_constructor and column_value_tuple came from different
hierarchies (term::raw and relation), so usually one of the types
just calls on_internal_error().

The change results in awkwards casts in two areas: WHERE clause
filtering (equal() and related), and clustering key range evaluations
(limits() and related). When equal() is replaced by recursive
evaluate(), the casts will go way (to be replaced by the evaluate())
visitor. Clustering key range extraction will remain limited
to tuples of column_value, so the prepare phase will have to vet
the expressions to ensure the casts don't fail (and use the
filtering path if they will).

Tests: unit (dev)

Closes #9274
2021-09-10 10:43:29 +02:00
Avi Kivity
2da7b79e16 cql3: expr: eliminate column_specification_or_tuple
column_specification_or_tuple is now used internally, wrapping and
a column_specification or a vector and immediately unwrapping in
the callee. The only exceptions are bind_variable and tuple_constructor,
which handles both cases.

Use the underlying types directly instead, and add dispatching
to prepare_term_multi_column() for the two cases it handles.
2021-08-26 16:30:47 +03:00
Avi Kivity
ad285c3c84 cql3: expr: hide column_specification_or_tuple
column_specification_or_tuple was introduced since some terms
were prepared using a single receiver e.g. (receiver = <term>) and
some using multiple receivers (e.g. (r1, r2) = <term>. Some
term types supported both.

To hide this complexity, the term->expr conversion used a single
interface for both variations (column_expression_or_tuple), but now
that we got rid of the term class and there are no virtual functions
any more, we can just use two separate functions for the two variants.

Internally we still use column_expression_or_tuple, it can be
removed later.
2021-08-26 16:17:49 +03:00
Avi Kivity
158822c1a6 cql3: term::raw: remove term::raw and scaffolding
Nothing now uses term::raw, remove it and the scaffolding used to
migrate it to expressions.
2021-08-26 16:14:47 +03:00
Avi Kivity
8cdb6a102f cql3: expr: don't convert subexpressions to term::raw during the prepare phase
Now that we have the prepare machinery exposed as expression API:s
(not just term::raw) we can avoid conversions from expressions to
term::raw when preparing subexpressions.
2021-08-26 15:34:01 +03:00
Avi Kivity
3c6914c5bf cql3: expr: introduce test_assignment_all()
The test_assignment class has a test_all() helper to test
a vector of assignment_testable. But expressions are not
derived from assignment_testable, so introduce a new helper
that does the same for expressions.
2021-08-26 15:30:46 +03:00
Avi Kivity
55fd8e69ec cql3: expr: expose prepare_term, test_assignment in the expression domain
So far prepare (in the term domain) was called via term::raw. To be
able to prepare in the expression domain, expose functions prepare_term()
and test_assignment() that accept expressions as arguments.

prepare_term() was chosen rather that prepare() to differentiate wrt.
the other domain that can be prepared (selectables).
2021-08-26 15:29:10 +03:00
Avi Kivity
be335f4dee cql3: expr: provide a bridge between expressions and assignment_testable
While we have a bridge between expressions and term::raw, which is
derived from assignment_testable, we will soon get rid of term::raw
and so won't be able to interface with API:s that require an
assignment_testable. So add a bridge for that. The user is
function::get(), which uses assignment_testable to infer the
function overload from the argument types.
2021-08-26 15:26:38 +03:00
Avi Kivity
562e68835b cql3: expr, user types: convert user type literals to expressions
Convert the user_types::literal raw to a new expression type
usertype_constructor. I used "usertype" to convey that is is a
((user type) constructor), not a (user (type constructor)).
2021-08-26 15:26:35 +03:00
Avi Kivity
9d6bc7eae6 cql3: sets, user types: move user types raw functions around
Move them closer to prepare related functions for modification.
2021-08-26 15:15:59 +03:00
Avi Kivity
06bca067f8 cql3: expr, sets, maps: convert set and map literals to collection_constructor
Add set and map styles to collection_constructor. Maps are implemented as
collection_constructor{tuple_constructor{key, value}...}. This saves
having a new expression type, and reduces the effort to implement
recursive descent evaluation for this omitted expression type.
2021-08-26 15:13:37 +03:00
Avi Kivity
658cd47d21 cql3: sets, maps, expr: move set and map raw functions around
Move them closer to prepare related functions for modification. Since
sets and maps share some implementation details in the grammar, they
are moved and converted as a unit.
2021-08-26 15:13:07 +03:00
Avi Kivity
d2ab7fc26d cql3: expr, lists: convert lists::literal to new collection_constructor
Introduce a collection_constructor (similar to C++'s std::initializer_list)
to hold subexpressions being gathered into a list. Since sets, maps, and
lists construction share some attributes (all elements must be of the
same type) collection_constructor will be used for all of them, so it
also holds an enum. I used "style" for the enum since it's a weak
attribute - an empty set is also an empty map. I chose collection_constructor
rather than plain 'collection' to highlight that it's not the only way
to get a collection (selecting a collection column is another, as an
example) and to hint at what it does - construct a collection from
more primitive elements.
2021-08-26 15:10:41 +03:00
Avi Kivity
4defb42c86 cql3: lists, expr: move list raw functions around
Move them closer to prepare related functions for modification.
2021-08-26 15:08:14 +03:00
Avi Kivity
5e448e4a2a cql3: tuples, expr: convert tuples::literal to expr::tuple_constructor
Introduce tuple_constructor (not a literal, since (?, ?) and (column_value,
column_value) are not literals) to represent a tuple constructed from
subexpressions. In the future we can replace column_value_tuple
with tuple_constructor(column_value, column_value, ...), but this is
not done now.

I chose the name 'tuple_constructor' since other expressions can represent
tuples (e.g. my_tuple_column, :bind_variable_of_tuple_type,
func_returning_tuple()). It also explains what the expression does.
2021-08-26 15:07:15 +03:00
Avi Kivity
41c532f19c cql3: expr, tuples: deinline and move tuple raw functions
Move them closer to prepare functions for modification.
2021-08-26 15:04:21 +03:00
Avi Kivity
2c42a65db1 cql3: expr, constants: convert constants::literal to untyped_constant
Introduce a new expression untyped_constant that corresponds to
constants::literal, which is removed. untyped_constant is rather
ugly in that it won't exist post-prepare. We should probably instead
replace it with typed constants that use the widest possible type
(decimal and varint), and select a narrower type during the prepare
phase when we perform type inference. The conversion itseld is
straightforward.
2021-08-26 15:03:07 +03:00
Avi Kivity
4d9bde561a cql3: constants: move constants::literal implementation around
Move it closer to prepare functions for modification.
2021-08-26 15:01:06 +03:00
Avi Kivity
838bfbd3e0 cql3: expr, abstract_marker: convert to expressions
Convert the four forms of abstract_marker to expr::bind_variable (the
name was chosen since variable is the role of the thing, while "marker"
refers more to the grammar). Having four variants is unnecessary, but
this patch doesn't do anything about that.
2021-08-26 15:01:04 +03:00
Avi Kivity
6dcc43d227 cql3: tuple markers: deinline and rearrange
Move raw methods near to the other prepare-related functions.
2021-08-26 14:54:15 +03:00
Avi Kivity
35db2b34e4 cql3: abstract_marker, term_expr: rearrange raw abstract marker implementation
Move raw methods near to the other prepare-related functions.
2021-08-26 14:53:58 +03:00
Avi Kivity
aba205917d cql3: expr, constants: convert cql3::constants::null_literal to new cql3::expr::null
Introduce cql3::expr::null and use it to represent null_literal, which is
removed.
2021-08-26 14:49:46 +03:00
Avi Kivity
5b42cbf9e0 cql3: expr, constants: deinline null_literal
Deinline null_literal methods and place them near the other prepare-related
functions.
2021-08-26 14:45:56 +03:00
Avi Kivity
10e08dc87e cql3: term::raw, expr: convert type casts to expressions
We reuse the expr::cast type that was previously used for selectables.
When preparing, subexpressions are converted to term::raw; this will
be removed later.
2021-08-26 14:42:55 +03:00
Avi Kivity
6f8b6aef17 cql3: type_cast: deinline some methods
These methods will be converted to the expression variant, and
it's impossible to do this while inlined due to #include cycles. In
any case, deinlining is better.

Since there is no type_cast.cc, and since they'll become part of
expr_term call chain soon, they're moved there, even though it seems
odd for this patch. It's a waste to create type_cast.cc just for those
three functions.
2021-08-26 14:41:38 +03:00
Avi Kivity
b76395a410 cql3: expr, functions: move raw function calls to expressions
Remove cql3::functions::function_call::raw and replace it with
cql3::expr::function_call, which already existed from the selector
migration to expressions. The virtual functions implementing term::raw
are made free functions and remain in place, to ease migration and
review.

Note that preparing becomes a more complicated as it needs to
account for anonymous functions, which were not representable
in the previous structure (and still cannot be created by the
parser for the term::raw path).

The parser now wraps all its arguments with the term::raw->expr
bridge, since that's what expr::function_call expects, and in
turn wraps the function call with an expr->term::raw bridge, since
that's what the rest of the parser expects. These will disappear
when the migration completes.
2021-08-26 14:38:16 +03:00
Avi Kivity
a5031dd5bf cql3: expr, term::raw: add reverse bridge
Since expressions can nest, and since we won't covert everything at once,
add a way to store a term::raw as an expression. We can now have a
term::raw that is internally an expression, and an expression that is
implemented as term::raw.
2021-08-26 14:32:04 +03:00
Avi Kivity
725065b066 cql3: term::raw, expr: add bridge between term::raw and expressions
A term_raw_expression is a term::raw that holds an expression. It will
be used to incrementally convert the source base to expressions, while
still exposing the result to the common interface of shared_ptr<term::raw>.
2021-08-26 14:14:18 +03:00