2011-08-28 00:31:30 +00:00
|
|
|
#pragma once
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2016-05-28 14:14:18 +00:00
|
|
|
#include <algorithm>
|
2014-01-08 16:33:28 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <functional>
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-07-04 10:42:53 +00:00
|
|
|
#include <Common/UInt128.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2017-12-14 03:56:56 +00:00
|
|
|
#include <Core/Defines.h>
|
2015-12-24 14:31:41 +00:00
|
|
|
#include <common/strong_typedef.h>
|
2014-12-09 12:07:27 +00:00
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2013-10-11 11:43:50 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int BAD_TYPE_OF_FIELD;
|
|
|
|
extern const int BAD_GET;
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2013-11-02 22:20:58 +00:00
|
|
|
class Field;
|
2017-01-06 17:41:19 +00:00
|
|
|
using Array = std::vector<Field>;
|
2015-12-24 14:31:41 +00:00
|
|
|
using TupleBackend = std::vector<Field>;
|
2017-01-06 17:41:19 +00:00
|
|
|
STRONG_TYPEDEF(TupleBackend, Tuple); /// Array and Tuple are different types with equal representation inside Field.
|
2013-10-11 11:43:50 +00:00
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2018-08-09 16:32:01 +00:00
|
|
|
class DecField
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr UInt32 wrongScale() { return std::numeric_limits<UInt32>::max(); }
|
|
|
|
|
|
|
|
DecField(Int128 value, UInt32 scale_ = wrongScale())
|
|
|
|
: dec(value),
|
|
|
|
scale(scale_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
operator Dec32() const { return dec; }
|
|
|
|
operator Dec64() const { return dec; }
|
|
|
|
operator Dec128() const { return dec; }
|
|
|
|
UInt32 getScale() const { return scale; }
|
|
|
|
|
|
|
|
bool operator < (const DecField & r) const;
|
|
|
|
bool operator <= (const DecField & r) const;
|
|
|
|
bool operator == (const DecField & r) const;
|
|
|
|
|
|
|
|
bool operator > (const DecField & r) const { return r < *this; }
|
|
|
|
bool operator >= (const DecField & r) const { return r <= * this; }
|
|
|
|
bool operator != (const DecField & r) const { return !(*this == r); }
|
|
|
|
|
|
|
|
bool hasScale() const { return scale != wrongScale(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Int128 dec;
|
|
|
|
UInt32 scale;
|
|
|
|
};
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** 32 is enough. Round number is used for alignment and for better arithmetic inside std::vector.
|
2018-02-18 02:22:32 +00:00
|
|
|
* NOTE: Actually, sizeof(std::string) is 32 when using libc++, so Field is 40 bytes.
|
2013-01-08 21:32:16 +00:00
|
|
|
*/
|
2016-01-14 01:43:14 +00:00
|
|
|
#define DBMS_MIN_FIELD_SIZE 32
|
2013-01-08 21:32:16 +00:00
|
|
|
|
2013-01-05 20:03:19 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/** Discriminated union of several types.
|
|
|
|
* Made for replacement of `boost::variant`
|
|
|
|
* is not generalized,
|
|
|
|
* but somewhat more efficient, and simpler.
|
2013-01-05 20:03:19 +00:00
|
|
|
*
|
2017-08-29 19:07:54 +00:00
|
|
|
* Used to represent a single value of one of several types in memory.
|
|
|
|
* Warning! Prefer to use chunks of columns instead of single values. See Column.h
|
2010-03-01 16:59:51 +00:00
|
|
|
*/
|
2016-01-14 01:43:14 +00:00
|
|
|
class Field
|
2013-01-05 20:03:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Types
|
|
|
|
{
|
|
|
|
/// Type tag.
|
|
|
|
enum Which
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
Null = 0,
|
|
|
|
UInt64 = 1,
|
|
|
|
Int64 = 2,
|
|
|
|
Float64 = 3,
|
2017-07-04 10:42:53 +00:00
|
|
|
UInt128 = 4,
|
2018-07-20 19:05:07 +00:00
|
|
|
Int128 = 5,
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Non-POD types.
|
|
|
|
|
2017-05-11 19:48:46 +00:00
|
|
|
String = 16,
|
|
|
|
Array = 17,
|
|
|
|
Tuple = 18,
|
2018-08-09 16:32:01 +00:00
|
|
|
Decimal = 19,
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const int MIN_NON_POD = 16;
|
|
|
|
|
|
|
|
static const char * toString(Which which)
|
|
|
|
{
|
|
|
|
switch (which)
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
case Null: return "Null";
|
|
|
|
case UInt64: return "UInt64";
|
2017-07-06 14:42:27 +00:00
|
|
|
case UInt128: return "UInt128";
|
2017-05-11 19:48:46 +00:00
|
|
|
case Int64: return "Int64";
|
2018-07-20 19:05:07 +00:00
|
|
|
case Int128: return "Int128";
|
2017-05-11 19:48:46 +00:00
|
|
|
case Float64: return "Float64";
|
|
|
|
case String: return "String";
|
|
|
|
case Array: return "Array";
|
|
|
|
case Tuple: return "Tuple";
|
2018-08-09 16:32:01 +00:00
|
|
|
case Decimal: return "Decimal";
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
|
|
|
throw Exception("Bad type of Field", ErrorCodes::BAD_TYPE_OF_FIELD);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Returns an identifier for the type or vice versa.
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T> struct TypeToEnum;
|
|
|
|
template <Types::Which which> struct EnumToType;
|
|
|
|
|
|
|
|
|
|
|
|
Field()
|
|
|
|
: which(Types::Null)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/** Despite the presence of a template constructor, this constructor is still needed,
|
|
|
|
* since, in its absence, the compiler will still generate the default constructor.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
Field(const Field & rhs)
|
|
|
|
{
|
|
|
|
create(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Field(Field && rhs)
|
|
|
|
{
|
|
|
|
create(std::move(rhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-25 04:01:46 +00:00
|
|
|
Field(T && rhs, std::integral_constant<int, Field::TypeToEnum<std::decay_t<T>>::value> * = nullptr)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
createConcrete(std::forward<T>(rhs));
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// Create a string inplace.
|
2017-04-01 07:20:54 +00:00
|
|
|
Field(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
create(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Field(const unsigned char * data, size_t size)
|
|
|
|
{
|
|
|
|
create(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// NOTE In case when field already has string type, more direct assign is possible.
|
|
|
|
void assignString(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
create(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assignString(const unsigned char * data, size_t size)
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
create(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Field & operator= (const Field & rhs)
|
|
|
|
{
|
|
|
|
if (this != &rhs)
|
|
|
|
{
|
|
|
|
if (which != rhs.which)
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
create(rhs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assign(rhs); /// This assigns string or vector without deallocation of existing buffer.
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Field & operator= (Field && rhs)
|
|
|
|
{
|
|
|
|
if (this != &rhs)
|
|
|
|
{
|
|
|
|
if (which != rhs.which)
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
create(std::move(rhs));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assign(std::move(rhs));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-25 04:01:46 +00:00
|
|
|
std::enable_if_t<!std::is_same_v<std::decay_t<T>, Field>, Field &>
|
2017-04-01 07:20:54 +00:00
|
|
|
operator= (T && rhs)
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
if (which != TypeToEnum<std::decay_t<T>>::value)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
createConcrete(std::forward<T>(rhs));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assignConcrete(std::forward<T>(rhs));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Field()
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Types::Which getType() const { return which; }
|
|
|
|
const char * getTypeName() const { return Types::toString(which); }
|
|
|
|
|
|
|
|
bool isNull() const { return which == Types::Null; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> T & get()
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
using TWithoutRef = std::remove_reference_t<T>;
|
2018-03-23 16:05:14 +00:00
|
|
|
TWithoutRef * MAY_ALIAS ptr = reinterpret_cast<TWithoutRef*>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
return *ptr;
|
2018-06-03 16:51:31 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
template <typename T> const T & get() const
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
using TWithoutRef = std::remove_reference_t<T>;
|
2018-03-23 16:05:14 +00:00
|
|
|
const TWithoutRef * MAY_ALIAS ptr = reinterpret_cast<const TWithoutRef*>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
return *ptr;
|
2018-06-03 16:51:31 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-28 21:45:15 +00:00
|
|
|
template <typename T> bool tryGet(T & result)
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
const Types::Which requested = TypeToEnum<std::decay_t<T>>::value;
|
2017-06-28 21:45:15 +00:00
|
|
|
if (which != requested)
|
|
|
|
return false;
|
|
|
|
result = get<T>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool tryGet(T & result) const
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
const Types::Which requested = TypeToEnum<std::decay_t<T>>::value;
|
2017-06-28 21:45:15 +00:00
|
|
|
if (which != requested)
|
|
|
|
return false;
|
|
|
|
result = get<T>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T> T & safeGet()
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
const Types::Which requested = TypeToEnum<std::decay_t<T>>::value;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (which != requested)
|
|
|
|
throw Exception("Bad get: has " + std::string(getTypeName()) + ", requested " + std::string(Types::toString(requested)), ErrorCodes::BAD_GET);
|
|
|
|
return get<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> const T & safeGet() const
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
const Types::Which requested = TypeToEnum<std::decay_t<T>>::value;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (which != requested)
|
|
|
|
throw Exception("Bad get: has " + std::string(getTypeName()) + ", requested " + std::string(Types::toString(requested)), ErrorCodes::BAD_GET);
|
|
|
|
return get<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator< (const Field & rhs) const
|
|
|
|
{
|
|
|
|
if (which < rhs.which)
|
|
|
|
return true;
|
|
|
|
if (which > rhs.which)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (which)
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Null: return false;
|
|
|
|
case Types::UInt64: return get<UInt64>() < rhs.get<UInt64>();
|
2017-07-06 14:42:27 +00:00
|
|
|
case Types::UInt128: return get<UInt128>() < rhs.get<UInt128>();
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Int64: return get<Int64>() < rhs.get<Int64>();
|
2018-07-20 19:05:07 +00:00
|
|
|
case Types::Int128: return get<Int128>() < rhs.get<Int128>();
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Float64: return get<Float64>() < rhs.get<Float64>();
|
|
|
|
case Types::String: return get<String>() < rhs.get<String>();
|
|
|
|
case Types::Array: return get<Array>() < rhs.get<Array>();
|
|
|
|
case Types::Tuple: return get<Tuple>() < rhs.get<Tuple>();
|
2018-08-09 16:32:01 +00:00
|
|
|
case Types::Decimal: return get<DecField>() < rhs.get<DecField>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
|
|
|
throw Exception("Bad type of Field", ErrorCodes::BAD_TYPE_OF_FIELD);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator> (const Field & rhs) const
|
|
|
|
{
|
|
|
|
return rhs < *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<= (const Field & rhs) const
|
|
|
|
{
|
|
|
|
if (which < rhs.which)
|
|
|
|
return true;
|
|
|
|
if (which > rhs.which)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (which)
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Null: return true;
|
|
|
|
case Types::UInt64: return get<UInt64>() <= rhs.get<UInt64>();
|
2017-07-06 14:42:27 +00:00
|
|
|
case Types::UInt128: return get<UInt128>() <= rhs.get<UInt128>();
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Int64: return get<Int64>() <= rhs.get<Int64>();
|
2018-07-20 19:05:07 +00:00
|
|
|
case Types::Int128: return get<Int128>() <= rhs.get<Int128>();
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Float64: return get<Float64>() <= rhs.get<Float64>();
|
|
|
|
case Types::String: return get<String>() <= rhs.get<String>();
|
|
|
|
case Types::Array: return get<Array>() <= rhs.get<Array>();
|
|
|
|
case Types::Tuple: return get<Tuple>() <= rhs.get<Tuple>();
|
2018-08-09 16:32:01 +00:00
|
|
|
case Types::Decimal: return get<DecField>() <= rhs.get<DecField>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
|
|
|
throw Exception("Bad type of Field", ErrorCodes::BAD_TYPE_OF_FIELD);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>= (const Field & rhs) const
|
|
|
|
{
|
|
|
|
return rhs <= *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (const Field & rhs) const
|
|
|
|
{
|
|
|
|
if (which != rhs.which)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (which)
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Null: return true;
|
2017-04-01 07:20:54 +00:00
|
|
|
case Types::UInt64:
|
|
|
|
case Types::Int64:
|
2017-07-04 10:42:53 +00:00
|
|
|
case Types::Float64: return get<UInt64>() == rhs.get<UInt64>();
|
|
|
|
case Types::String: return get<String>() == rhs.get<String>();
|
|
|
|
case Types::Array: return get<Array>() == rhs.get<Array>();
|
|
|
|
case Types::Tuple: return get<Tuple>() == rhs.get<Tuple>();
|
|
|
|
case Types::UInt128: return get<UInt128>() == rhs.get<UInt128>();
|
2018-07-20 19:05:07 +00:00
|
|
|
case Types::Int128: return get<Int128>() == rhs.get<Int128>();
|
2018-08-09 16:32:01 +00:00
|
|
|
case Types::Decimal: return get<DecField>() == rhs.get<DecField>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
|
|
|
throw Exception("Bad type of Field", ErrorCodes::BAD_TYPE_OF_FIELD);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!= (const Field & rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2013-01-05 20:03:19 +00:00
|
|
|
|
|
|
|
private:
|
2017-12-25 04:01:46 +00:00
|
|
|
std::aligned_union_t<DBMS_MIN_FIELD_SIZE - sizeof(Types::Which),
|
2018-08-09 16:32:01 +00:00
|
|
|
Null, UInt64, UInt128, Int64, Int128, Float64, String, Array, Tuple, DecField
|
2017-12-25 04:01:46 +00:00
|
|
|
> storage;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Types::Which which;
|
|
|
|
|
|
|
|
|
|
|
|
/// Assuming there was no allocated state or it was deallocated (see destroy).
|
|
|
|
template <typename T>
|
|
|
|
void createConcrete(T && x)
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
using JustT = std::decay_t<T>;
|
2018-03-23 16:05:14 +00:00
|
|
|
JustT * MAY_ALIAS ptr = reinterpret_cast<JustT *>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
new (ptr) JustT(std::forward<T>(x));
|
|
|
|
which = TypeToEnum<JustT>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assuming same types.
|
|
|
|
template <typename T>
|
|
|
|
void assignConcrete(T && x)
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
using JustT = std::decay_t<T>;
|
2018-03-23 16:05:14 +00:00
|
|
|
JustT * MAY_ALIAS ptr = reinterpret_cast<JustT *>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
*ptr = std::forward<T>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename F, typename Field> /// Field template parameter may be const or non-const Field.
|
|
|
|
static void dispatch(F && f, Field & field)
|
|
|
|
{
|
|
|
|
switch (field.which)
|
|
|
|
{
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Null: f(field.template get<Null>()); return;
|
2018-05-08 14:42:22 +00:00
|
|
|
|
|
|
|
// gcc 7.3.0
|
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::UInt64: f(field.template get<UInt64>()); return;
|
2017-07-06 14:42:27 +00:00
|
|
|
case Types::UInt128: f(field.template get<UInt128>()); return;
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Int64: f(field.template get<Int64>()); return;
|
2018-07-20 19:05:07 +00:00
|
|
|
case Types::Int128: f(field.template get<Int128>()); return;
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::Float64: f(field.template get<Float64>()); return;
|
2018-05-08 14:42:22 +00:00
|
|
|
#if !__clang__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2017-05-11 19:48:46 +00:00
|
|
|
case Types::String: f(field.template get<String>()); return;
|
|
|
|
case Types::Array: f(field.template get<Array>()); return;
|
|
|
|
case Types::Tuple: f(field.template get<Tuple>()); return;
|
2018-08-09 16:32:01 +00:00
|
|
|
case Types::Decimal: f(field.template get<DecField>()); return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw Exception("Bad type of Field", ErrorCodes::BAD_TYPE_OF_FIELD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void create(const Field & x)
|
|
|
|
{
|
|
|
|
dispatch([this] (auto & value) { createConcrete(value); }, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(Field && x)
|
|
|
|
{
|
|
|
|
dispatch([this] (auto & value) { createConcrete(std::move(value)); }, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assign(const Field & x)
|
|
|
|
{
|
|
|
|
dispatch([this] (auto & value) { assignConcrete(value); }, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assign(Field && x)
|
|
|
|
{
|
|
|
|
dispatch([this] (auto & value) { assignConcrete(std::move(value)); }, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void create(const char * data, size_t size)
|
|
|
|
{
|
2018-03-23 16:05:14 +00:00
|
|
|
String * MAY_ALIAS ptr = reinterpret_cast<String*>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
new (ptr) String(data, size);
|
|
|
|
which = Types::String;
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(const unsigned char * data, size_t size)
|
|
|
|
{
|
|
|
|
create(reinterpret_cast<const char *>(data), size);
|
|
|
|
}
|
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
ALWAYS_INLINE void destroy()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (which < Types::MIN_NON_POD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (which)
|
|
|
|
{
|
|
|
|
case Types::String:
|
|
|
|
destroy<String>();
|
|
|
|
break;
|
|
|
|
case Types::Array:
|
|
|
|
destroy<Array>();
|
|
|
|
break;
|
|
|
|
case Types::Tuple:
|
|
|
|
destroy<Tuple>();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
which = Types::Null; /// for exception safety in subsequent calls to destroy and create, when create fails.
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void destroy()
|
|
|
|
{
|
2018-03-23 16:05:14 +00:00
|
|
|
T * MAY_ALIAS ptr = reinterpret_cast<T*>(&storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
ptr->~T();
|
|
|
|
}
|
2013-01-05 20:03:19 +00:00
|
|
|
};
|
|
|
|
|
2016-01-14 01:43:14 +00:00
|
|
|
#undef DBMS_MIN_FIELD_SIZE
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2014-05-12 00:45:50 +00:00
|
|
|
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::TypeToEnum<Null> { static const Types::Which value = Types::Null; };
|
|
|
|
template <> struct Field::TypeToEnum<UInt64> { static const Types::Which value = Types::UInt64; };
|
2017-07-06 14:42:27 +00:00
|
|
|
template <> struct Field::TypeToEnum<UInt128> { static const Types::Which value = Types::UInt128; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::TypeToEnum<Int64> { static const Types::Which value = Types::Int64; };
|
2018-07-20 19:05:07 +00:00
|
|
|
template <> struct Field::TypeToEnum<Int128> { static const Types::Which value = Types::Int128; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::TypeToEnum<Float64> { static const Types::Which value = Types::Float64; };
|
|
|
|
template <> struct Field::TypeToEnum<String> { static const Types::Which value = Types::String; };
|
|
|
|
template <> struct Field::TypeToEnum<Array> { static const Types::Which value = Types::Array; };
|
|
|
|
template <> struct Field::TypeToEnum<Tuple> { static const Types::Which value = Types::Tuple; };
|
2018-08-09 16:32:01 +00:00
|
|
|
template <> struct Field::TypeToEnum<DecField>{ static const Types::Which value = Types::Decimal; };
|
2013-01-05 20:03:19 +00:00
|
|
|
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::Null> { using Type = Null; };
|
|
|
|
template <> struct Field::EnumToType<Field::Types::UInt64> { using Type = UInt64; };
|
2017-07-06 14:42:27 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::UInt128> { using Type = UInt128; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::Int64> { using Type = Int64; };
|
2018-07-20 19:05:07 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::Int128> { using Type = Int128; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::Float64> { using Type = Float64; };
|
|
|
|
template <> struct Field::EnumToType<Field::Types::String> { using Type = String; };
|
|
|
|
template <> struct Field::EnumToType<Field::Types::Array> { using Type = Array; };
|
|
|
|
template <> struct Field::EnumToType<Field::Types::Tuple> { using Type = Tuple; };
|
2018-08-09 16:32:01 +00:00
|
|
|
template <> struct Field::EnumToType<Field::Types::Decimal> { using Type = DecField; };
|
2013-01-05 20:03:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T get(const Field & field)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return field.template get<T>();
|
2010-03-01 16:59:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-05 20:03:19 +00:00
|
|
|
template <typename T>
|
|
|
|
T get(Field & field)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return field.template get<T>();
|
2013-01-05 20:03:19 +00:00
|
|
|
}
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2013-01-05 20:03:19 +00:00
|
|
|
template <typename T>
|
|
|
|
T safeGet(const Field & field)
|
2010-03-01 16:59:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return field.template safeGet<T>();
|
2013-01-05 20:03:19 +00:00
|
|
|
}
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2013-01-05 20:03:19 +00:00
|
|
|
template <typename T>
|
|
|
|
T safeGet(Field & field)
|
2010-03-01 16:59:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return field.template safeGet<T>();
|
2013-01-05 20:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct TypeName<Array> { static std::string get() { return "Array"; } };
|
2015-12-24 14:31:41 +00:00
|
|
|
template <> struct TypeName<Tuple> { static std::string get() { return "Tuple"; } };
|
2013-01-05 20:03:19 +00:00
|
|
|
|
|
|
|
|
2010-03-18 19:32:14 +00:00
|
|
|
template <typename T> struct NearestFieldType;
|
|
|
|
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct NearestFieldType<UInt8> { using Type = UInt64; };
|
|
|
|
template <> struct NearestFieldType<UInt16> { using Type = UInt64; };
|
|
|
|
template <> struct NearestFieldType<UInt32> { using Type = UInt64; };
|
|
|
|
template <> struct NearestFieldType<UInt64> { using Type = UInt64; };
|
2017-07-04 10:42:53 +00:00
|
|
|
template <> struct NearestFieldType<UInt128> { using Type = UInt128; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct NearestFieldType<Int8> { using Type = Int64; };
|
|
|
|
template <> struct NearestFieldType<Int16> { using Type = Int64; };
|
|
|
|
template <> struct NearestFieldType<Int32> { using Type = Int64; };
|
|
|
|
template <> struct NearestFieldType<Int64> { using Type = Int64; };
|
2018-07-20 19:05:07 +00:00
|
|
|
template <> struct NearestFieldType<Int128> { using Type = Int128; };
|
2018-08-09 16:32:01 +00:00
|
|
|
template <> struct NearestFieldType<Dec32> { using Type = DecField; };
|
|
|
|
template <> struct NearestFieldType<Dec64> { using Type = DecField; };
|
|
|
|
template <> struct NearestFieldType<Dec128> { using Type = DecField; };
|
2017-05-11 19:48:46 +00:00
|
|
|
template <> struct NearestFieldType<Float32> { using Type = Float64; };
|
|
|
|
template <> struct NearestFieldType<Float64> { using Type = Float64; };
|
|
|
|
template <> struct NearestFieldType<String> { using Type = String; };
|
|
|
|
template <> struct NearestFieldType<Array> { using Type = Array; };
|
|
|
|
template <> struct NearestFieldType<Tuple> { using Type = Tuple; };
|
|
|
|
template <> struct NearestFieldType<bool> { using Type = UInt64; };
|
|
|
|
template <> struct NearestFieldType<Null> { using Type = Null; };
|
2012-06-07 18:32:21 +00:00
|
|
|
|
2013-10-04 20:15:42 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename NearestFieldType<T>::Type nearestFieldType(const T & x)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return typename NearestFieldType<T>::Type(x);
|
2013-10-04 20:15:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 19:51:09 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
class ReadBuffer;
|
|
|
|
class WriteBuffer;
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// It is assumed that all elements of the array have the same type.
|
2017-01-21 04:24:28 +00:00
|
|
|
void readBinary(Array & x, ReadBuffer & buf);
|
2012-09-05 19:51:09 +00:00
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
inline void readText(Array &, ReadBuffer &) { throw Exception("Cannot read Array.", ErrorCodes::NOT_IMPLEMENTED); }
|
|
|
|
inline void readQuoted(Array &, ReadBuffer &) { throw Exception("Cannot read Array.", ErrorCodes::NOT_IMPLEMENTED); }
|
2012-09-05 19:51:09 +00:00
|
|
|
|
2017-04-30 13:50:16 +00:00
|
|
|
/// It is assumed that all elements of the array have the same type.
|
2017-01-21 04:24:28 +00:00
|
|
|
void writeBinary(const Array & x, WriteBuffer & buf);
|
2014-08-09 22:36:07 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
void writeText(const Array & x, WriteBuffer & buf);
|
2014-08-09 22:36:07 +00:00
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
inline void writeQuoted(const Array &, WriteBuffer &) { throw Exception("Cannot write Array quoted.", ErrorCodes::NOT_IMPLEMENTED); }
|
2012-09-05 19:51:09 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
void readBinary(Tuple & x, ReadBuffer & buf);
|
2014-08-09 22:36:07 +00:00
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
inline void readText(Tuple &, ReadBuffer &) { throw Exception("Cannot read Tuple.", ErrorCodes::NOT_IMPLEMENTED); }
|
|
|
|
inline void readQuoted(Tuple &, ReadBuffer &) { throw Exception("Cannot read Tuple.", ErrorCodes::NOT_IMPLEMENTED); }
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
void writeBinary(const Tuple & x, WriteBuffer & buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
void writeText(const Tuple & x, WriteBuffer & buf);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
inline void writeQuoted(const Tuple &, WriteBuffer &) { throw Exception("Cannot write Tuple quoted.", ErrorCodes::NOT_IMPLEMENTED); }
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|