-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
create_index_statement.cc
192 lines (166 loc) · 8.13 KB
/
create_index_statement.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2015 Cloudius Systems
*
* Modified by Cloudius Systems
*/
#include "create_index_statement.hh"
#include "validation.hh"
#include "service/storage_proxy.hh"
#include "service/migration_manager.hh"
#include "schema.hh"
#include "schema_builder.hh"
cql3::statements::create_index_statement::create_index_statement(
::shared_ptr<cf_name> name, sstring index_name,
::shared_ptr<index_target::raw> raw_target,
::shared_ptr<index_prop_defs> properties, bool if_not_exists)
: schema_altering_statement(name), _index_name(index_name), _raw_target(
raw_target), _properties(properties), _if_not_exists(
if_not_exists) {
}
void
cql3::statements::create_index_statement::check_access(const service::client_state& state) {
warn(unimplemented::cause::MIGRATIONS);
// TODO
//state.hasColumnFamilyAccess(keyspace(), columnFamily(), Permission.ALTER);
}
void
cql3::statements::create_index_statement::validate(distributed<service::storage_proxy>& proxy
, const service::client_state& state)
{
auto schema = validation::validate_column_family(proxy.local().get_db().local(), keyspace(), column_family());
if (schema->is_counter()) {
throw exceptions::invalid_request_exception("Secondary indexes are not supported on counter tables");
}
// Added since we might as well fail fast if anyone is trying to insert java classes here...
if (_properties->is_custom) {
throw exceptions::invalid_request_exception("CUSTOM index not supported");
}
auto target = _raw_target->prepare(schema);
auto cd = schema->get_column_definition(target->column->name());
if (cd == nullptr) {
throw exceptions::invalid_request_exception(sprint("No column definition found for column %s", target->column->name()));
}
bool is_map = dynamic_cast<const collection_type_impl *>(cd->type.get()) != nullptr
&& dynamic_cast<const collection_type_impl *>(cd->type.get())->is_map();
bool is_frozen_collection = cd->type->is_collection() && !cd->type->is_multi_cell();
if (is_frozen_collection) {
if (target->type != index_target::target_type::full) {
throw exceptions::invalid_request_exception(
sprint("Cannot create index on %s of frozen<map> column %s",
index_target::index_option(target->type),
target->column->name()));
}
} else {
// validateNotFullIndex
if (target->type != index_target::target_type::full) {
throw exceptions::invalid_request_exception("full() indexes can only be created on frozen collections");
}
// validateIsValuesIndexIfTargetColumnNotCollection
if (!cd->type->is_collection()
&& target->type != index_target::target_type::values) {
throw exceptions::invalid_request_exception(
sprint(
"Cannot create index on %s of column %s; only non-frozen collections support %s indexes",
index_target::index_option(target->type),
target->column->name(),
index_target::index_option(target->type)));
}
// validateTargetColumnIsMapIfIndexInvolvesKeys
if (target->type == index_target::target_type::keys
|| target->type == index_target::target_type::keys_and_values) {
if (!is_map) {
throw exceptions::invalid_request_exception(
sprint(
"Cannot create index on %s of column %s with non-map type",
index_target::index_option(target->type),
target->column->name()));
}
}
}
if (cd->idx_info.index_type != ::index_type::none) {
auto prev_type = index_target::from_column_definition(*cd);
if (is_map && target->type != prev_type) {
throw exceptions::invalid_request_exception(
sprint(
"Cannot create index on %s(%s): an index on %s(%s) already exists and indexing "
"a map on more than one dimension at the same time is not currently supported",
index_target::index_option(target->type),
target->column->name(),
index_target::index_option(prev_type),
target->column->name()));
}
if (_if_not_exists) {
return;
} else {
throw exceptions::invalid_request_exception("Index already exists");
}
}
_properties->validate();
// Origin TODO: we could lift that limitation
if ((schema->is_dense() || !schema->thrift().has_compound_comparator()) && cd->kind != column_kind::regular_column) {
throw exceptions::invalid_request_exception("Secondary indexes are not supported on PRIMARY KEY columns in COMPACT STORAGE tables");
}
// It would be possible to support 2ndary index on static columns (but not without modifications of at least ExtendedFilter and
// CompositesIndex) and maybe we should, but that means a query like:
// SELECT * FROM foo WHERE static_column = 'bar'
// would pull the full partition every time the static column of partition is 'bar', which sounds like offering a
// fair potential for foot-shooting, so I prefer leaving that to a follow up ticket once we have identified cases where
// such indexing is actually useful.
if (cd->is_static()) {
throw exceptions::invalid_request_exception("Secondary indexes are not allowed on static columns");
}
if (cd->kind == column_kind::partition_key && cd->is_on_all_components()) {
throw exceptions::invalid_request_exception(
sprint(
"Cannot create secondary index on partition key column %s",
target->column->name()));
}
}
future<bool>
cql3::statements::create_index_statement::announce_migration(distributed<service::storage_proxy>& proxy, bool is_local_only) {
auto schema = proxy.local().get_db().local().find_schema(keyspace(), column_family());
auto target = _raw_target->prepare(schema);
schema_builder cfm(schema);
auto* cd = schema->get_column_definition(target->column->name());
index_info idx = cd->idx_info;
if (idx.index_type != ::index_type::none && _if_not_exists) {
return make_ready_future<bool>(false);
}
if (_properties->is_custom) {
idx.index_type = index_type::custom;
idx.index_options = _properties->get_options();
} else if (schema->thrift().has_compound_comparator()) {
index_options_map options;
if (cd->type->is_collection() && cd->type->is_multi_cell()) {
options[index_target::index_option(target->type)] = "";
}
idx.index_type = index_type::composites;
idx.index_options = options;
} else {
idx.index_type = index_type::keys;
idx.index_options = index_options_map();
}
idx.index_name = _index_name;
cfm.add_default_index_names(proxy.local().get_db().local());
return service::migration_manager::announce_column_family_update(proxy,
cfm.build(), false, is_local_only).then([]() {
return make_ready_future<bool>(true);
});
}