forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuples.cc
167 lines (149 loc) · 6.61 KB
/
tuples.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2015 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include <seastar/core/shared_ptr.hh>
#include "tuples.hh"
#include "types/list.hh"
namespace cql3 {
shared_ptr<column_specification>
tuples::component_spec_of(shared_ptr<column_specification> column, size_t component) {
return ::make_shared<column_specification>(
column->ks_name,
column->cf_name,
::make_shared<column_identifier>(format("{}[{:d}]", column->name, component), true),
static_pointer_cast<const tuple_type_impl>(column->type)->type(component));
}
shared_ptr<term>
tuples::literal::prepare(database& db, const sstring& keyspace, shared_ptr<column_specification> receiver) {
validate_assignable_to(db, keyspace, receiver);
std::vector<shared_ptr<term>> values;
bool all_terminal = true;
for (size_t i = 0; i < _elements.size(); ++i) {
auto&& value = _elements[i]->prepare(db, keyspace, component_spec_of(receiver, i));
if (dynamic_pointer_cast<non_terminal>(value)) {
all_terminal = false;
}
values.push_back(std::move(value));
}
delayed_value value(static_pointer_cast<const tuple_type_impl>(receiver->type), values);
if (all_terminal) {
return value.bind(query_options::DEFAULT);
} else {
return make_shared(std::move(value));
}
}
shared_ptr<term>
tuples::literal::prepare(database& db, const sstring& keyspace, const std::vector<shared_ptr<column_specification>>& receivers) {
if (_elements.size() != receivers.size()) {
throw exceptions::invalid_request_exception(format("Expected {:d} elements in value tuple, but got {:d}: {}", receivers.size(), _elements.size(), *this));
}
std::vector<shared_ptr<term>> values;
std::vector<data_type> types;
bool all_terminal = true;
for (size_t i = 0; i < _elements.size(); ++i) {
auto&& t = _elements[i]->prepare(db, keyspace, receivers[i]);
if (dynamic_pointer_cast<non_terminal>(t)) {
all_terminal = false;
}
values.push_back(t);
types.push_back(receivers[i]->type);
}
delayed_value value(tuple_type_impl::get_instance(std::move(types)), std::move(values));
if (all_terminal) {
return value.bind(query_options::DEFAULT);
} else {
return make_shared(std::move(value));
}
}
tuples::in_value
tuples::in_value::from_serialized(const fragmented_temporary_buffer::view& value_view, list_type type, const query_options& options) {
try {
// Collections have this small hack that validate cannot be called on a serialized object,
// but the deserialization does the validation (so we're fine).
return with_linearized(value_view, [&] (bytes_view value) {
auto l = value_cast<list_type_impl::native_type>(type->deserialize(value, options.get_cql_serialization_format()));
auto ttype = dynamic_pointer_cast<const tuple_type_impl>(type->get_elements_type());
assert(ttype);
std::vector<std::vector<bytes_opt>> elements;
elements.reserve(l.size());
for (auto&& element : l) {
auto tuple_buff = ttype->decompose(element);
auto tuple = ttype->split(tuple_buff);
std::vector<bytes_opt> elems;
elems.reserve(tuple.size());
for (auto&& e : tuple) {
elems.emplace_back(to_bytes_opt(e));
}
elements.emplace_back(std::move(elems));
}
return tuples::in_value(elements);
});
} catch (marshal_exception& e) {
throw exceptions::invalid_request_exception(e.what());
}
}
::shared_ptr<column_specification>
tuples::in_raw::make_in_receiver(const std::vector<shared_ptr<column_specification>>& receivers) {
std::vector<data_type> types;
types.reserve(receivers.size());
sstring in_name = "in(";
for (auto&& receiver : receivers) {
in_name += receiver->name->text();
if (receiver != receivers.back()) {
in_name += ",";
}
if (receiver->type->is_collection() && receiver->type->is_multi_cell()) {
throw exceptions::invalid_request_exception("Non-frozen collection columns do not support IN relations");
}
types.emplace_back(receiver->type);
}
in_name += ")";
auto identifier = make_shared<column_identifier>(in_name, true);
auto type = tuple_type_impl::get_instance(types);
return make_shared<column_specification>(receivers.front()->ks_name, receivers.front()->cf_name, identifier, list_type_impl::get_instance(type, false));
}
tuples::in_marker::in_marker(int32_t bind_index, ::shared_ptr<column_specification> receiver)
: abstract_marker(bind_index, std::move(receiver))
{
assert(dynamic_pointer_cast<const list_type_impl>(_receiver->type));
}
shared_ptr<terminal> tuples::in_marker::bind(const query_options& options) {
const auto& value = options.get_value_at(_bind_index);
if (value.is_null()) {
return nullptr;
} else if (value.is_unset_value()) {
throw exceptions::invalid_request_exception(format("Invalid unset value for tuple {}", _receiver->name->text()));
} else {
auto as_list_type = static_pointer_cast<const list_type_impl>(_receiver->type);
auto as_tuple_type = static_pointer_cast<const tuple_type_impl>(as_list_type->get_elements_type());
try {
with_linearized(*value, [&] (bytes_view v) {
as_list_type->validate(v, options.get_cql_serialization_format());
auto l = value_cast<list_type_impl::native_type>(as_list_type->deserialize(v, options.get_cql_serialization_format()));
for (auto&& element : l) {
as_tuple_type->validate(as_tuple_type->decompose(element), options.get_cql_serialization_format());
}
});
} catch (marshal_exception& e) {
throw exceptions::invalid_request_exception(e.what());
}
return make_shared(tuples::in_value::from_serialized(*value, as_list_type, options));
}
}
}