#pragma once #include #include #include #include #include #include namespace common { template inline bool addOverflow(T x, T y, T & res) { return __builtin_add_overflow(x, y, &res); } template <> inline bool addOverflow(Int32 x, Int32 y, Int32 & res) { return __builtin_sadd_overflow(x, y, &res); } template <> inline bool addOverflow(Int64 x, Int64 y, Int64 & res) { return __builtin_saddl_overflow(x, y, &res); } template <> inline bool addOverflow(__int128 x, __int128 y, __int128 & res) { res = x + y; return (res - y) != x; } template inline bool subOverflow(T x, T y, T & res) { return __builtin_sub_overflow(x, y, &res); } template <> inline bool subOverflow(Int32 x, Int32 y, Int32 & res) { return __builtin_ssub_overflow(x, y, &res); } template <> inline bool subOverflow(Int64 x, Int64 y, Int64 & res) { return __builtin_ssubl_overflow(x, y, &res); } template <> inline bool subOverflow(__int128 x, __int128 y, __int128 & res) { res = x - y; return (res + y) != x; } template inline bool mulOverflow(T x, T y, T & res) { return __builtin_mul_overflow(x, y, &res); } template <> inline bool mulOverflow(Int32 x, Int32 y, Int32 & res) { return __builtin_smul_overflow(x, y, &res); } template <> inline bool mulOverflow(Int64 x, Int64 y, Int64 & res) { return __builtin_smull_overflow(x, y, &res); } template <> inline bool mulOverflow(__int128 x, __int128 y, __int128 & res) { res = x * y; return (res / y) != x; } } namespace DB { /// Methods, that helps dispatching over real column types. template const Type * checkAndGetDataType(const IDataType * data_type) { return typeid_cast(data_type); } template bool checkDataType(const IDataType * data_type) { return checkAndGetDataType(data_type); } template const Type * checkAndGetColumn(const IColumn * column) { return typeid_cast(column); } template bool checkColumn(const IColumn * column) { return checkAndGetColumn(column); } template const ColumnConst * checkAndGetColumnConst(const IColumn * column) { if (!column || !column->isColumnConst()) return {}; const ColumnConst * res = static_cast(column); if (!checkColumn(&res->getDataColumn())) return {}; return res; } template const Type * checkAndGetColumnConstData(const IColumn * column) { const ColumnConst * res = checkAndGetColumnConst(column); if (!res) return {}; return static_cast(&res->getDataColumn()); } template bool checkColumnConst(const IColumn * column) { return checkAndGetColumnConst(column); } /// Returns non-nullptr if column is ColumnConst with ColumnString or ColumnFixedString inside. const ColumnConst * checkAndGetColumnConstStringOrFixedString(const IColumn * column); /// Transform anything to Field. template inline Field toField(const T & x) { return Field(typename NearestFieldType::Type(x)); } Columns convertConstTupleToConstantElements(const ColumnConst & column); /// Returns the copy of a given block in which each column specified in /// the "arguments" parameter is replaced with its respective nested /// column if it is nullable. Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args); /// Similar function as above. Additionally transform the result type if needed. Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args, size_t result); template typename> typename Apply, template typename Op, typename... Args> void callByTypeAndNumber(UInt8 number, bool & done, Args &... args) { done = true; switch (number) { case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; //case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; case TypeNumber::value: Apply(args...); break; default: done = false; } } /// Unroll template using TypeNumber template