`duration` is a new native type that was introduced in Cassandra 3.10 [1]. Support for parsing and the internal representation of the type was added in8fa47b74e8. Important note: The version of cqlsh distributed with Scylla does not have support for durations included (it was added to Cassandra in [2]). To test this change, you can use cqlsh distributed with Cassandra. Duration types are useful when working with time-series tables, because they can be used to manipulate date-time values in relative terms. Two interesting applications are: - Aggregation by time intervals [3]: `SELECT * FROM my_table GROUP BY floor(time, 3h)` - Querying on changes in date-times: `SELECT ... WHERE last_heartbeat_time < now() - 3h` (Note: neither of these is currently supported, though columns with duration values are.) Internally, durations are represented as three signed counters: one for months, for days, and for nanoseconds. Each of these counters is serialized using a variable-length encoding which is described in version 5 of the CQL native protocol specification. The representation of a duration as three counters means that a semantic ordering on durations doesn't exist: Is `1mo` greater than `1mo1d`? We cannot know, because some months have more days than others. Durations can only have a concrete absolute value when they are "attached" to absolute date-time references. For example, `2015-04-31 at 12:00:00 + 1mo`. That duration values are not comparable presents some difficulties for the implementation, because most CQL types are. Like in Cassandra's implementation [2], I adopted a similar strategy to the way restrictions on the `counter` type are checked. A type "references" a duration if it is either a duration or it contains a duration (like a `tuple<..., duration, ...>`, or a UDT with a duration member). The following restrictions apply on durations. Note that some of these contexts are either experimental features (materialized views), or not currently supported at run-time (though support exists in the parser and code, so it is prudent to add the restrictions now): - Durations cannot appear in any part of a primary key, either for tables or materialized views. - Durations cannot be directly used as the element type of a `set`, nor can they be used as the key type of a `map`. Because internal ordering on durations is based on a byte-level comparison, this property of Cassandra was intended to help avoid user confusion around ordering of collection elements. - Secondary indexes on durations are not supported. - "Slice" relations (<=, <, >=, >) are not supported on durations with `WHERE` restrictions (like `SELECT ... WHERE span <= 3d`). Multi-column restrictions only work with clustering columns, which cannot be `duration` due to the first rule. - "Slice" relations are not supported on durations with query conditions (like `UPDATE my_table ... IF span > 5us`). Backwards incompatibility note: As described in the documentation [4], duration literals take one of two forms: either ISO 8601 formats (there are three), or a "standard" format. The ISO 8601 formats start with "P" (like "P5W"). Therefore, identifiers that have this form are no longer supported. Fixes #2240. [1] https://issues.apache.org/jira/browse/CASSANDRA-11873 [2]bfd57d13b7[3] https://issues.apache.org/jira/browse/CASSANDRA-11871 [4] http://cassandra.apache.org/doc/latest/cql/types.html#working-with-durations
161 lines
6.4 KiB
C++
161 lines
6.4 KiB
C++
/*
|
|
* 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 (C) 2015 ScyllaDB
|
|
*
|
|
* Modified by 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 "cql3/column_condition.hh"
|
|
#include "statements/request_validations.hh"
|
|
#include "unimplemented.hh"
|
|
#include "lists.hh"
|
|
#include "maps.hh"
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
|
|
|
namespace {
|
|
|
|
void validate_operation_on_durations(const abstract_type& type, const cql3::operator_type& op) {
|
|
using cql3::statements::request_validations::check_false;
|
|
|
|
if (op.is_slice() && type.references_duration()) {
|
|
check_false(type.is_collection(), "Slice conditions are not supported on collections containing durations");
|
|
check_false(type.is_tuple(), "Slice conditions are not supported on tuples containing durations");
|
|
check_false(type.is_user_type(), "Slice conditions are not supported on UDTs containing durations");
|
|
|
|
// We're a duration.
|
|
throw exceptions::invalid_request_exception(sprint("Slice conditions are not supported on durations"));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
namespace cql3 {
|
|
|
|
bool
|
|
column_condition::uses_function(const sstring& ks_name, const sstring& function_name) {
|
|
if (bool(_collection_element) && _collection_element->uses_function(ks_name, function_name)) {
|
|
return true;
|
|
}
|
|
if (bool(_value) && _value->uses_function(ks_name, function_name)) {
|
|
return true;
|
|
}
|
|
if (!_in_values.empty()) {
|
|
for (auto&& value : _in_values) {
|
|
if (bool(value) && value->uses_function(ks_name, function_name)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void column_condition::collect_marker_specificaton(::shared_ptr<variable_specifications> bound_names) {
|
|
if (_collection_element) {
|
|
_collection_element->collect_marker_specification(bound_names);
|
|
}
|
|
if (!_in_values.empty()) {
|
|
for (auto&& value : _in_values) {
|
|
value->collect_marker_specification(bound_names);
|
|
}
|
|
}
|
|
_value->collect_marker_specification(bound_names);
|
|
}
|
|
|
|
::shared_ptr<column_condition>
|
|
column_condition::raw::prepare(database& db, const sstring& keyspace, const column_definition& receiver) {
|
|
if (receiver.type->is_counter()) {
|
|
throw exceptions::invalid_request_exception("Conditions on counters are not supported");
|
|
}
|
|
|
|
if (!_collection_element) {
|
|
if (_op == operator_type::IN) {
|
|
if (_in_values.empty()) { // ?
|
|
return column_condition::in_condition(receiver, _in_marker->prepare(db, keyspace, receiver.column_specification));
|
|
}
|
|
|
|
std::vector<::shared_ptr<term>> terms;
|
|
for (auto&& value : _in_values) {
|
|
terms.push_back(value->prepare(db, keyspace, receiver.column_specification));
|
|
}
|
|
return column_condition::in_condition(receiver, std::move(terms));
|
|
} else {
|
|
validate_operation_on_durations(*receiver.type, _op);
|
|
return column_condition::condition(receiver, _value->prepare(db, keyspace, receiver.column_specification), _op);
|
|
}
|
|
}
|
|
|
|
if (!receiver.type->is_collection()) {
|
|
throw exceptions::invalid_request_exception(sprint("Invalid element access syntax for non-collection column %s", receiver.name_as_text()));
|
|
}
|
|
|
|
shared_ptr<column_specification> element_spec, value_spec;
|
|
auto ctype = static_cast<const collection_type_impl*>(receiver.type.get());
|
|
if (&ctype->_kind == &collection_type_impl::kind::list) {
|
|
element_spec = lists::index_spec_of(receiver.column_specification);
|
|
value_spec = lists::value_spec_of(receiver.column_specification);
|
|
} else if (&ctype->_kind == &collection_type_impl::kind::map) {
|
|
element_spec = maps::key_spec_of(*receiver.column_specification);
|
|
value_spec = maps::value_spec_of(*receiver.column_specification);
|
|
} else if (&ctype->_kind == &collection_type_impl::kind::set) {
|
|
throw exceptions::invalid_request_exception(sprint("Invalid element access syntax for set column %s", receiver.name()));
|
|
} else {
|
|
abort();
|
|
}
|
|
|
|
if (_op == operator_type::IN) {
|
|
if (_in_values.empty()) {
|
|
return column_condition::in_condition(receiver,
|
|
_collection_element->prepare(db, keyspace, element_spec),
|
|
_in_marker->prepare(db, keyspace, value_spec));
|
|
}
|
|
std::vector<shared_ptr<term>> terms;
|
|
terms.reserve(_in_values.size());
|
|
boost::push_back(terms, _in_values
|
|
| boost::adaptors::transformed(std::bind(&term::raw::prepare, std::placeholders::_1, std::ref(db), std::ref(keyspace), value_spec)));
|
|
return column_condition::in_condition(receiver, _collection_element->prepare(db, keyspace, element_spec), terms);
|
|
} else {
|
|
validate_operation_on_durations(*receiver.type, _op);
|
|
|
|
return column_condition::condition(receiver,
|
|
_collection_element->prepare(db, keyspace, element_spec),
|
|
_value->prepare(db, keyspace, value_spec),
|
|
_op);
|
|
}
|
|
}
|
|
|
|
}
|