Commit Graph

88 Commits

Author SHA1 Message Date
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
Nadav Har'El
9666921dbc Merge 'cql3: expr: introduce search_and_replace()' from Avi Kivity
Introduce a general-purpose search and replace function to manipulate
expressions, and use it to simplify replace_column_def() and
replace_token().

Closes #9259

* github.com:scylladb/scylla:
  cql3: expr: rewrite replace_token in terms of search_and_replace()
  cql3: expr: rewrite replace_column_def in terms of search_and_replace()
  cql3: expr: add general-purpose search-and-replace
2021-08-31 15:56:41 +03:00
Avi Kivity
542a8bc0f3 cql3: expr: rewrite replace_token in terms of search_and_replace()
Use search_and_replace() to simplify replace_token(). Note the conversion
does not have 100% fidelity - the previous implementation throws on
some impossible subexpression types, and the new one passes them
through. It should be the caller's responsibility anyway, not a
side effect of replacing tokens, and since these subexpressions are
impossible there is no real effect on execution.

Note that this affects only TOKEN() calls on the partition key
columns in the right order. Other uses of the token function
(say with constants) won't be translated to the token subexpression
type. So something like

    WHERE token(pk) = token(?)

would only see the left-hand side replaced, not the right-hand
side, even if it were an expression rather than a term.
2021-08-31 12:29:47 +03:00
Avi Kivity
10ca63128a cql3: expr: rewrite replace_column_def in terms of search_and_replace()
We're won't introduce new expression types that are equivalent
to column_value, and search_and_replace() takes care of all
expressions that need to recurse, so we don't need std::visit()
for the search/replace lambda.
2021-08-31 12:29:47 +03:00
Avi Kivity
7a594bc42f cql3: expr: add general-purpose search-and-replace
Add a recursive search-and-replace function on expressions. The
caller provides a search/replace function to operate on subexpressions,
returning nullopt if they want the default behavior of recursively
copying, or a new expression to terminate the search (in the current
subtree) and replace the current node with the returned expression.

To avoid endlessly specifying the subexpression types that get the
the common behavior (copying) since they don't contain any subexpressions,
we add a new concept LeafExpression to signify them.

Existing functions such as replace_token() can be reimplemented in
terms of search_and_replace, but that is left for later.
2021-08-31 12:29:37 +03:00
Dejan Mircevski
81f00d82cf cql3: Drop more dead code
This is some dead code that 44ca965ba missed.

Tests: unit (dev)

Signed-off-by: Dejan Mircevski <dejan@scylladb.com>

Closes #9267
2021-08-31 12:06:19 +03: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
3d30c161e4 cql3: expr: prepare expr::cast for unprepared types
The cast expression has two operands: the subexpression to cast and the
type to cast to. Since prepared and unprepared expressions are the
same type, we don't have to do anything, but prepared and unprepared
types are different. So add a variant to be able to support both.

The reason the selectable->expression transformation did not need to
do this is that casts in a selector cannot accept a user defined type.
Note those casts also have different syntax and different execution,
so we'll have to choose whether to unify the two semantics, or whether
to keep them separate. This patch does not force anything (but does hint
at unification by not including any discriminant beyond the type's
rawness). The string representation matches the part of the grammar
it was derived from (or conversion back to CQL will yield wrong
results).
2021-08-26 14:39:33 +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
0d24af7775 cql3: expr, term::raw: add conversions between the two types
Add a way to convert between the old world and the new, and back. Note
that instead of blindly wrapping, we unwrap if we received a wrapped
object.
2021-08-26 14:35:46 +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
Jan Ciolek
a7d1dab066 statement_restrictions_test: tests for extracting column restrictions
Add unit tests for the function
extract_single_column_restrictions_for_column()

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-08-02 15:43:42 +02:00
Jan Ciolek
43ab3d6831 expression: add a function to extract restrictions for a column
Add a function, which given an expression and a column,
extracts all restrictions involving this column.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-08-02 15:43:33 +02:00
Avi Kivity
a38b1006d1 cql3: expression: update find_atom, count_if for function_call, cast, field_selection
The combination of the new types and these functions cannot happen yet,
but as they are generic functions it is better to implement them in
case it becomes possible later.
2021-07-27 20:16:43 +03:00
Avi Kivity
2b7b9bb469 cql3: expressions: fix printing of nested expressions
Now that we eliminated cql3::selectable::raw, we can print
nested expressions.
2021-07-27 20:16:29 +03:00
Avi Kivity
98c4f0dfb3 cql3: selection: replace selectable::raw with expression
Now that all selectable::raw subclasses have been converted to
cql3::selectable::with_expression::raw, the class structure is
just a wrapper around expressions. Peel it, converting the
virtual member functions to free functions, and replacing
object instances with expression or nested_expression as the
case allows.
2021-07-27 20:16:15 +03:00
Avi Kivity
979010a1e5 cql3: expression: convert selectable::with_field_selection::raw to expression
Add a field_selection variant element to expression. Like function_call
and cast, the structure from which a field is selectewd cannot yet be
an expression, since not all seletable::raw:s are converted. This will
be done in a later pass. This is also why printing a field selection now
does not print the selected expression; this will also be corrected later.
2021-07-27 20:16:12 +03:00
Avi Kivity
714b812212 cql3: expression: convert selectable::with_cast::raw to expression
Add a cast variant element to expression. Like function_call, the
argument being converted cannot yet be an expression, since not
all seletable::raw:s are converted. This will be done in a later
pass.  This is also why printing a cast now does not print the
casted expression; this will also be corrected later.
2021-07-27 20:14:52 +03:00
Avi Kivity
5adae5837e cql3: expression: convert selectable::with_anonymous_function::raw to expression
Rather than creating a new variant element in expression, we extend
function_call to handle both named and anonymous functions, since
most of the processing is the same.
2021-07-27 20:13:55 +03:00
Avi Kivity
3e392d2513 cql3: expression: convert selectable::with_function_call::raw to expressions
Add a function_call variant element to hold function calls. Note
that because not all selectables are yet converted, function call
arguments are still of type selectable::raw. They will be converted
to expressions later. This is also why printing a function now
does not print its arguments; this will also be corrected later.
2021-07-27 20:13:51 +03:00
Avi Kivity
ff65c54316 cql3: expressions: convert writetime_or_ttl::raw to expression
Create a new element in the expression variant, column_mutation_attribute,
signifying we're picking up an attribute of a column mutation (not a
column value!). We use an enum rather than a bool to choose between
writetime and ttl (the two mutation attributes) for increased
explicitness.

Although there can only be one type for the column we're operating
on (it must be an unresolved_identifer), we use a nested_expression.
This is because we'll later need to also support a column_value
as the column type after we prepare it. This is somewhat similar
to the address of operator in C, which syntactically takes any
expression but semantically operates only on lvalues.
2021-07-27 20:10:52 +03:00
Avi Kivity
294f0f35b1 cql3: expression: add convenience constructor from expression element to nested expression
It is convenient to initialize a nested_expression variable from
one of the types that compose the expression variant, but C++ doesn't
allow it. Add a constructor that does this. Use the new variant_element
concept to constrain the input to be one of the variant's elements.
2021-07-27 20:08:48 +03:00
Avi Kivity
ac3b093e3c cql3: expression: use nested_expression in binary_operator
binary_operator::lhs is implementing the pattern in nested_expression.
Use nested_expression instead to reduce code size.
2021-07-27 20:08:34 +03:00
Avi Kivity
b07a0867b3 cql3: expression: introduce nested_expression class
The exression type cannot be a member of a struct that is an
element of the expression variant. This is because it would then
be required to contain itself. So introduce a nested_expression
type to indirectly hold an expression, but keep the value semantics
we expect from expressions: it is copyable and a copy has separate
identity and storage.

In fact binary_operator had to resort to this trick, so it's converted
to nested_expression in the next patch.
2021-07-27 20:08:21 +03:00
Avi Kivity
8a518e9c78 Convert column_identifier_raw's use as selectable to expressions
Introduce unresolved_identifer as an unprepared counterpart to column_value.
column_identifier_raw no longer inherits from selectable::raw, but
methods for now to reduce churn.
2021-07-27 20:08:15 +03:00
Jan Ciolek
51ee9adeec expression: Add replace_token function
Adds replace_token function which takes an expression
and replaces all left hand side occurences of token()
with the given column definition.

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2021-07-21 12:25:12 +02:00