#pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { /** Позволяет получить тип результата применения функций +, -, *, /, %, div (целочисленное деление). * Правила отличаются от используемых в C++. */ namespace NumberTraits { using Unsigned = boost::mpl::false_ ; using Signed = boost::mpl::true_ ; using Integer = boost::mpl::false_ ; using Floating = boost::mpl::true_ ; using Bits0 = boost::mpl::int_<0> ; using Bits8 = boost::mpl::int_<8> ; using Bits16 = boost::mpl::int_<16> ; using Bits32 = boost::mpl::int_<32> ; using Bits64 = boost::mpl::int_<64> ; using BitsTooMany = boost::mpl::int_<1024>; struct Error {}; template struct Next; template <> struct Next { using Type = Bits0; }; template <> struct Next { using Type = Bits16; }; template <> struct Next { using Type = Bits32; }; template <> struct Next { using Type = Bits64; }; template <> struct Next { using Type = Bits64; }; template struct ExactNext { using Type = typename Next::Type; }; template <> struct ExactNext { using Type = BitsTooMany; }; template struct Traits; template <> struct Traits { using Bits = Unsigned Sign; typedef Integer Floatness; typedef Bits0; }; template <> struct Traits { using Bits = Unsigned Sign; typedef Integer Floatness; typedef Bits8; }; template <> struct Traits { using Bits = Unsigned Sign; typedef Integer Floatness; typedef Bits16; }; template <> struct Traits { using Bits = Unsigned Sign; typedef Integer Floatness; typedef Bits32; }; template <> struct Traits { using Bits = Unsigned Sign; typedef Integer Floatness; typedef Bits64; }; template <> struct Traits { using Bits = Signed Sign; typedef Integer Floatness; typedef Bits8; }; template <> struct Traits { using Bits = Signed Sign; typedef Integer Floatness; typedef Bits16; }; template <> struct Traits { using Bits = Signed Sign; typedef Integer Floatness; typedef Bits32; }; template <> struct Traits { using Bits = Signed Sign; typedef Integer Floatness; typedef Bits64; }; template <> struct Traits { using Bits = Signed Sign; typedef Floating Floatness; typedef Bits32; }; template <> struct Traits { using Bits = Signed Sign; typedef Floating Floatness; typedef Bits64; }; template struct Construct; template <> struct Construct { using Type = void ; }; template <> struct Construct { using Type = void ; }; template <> struct Construct { using Type = void ; }; template <> struct Construct { using Type = void ; }; template <> struct Construct { using Type = UInt8 ; }; template <> struct Construct { using Type = UInt16 ; }; template <> struct Construct { using Type = UInt32 ; }; template <> struct Construct { using Type = UInt64 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float64 ; }; template <> struct Construct { using Type = Int8 ; }; template <> struct Construct { using Type = Int16 ; }; template <> struct Construct { using Type = Int32 ; }; template <> struct Construct { using Type = Int64 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float32 ; }; template <> struct Construct { using Type = Float64 ; }; template struct Construct { using Type = Error; }; template inline bool isErrorType() { return false; } template <> inline bool isErrorType() { return true; } /** Результат сложения или умножения вычисляется по следующим правилам: * - если один из аргументов с плавающей запятой, то результат - с плавающей запятой, иначе - целый; * - если одно из аргументов со знаком, то результат - со знаком, иначе - без знака; * - результат содержит больше бит (не только значащих), чем максимум в аргументах * (например, UInt8 + Int32 = Int64). */ template struct ResultOfAdditionMultiplication { typedef typename Construct< typename boost::mpl::or_::Sign, typename Traits::Sign>::type, typename boost::mpl::or_::Floatness, typename Traits::Floatness>::type, typename Next::Bits, typename Traits::Bits>::type>::Type>::Type Type; }; template struct ResultOfSubtraction { typedef typename Construct< Signed, typename boost::mpl::or_::Floatness, typename Traits::Floatness>::type, typename Next::Bits, typename Traits::Bits>::type>::Type>::Type Type; }; /** При делении всегда получается число с плавающей запятой. */ template struct ResultOfFloatingPointDivision { using Type = Float64; }; /** При целочисленном делении получается число, битность которого равна делимому. */ template struct ResultOfIntegerDivision { typedef typename Construct< typename boost::mpl::or_::Sign, typename Traits::Sign>::type, typename boost::mpl::or_::Floatness, typename Traits::Floatness>::type, typename Traits::Bits>::Type Type; }; /** При взятии остатка получается число, битность которого равна делителю. */ template struct ResultOfModulo { typedef typename Construct< typename boost::mpl::or_::Sign, typename Traits::Sign>::type, Integer, typename Traits::Bits>::Type Type; }; template struct ResultOfNegate { typedef typename Construct< Signed, typename Traits::Floatness, typename boost::mpl::if_< typename Traits::Sign, typename Traits::Bits, typename Next::Bits>::Type>::type>::Type Type; }; template struct ResultOfAbs { typedef typename Construct< Unsigned, typename Traits::Floatness, typename Traits ::Bits>::Type Type; }; /** При побитовых операциях получается целое число, битность которого равна максимальной из битностей аргументов. */ template struct ResultOfBit { typedef typename Construct< typename boost::mpl::or_::Sign, typename Traits::Sign>::type, Integer, typename boost::mpl::max< typename boost::mpl::if_< typename Traits::Floatness, Bits64, typename Traits::Bits>::type, typename boost::mpl::if_< typename Traits::Floatness, Bits64, typename Traits::Bits>::type>::type>::Type Type; }; template struct ResultOfBitNot { typedef typename Construct< typename Traits::Sign, Integer, typename Traits::Bits>::Type Type; }; /** Приведение типов для функции if: * 1) void, Type -> Type * 2) UInt, UInt -> UInt * 3) Int, Int -> Int * 4) Float, Float -> Float * 5) UInt, Int -> Int * 6) Float, [U]Int -> Float * 7) UInt64 , Int -> Error * 8) Float, [U]Int64 -> Error */ template struct ResultOfIf { typedef /// 1) typename boost::mpl::if_< typename boost::mpl::equal_to::Bits, Bits0>::type, B, typename boost::mpl::if_< typename boost::mpl::equal_to::Bits, Bits0>::type, A, /// 4) и 6) typename boost::mpl::if_< typename boost::mpl::or_< typename Traits::Floatness, typename Traits::Floatness>::type, typename Construct< Signed, Floating, typename boost::mpl::max< /// Этот максимум нужен только потому что if_ всегда вычисляет все аргументы. typename boost::mpl::max< typename boost::mpl::if_< typename Traits::Floatness, typename Traits::Bits, typename ExactNext::Bits>::Type>::type, typename boost::mpl::if_< typename Traits::Floatness, typename Traits::Bits, typename ExactNext::Bits>::Type>::type>::type, Bits32>::type>::Type, /// 2) и 3) typename boost::mpl::if_< typename boost::mpl::equal_to< typename Traits::Sign, typename Traits::Sign>::type, typename boost::mpl::if_< typename boost::mpl::less< typename Traits::Bits, typename Traits::Bits>::type, B, A>::type, /// 5) typename Construct< Signed, Integer, typename boost::mpl::max< typename boost::mpl::if_< typename Traits::Sign, typename Traits::Bits, typename ExactNext::Bits>::Type>::type, typename boost::mpl::if_< typename Traits::Sign, typename Traits::Bits, typename ExactNext::Bits>::Type>::type>::type>::Type>::type>::type>::type>::type Type; }; /** Перед применением оператора % и побитовых операций, операнды приводятся к целым числам. */ template struct ToInteger { typedef typename Construct< typename Traits::Sign, Integer, typename boost::mpl::if_< typename Traits::Floatness, Bits64, typename Traits::Bits>::type>::Type Type; }; /// Notes on type composition. /// /// I. Problem statement. /// /// Type composition with ResultOfIf is not associative. Example: /// (Int8 x UInt32) x Float32 = Int64 x Float32 = Error; /// Int8 x (UInt32 x Float32) = Int8 x Float64 = Float64. /// In order to sort out this issue, we design a slightly improved version /// of ResultOfIf. /// /// II. A more rigorous approach to ResultOfIf. /// /// First we represent the set of types: /// T = {Void,Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64,Float32,Float64} /// as a poset P with the partial order being such that for any t1,t2 ∈ T, /// t1 < t2 if and only if the domain of values of t1 is included in the domain /// of values of t2. /// /// For each type t ∈ T, we define C(t) as the set of chains of the poset P whose /// unique minimal element is T. /// /// Now for any two types t1,t2 ∈ T, we define the poset C(t1,t2) as the intersection /// C(t1) ∩ C(t2). /// /// Denote K(t1,t2) as the unique antichain of C(t1,t2) in which each element minimally /// represents both t1 and t2. It is important to keep in mind that t1 and t2 are /// *not* comparable. /// /// For the most part, K(t1,t2) coincides with the result of the application of /// ResultOfIf to t1 and t2. Nevertheless, for some particular combinations of t1 /// and t2, the map K returns one of the following two antichains: {Int32,Float32}, /// {Int64,Float64}. /// /// From these observations, we conclude that the type system T and the composition /// law ResultOfIf are not powerful enough to represent all the combinations of /// elements of T. That is the reason why ResultOfIf is not associative. /// /// III. Extending ResultOfIf. /// /// Let's embed T into a larger set E of "enriched types" such that: /// 1. E ⊂ TxT; /// 2. for each t ∈ T, (T,Void) ∈ E. /// 3. (Int32,Float32) ∈ E /// 4. (Int64,Float64) ∈ E. /// /// E represents the image A of the map K, a set of antichains, as a set of types. /// /// Consider the canonical injection ψ : T x T ----> E x E and the natural bijection /// φ : A ----> E. /// Then there exists a unique map K' : E x E ----> E, that makes the diagram below /// commutative: /// /// K /// T x T ----> A /// | | /// | ψ | φ /// ↓ L ↓ /// E x E ----> E /// /// L is exactly the same map as K, the sole difference being that L takes as /// parameters extended types that map to ordinary ones. /// /// Finally we extend the map L. To this end, we complete the type composition table /// with the new types (Int32,Float32) and (Int64,Float64) appearing on either /// the left-hand side or the right-hand side. This extended map is called /// TypeProduct in the implementation. TypeProduct is both commutative and associative. /// /// IV. Usage. /// /// When we need to compose ordinary types, the following is to be performed: /// 1. embed each type into its counterpart in E with EmbedType; /// 2. compose the resulting enriched types with TypeProduct; /// 3. return the first component of the result with ToOrdinaryType, which means /// that, given an extended type e = (p,q) ∈ E, we return the ordinary type p ∈ T. /// /// The result is the type we are looking for. /// /// V. Example. /// /// Suppose we need to compose, as in the problem statement, the types Int8, /// UInt32, and Float32. The corresponding embedded types are: /// (Int8,Void), (UInt32,Void), (Float32,Void). /// /// By computing (Int8 x UInt32) x Float32, we get: /// /// TypeProduct(TypeProduct((Int8,Void),(UInt32,Void)), (Float32,Void)) /// = TypeProduct((Int64,Float64), (Float32,Void)) /// = (Float64,void) /// Thus, (Int8 x UInt32) x Float32 = Float64. /// /// By computing Int8 x (UInt32 x Float32), we get: /// /// TypeProduct((Int8,Void), TypeProduct((UInt32,Void), (Float32,Void))) /// = TypeProduct((Int8,Void), (Float64,Void)) /// = (Float64,void) /// Thus, Int8 x (UInt32 x Float32) = Float64. /// namespace Enriched { /// Definitions of enriched types. using Void = std::tuple; using Int8 = std::tuple; using Int16 = std::tuple; using Int32 = std::tuple; using Int64 = std::tuple; using UInt8 = std::tuple; using UInt16 = std::tuple; using UInt32 = std::tuple; using UInt64 = std::tuple; using Float32 = std::tuple; using Float64 = std::tuple; using IntFloat32 = std::tuple; using IntFloat64 = std::tuple; } /// Embed an ordinary type into the corresponding enriched type. template struct EmbedType; template <> struct EmbedType { using Type = Enriched::Void; }; template <> struct EmbedType { using Type = Enriched::Int8; }; template <> struct EmbedType { using Type = Enriched::Int16; }; template <> struct EmbedType { using Type = Enriched::Int32; }; template <> struct EmbedType { using Type = Enriched::Int64; }; template <> struct EmbedType { using Type = Enriched::UInt8; }; template <> struct EmbedType { using Type = Enriched::UInt16; }; template <> struct EmbedType { using Type = Enriched::UInt32; }; template <> struct EmbedType { using Type = Enriched::UInt64; }; template <> struct EmbedType { using Type = Enriched::Float32; }; template <> struct EmbedType { using Type = Enriched::Float64; }; /// Get an ordinary type from an enriched type. template struct ToOrdinaryType { using Type = typename std::tuple_element<0, TType>::type; }; /// Get an ordinary type from an enriched type. /// Error case. template <> struct ToOrdinaryType { using Type = Error; }; /// Compute the product of two enriched numeric types. template struct TypeProduct { using Type = Error; }; /// Compute the product of two enriched numeric types. /// Case when both of the source types and the resulting type map to ordinary types. template struct TypeProduct, std::tuple > { private: using Result = typename NumberTraits::ResultOfIf::Type; public: using Type = typename std::conditional< std::is_same::value, Error, std::tuple >::type; }; /// Compute the product of two enriched numeric types. /// Case when a source type or the resulting type does not map to any ordinary type. template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::Int32; }; template <> struct TypeProduct { using Type = Enriched::Int32; }; template <> struct TypeProduct { using Type = Enriched::Int64; }; template <> struct TypeProduct { using Type = Enriched::Int64; }; template <> struct TypeProduct { using Type = Enriched::Float32; }; template <> struct TypeProduct { using Type = Enriched::Float32; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat32; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::Int64; }; template <> struct TypeProduct { using Type = Enriched::Int64; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::Float64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; template <> struct TypeProduct { using Type = Enriched::IntFloat64; }; } }