Merge branch 'pdziepak/date-timestamp-fixes/v2'

From Paweł:

These patches contain fixes for date and timestamp types:
 - date and timestamp are considered compatible types
 - date type is added to abstract_type::parse_type()
This commit is contained in:
Tomasz Grabiec
2016-01-19 18:35:09 +01:00
3 changed files with 41 additions and 9 deletions

View File

@@ -483,7 +483,6 @@ deps = {
}
tests_not_using_seastar_test_framework = set([
'tests/types_test',
'tests/keys_test',
'tests/partitioner_test',
'tests/map_difference_test',

View File

@@ -19,10 +19,7 @@
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE core
#include <boost/test/unit_test.hpp>
#include <seastar/tests/test-utils.hh>
#include <utils/UUID_gen.hh>
#include <boost/asio/ip/address_v4.hpp>
#include <net/ip.hh>
@@ -654,7 +651,7 @@ BOOST_AUTO_TEST_CASE(test_collection_type_compatibility) {
}
}
BOOST_AUTO_TEST_CASE(test_simple_type_compatibility) {
SEASTAR_TEST_CASE(test_simple_type_compatibility) {
test_case tests[] = {
{ vc, bytes_type, int32_type },
{ nc, int32_type, bytes_type },
@@ -668,8 +665,11 @@ BOOST_AUTO_TEST_CASE(test_simple_type_compatibility) {
{ nc, utf8_type, bytes_type },
{ nc, ascii_type, bytes_type },
{ nc, ascii_type, utf8_type },
{ cc, timestamp_type, date_type },
{ cc, date_type, timestamp_type },
};
for (auto&& tc : tests) {
tc.verify(tc.to, tc.from);
}
return make_ready_future<>();
}

View File

@@ -48,6 +48,7 @@ static const char* bytes_type_name = "org.apache.cassandra.db.marshal.BytesT
static const char* boolean_type_name = "org.apache.cassandra.db.marshal.BooleanType";
static const char* timeuuid_type_name = "org.apache.cassandra.db.marshal.TimeUUIDType";
static const char* timestamp_type_name = "org.apache.cassandra.db.marshal.TimestampType";
static const char* date_type_name = "org.apache.cassandra.db.marshal.DateType";
static const char* uuid_type_name = "org.apache.cassandra.db.marshal.UUIDType";
static const char* inet_addr_type_name = "org.apache.cassandra.db.marshal.InetAddressType";
static const char* double_type_name = "org.apache.cassandra.db.marshal.DoubleType";
@@ -380,8 +381,10 @@ struct boolean_type_impl : public simple_type_impl<bool> {
}
};
struct date_type_impl : public concrete_type<db_clock::time_point> {
date_type_impl() : concrete_type("date") {}
class date_type_impl : public concrete_type<db_clock::time_point> {
static logging::logger _logger;
public:
date_type_impl() : concrete_type(date_type_name) {}
virtual void serialize(const void* value, bytes::iterator& out) const override {
if (!value) {
return;
@@ -426,7 +429,21 @@ struct date_type_impl : public concrete_type<db_clock::time_point> {
virtual bool is_value_compatible_with_internal(const abstract_type& other) const override {
return &other == this || &other == timestamp_type.get() || &other == long_type.get();
}
virtual bool is_compatible_with(const abstract_type& other) const override {
if (&other == this) {
return true;
}
if (&other == timestamp_type.get()) {
_logger.warn("Changing from TimestampType to DateType is allowed, but be wary that they sort differently for pre-unix-epoch timestamps "
"(negative timestamp values) and thus this change will corrupt your data if you have such negative timestamp. There is no "
"reason to switch from DateType to TimestampType except if you were using DateType in the first place and switched to "
"TimestampType by mistake.");
return true;
}
return false;
}
};
logging::logger date_type_impl::_logger(date_type_name);
struct timeuuid_type_impl : public concrete_type<utils::UUID> {
timeuuid_type_impl() : concrete_type<utils::UUID>(timeuuid_type_name) {}
@@ -532,7 +549,9 @@ private:
friend class uuid_type_impl;
};
struct timestamp_type_impl : simple_type_impl<db_clock::time_point> {
class timestamp_type_impl : public simple_type_impl<db_clock::time_point> {
static logging::logger _logger;
public:
timestamp_type_impl() : simple_type_impl(timestamp_type_name) {}
virtual void serialize(const void* value, bytes::iterator& out) const override {
if (!value) {
@@ -673,7 +692,20 @@ struct timestamp_type_impl : simple_type_impl<db_clock::time_point> {
virtual bool is_value_compatible_with_internal(const abstract_type& other) const override {
return &other == this || &other == date_type.get() || &other == long_type.get();
}
virtual bool is_compatible_with(const abstract_type& other) const override {
if (&other == this) {
return true;
}
if (&other == date_type.get()) {
_logger.warn("Changing from DateType to TimestampType is allowed, but be wary that they sort differently for pre-unix-epoch timestamps "
"(negative timestamp values) and thus this change will corrupt your data if you have such negative timestamp. So unless you "
"know that you don't have *any* pre-unix-epoch timestamp you should change back to DateType");
return true;
}
return false;
}
};
logging::logger timestamp_type_impl::_logger(timestamp_type_name);
struct uuid_type_impl : concrete_type<utils::UUID> {
uuid_type_impl() : concrete_type(uuid_type_name) {}
@@ -2576,6 +2608,7 @@ data_type abstract_type::parse_type(const sstring& name)
{ bytes_type_name, bytes_type },
{ utf8_type_name, utf8_type },
{ boolean_type_name, boolean_type },
{ date_type_name, date_type },
{ timeuuid_type_name, timeuuid_type },
{ timestamp_type_name, timestamp_type },
{ uuid_type_name, uuid_type },