mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
dbms: development [#CONV-2944].
This commit is contained in:
parent
ca9ae06b48
commit
cd7e317362
@ -1,5 +1,4 @@
|
||||
#ifndef DBMS_DATA_TYPES_NUMBER_FIXED_H
|
||||
#define DBMS_DATA_TYPES_NUMBER_FIXED_H
|
||||
#pragma once
|
||||
|
||||
#include <DB/Columns/ColumnsNumber.h>
|
||||
#include <DB/DataTypes/IDataTypeNumberFixed.h>
|
||||
@ -36,9 +35,7 @@ DEFINE_DATA_TYPE_NUMBER_FIXED(Int16);
|
||||
DEFINE_DATA_TYPE_NUMBER_FIXED(Int32);
|
||||
DEFINE_DATA_TYPE_NUMBER_FIXED(Int64);
|
||||
|
||||
/* DEFINE_DATA_TYPE_NUMBER_FIXED(Float32);
|
||||
DEFINE_DATA_TYPE_NUMBER_FIXED(Float64); */
|
||||
DEFINE_DATA_TYPE_NUMBER_FIXED(Float32);
|
||||
DEFINE_DATA_TYPE_NUMBER_FIXED(Float64);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -20,13 +20,13 @@ class IDataTypeNumber : public IDataType
|
||||
public:
|
||||
void serializeText(const Field & field, WriteBuffer & ostr) const
|
||||
{
|
||||
writeIntText(boost::get<typename NearestFieldType<FieldType>::Type>(field), ostr);
|
||||
writeText(boost::get<typename NearestFieldType<FieldType>::Type>(field), ostr);
|
||||
}
|
||||
|
||||
void deserializeText(Field & field, ReadBuffer & istr) const
|
||||
{
|
||||
typename NearestFieldType<FieldType>::Type x;
|
||||
readIntText(x, istr);
|
||||
readText(x, istr);
|
||||
field = x;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
#ifndef DBMS_DATA_TYPES_IDATATYPE_NUMBER_FIXED_H
|
||||
#define DBMS_DATA_TYPES_IDATATYPE_NUMBER_FIXED_H
|
||||
#pragma once
|
||||
|
||||
#include <Poco/BinaryWriter.h>
|
||||
#include <Poco/BinaryReader.h>
|
||||
@ -28,13 +27,13 @@ public:
|
||||
{
|
||||
/// ColumnType::value_type - более узкий тип. Например, UInt8, когда тип Field - UInt64
|
||||
typename ColumnType::value_type x = boost::get<FieldType>(field);
|
||||
writeIntBinary(x, ostr);
|
||||
writeBinary(x, ostr);
|
||||
}
|
||||
|
||||
void deserializeBinary(Field & field, ReadBuffer & istr) const
|
||||
{
|
||||
typename ColumnType::value_type x;
|
||||
readIntBinary(x, istr);
|
||||
readBinary(x, istr);
|
||||
field = typename NearestFieldType<FieldType>::Type(x);
|
||||
}
|
||||
|
||||
@ -58,6 +57,5 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
@ -10,7 +10,9 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/** Арифметические функции: +, -, *, /, %, div.
|
||||
/** Арифметические функции: +, -, *, /, %,
|
||||
* div (целочисленное деление),
|
||||
* TODO: <<, >>, <<<, >>>, &, |, ^, &&, ||, ^^, !
|
||||
*/
|
||||
|
||||
template<typename A, typename B>
|
||||
@ -45,6 +47,166 @@ struct PlusImpl
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct MultiplyImpl
|
||||
{
|
||||
typedef typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type ResultType;
|
||||
|
||||
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] * b[i];
|
||||
}
|
||||
|
||||
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] * b;
|
||||
}
|
||||
|
||||
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a * b[i];
|
||||
}
|
||||
|
||||
static void constant_constant(A a, B b, ResultType & c)
|
||||
{
|
||||
c = a * b;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct MinusImpl
|
||||
{
|
||||
typedef typename NumberTraits::ResultOfSubtraction<A, B>::Type ResultType;
|
||||
|
||||
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] - b[i];
|
||||
}
|
||||
|
||||
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] - b;
|
||||
}
|
||||
|
||||
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a - b[i];
|
||||
}
|
||||
|
||||
static void constant_constant(A a, B b, ResultType & c)
|
||||
{
|
||||
c = a - b;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct DivideFloatingImpl
|
||||
{
|
||||
typedef typename NumberTraits::ResultOfFloatingPointDivision<A, B>::Type ResultType;
|
||||
|
||||
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = static_cast<ResultType>(a[i]) / b[i];
|
||||
}
|
||||
|
||||
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = static_cast<ResultType>(a[i]) / b;
|
||||
}
|
||||
|
||||
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = static_cast<ResultType>(a) / b[i];
|
||||
}
|
||||
|
||||
static void constant_constant(A a, B b, ResultType & c)
|
||||
{
|
||||
c = static_cast<ResultType>(a) / b;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct DivideIntegralImpl
|
||||
{
|
||||
typedef typename NumberTraits::ResultOfIntegerDivision<A, B>::Type ResultType;
|
||||
|
||||
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] / b[i];
|
||||
}
|
||||
|
||||
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] / b;
|
||||
}
|
||||
|
||||
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a / b[i];
|
||||
}
|
||||
|
||||
static void constant_constant(A a, B b, ResultType & c)
|
||||
{
|
||||
c = a / b;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct ModuloImpl
|
||||
{
|
||||
typedef typename NumberTraits::ResultOfModulo<A, B>::Type ResultType;
|
||||
|
||||
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] % b[i];
|
||||
}
|
||||
|
||||
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a[i] % b;
|
||||
}
|
||||
|
||||
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
|
||||
{
|
||||
size_t size = a.size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
c[i] = a % b[i];
|
||||
}
|
||||
|
||||
static void constant_constant(A a, B b, ResultType & c)
|
||||
{
|
||||
c = a % b;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <template <typename, typename> class Impl, typename Name>
|
||||
class FunctionBinaryArithmetic : public IFunction
|
||||
@ -160,9 +322,19 @@ public:
|
||||
};
|
||||
|
||||
|
||||
struct NamePlus { static const char * get() { return "plus"; } };
|
||||
struct NamePlus { static const char * get() { return "plus"; } };
|
||||
struct NameMinus { static const char * get() { return "minus"; } };
|
||||
struct NameMultiply { static const char * get() { return "multiply"; } };
|
||||
struct NameDivideFloating { static const char * get() { return "divide"; } };
|
||||
struct NameDivideIntegral { static const char * get() { return "div"; } };
|
||||
struct NameModulo { static const char * get() { return "modulo"; } };
|
||||
|
||||
typedef FunctionBinaryArithmetic<PlusImpl, NamePlus> FunctionPlus;
|
||||
typedef FunctionBinaryArithmetic<PlusImpl, NamePlus> FunctionPlus;
|
||||
typedef FunctionBinaryArithmetic<MinusImpl, NameMinus> FunctionMinus;
|
||||
typedef FunctionBinaryArithmetic<MultiplyImpl, NameMultiply> FunctionMultiply;
|
||||
typedef FunctionBinaryArithmetic<DivideFloatingImpl, NameDivideFloating> FunctionDivideFloating;
|
||||
typedef FunctionBinaryArithmetic<DivideIntegralImpl, NameDivideIntegral> FunctionDivideIntegral;
|
||||
typedef FunctionBinaryArithmetic<ModuloImpl, NameModulo> FunctionModulo;
|
||||
|
||||
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ namespace NumberTraits
|
||||
typename Next<typename boost::mpl::max<typename Traits<A>::Bits, typename Traits<B>::Bits>::type>::Type>::Type Type;
|
||||
};
|
||||
|
||||
template <typename A, typename B> struct ResultOfSubstraction
|
||||
template <typename A, typename B> struct ResultOfSubtraction
|
||||
{
|
||||
typedef typename Construct<
|
||||
typename boost::mpl::if_<
|
||||
|
@ -48,11 +48,22 @@ static inline void throwReadAfterEOF()
|
||||
|
||||
/// Чтение числа в native формате
|
||||
template <typename T>
|
||||
inline void readIntBinary(T & x, ReadBuffer & buf)
|
||||
inline void readBinary(T & x, ReadBuffer & buf)
|
||||
{
|
||||
buf.readStrict(reinterpret_cast<char *>(&x), sizeof(x));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void readIntBinary(T & x, ReadBuffer & buf)
|
||||
{
|
||||
readBinary(x, buf);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void readFloatBinary(T & x, ReadBuffer & buf)
|
||||
{
|
||||
readBinary(x, buf);
|
||||
}
|
||||
|
||||
inline void readChar(char & x, ReadBuffer & buf)
|
||||
{
|
||||
@ -218,6 +229,21 @@ void readFloatText(T & x, ReadBuffer & buf)
|
||||
x = -x;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void readText(T & x, ReadBuffer & buf);
|
||||
|
||||
template <> inline void readText<UInt8> (UInt8 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<UInt16> (UInt16 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<UInt32> (UInt32 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<UInt64> (UInt64 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<Int8> (Int8 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<Int16> (Int16 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<Int32> (Int32 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<Int64> (Int64 & x, ReadBuffer & buf) { readIntText(x, buf); }
|
||||
template <> inline void readText<Float32> (Float32 & x, ReadBuffer & buf) { readFloatText(x, buf); }
|
||||
template <> inline void readText<Float64> (Float64 & x, ReadBuffer & buf) { readFloatText(x, buf); }
|
||||
|
||||
|
||||
/// грубо; всё до '\n' или '\t'
|
||||
void readString(String & s, ReadBuffer & buf);
|
||||
|
||||
|
@ -34,11 +34,23 @@ inline void writeChar(char x, WriteBuffer & buf)
|
||||
|
||||
/// Запись числа в native формате
|
||||
template <typename T>
|
||||
inline void writeIntBinary(T & x, WriteBuffer & buf)
|
||||
inline void writeBinary(T & x, WriteBuffer & buf)
|
||||
{
|
||||
buf.write(reinterpret_cast<const char *>(&x), sizeof(x));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void writeIntBinary(T & x, WriteBuffer & buf)
|
||||
{
|
||||
writeBinary(x, buf);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void writeFloatBinary(T & x, WriteBuffer & buf)
|
||||
{
|
||||
writeBinary(x, buf);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void writeIntText(T x, WriteBuffer & buf)
|
||||
@ -86,6 +98,20 @@ void writeFloatText(T x, WriteBuffer & buf, unsigned precision = WRITE_HELPERS_D
|
||||
buf.write(tmp, res);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void writeText(T x, WriteBuffer & buf);
|
||||
|
||||
template <> inline void writeText<UInt8> (UInt8 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<UInt16> (UInt16 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<UInt32> (UInt32 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<UInt64> (UInt64 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<Int8> (Int8 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<Int16> (Int16 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<Int32> (Int32 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<Int64> (Int64 x, WriteBuffer & buf) { writeIntText(x, buf); }
|
||||
template <> inline void writeText<Float32> (Float32 x, WriteBuffer & buf) { writeFloatText(x, buf); }
|
||||
template <> inline void writeText<Float64> (Float64 x, WriteBuffer & buf) { writeFloatText(x, buf); }
|
||||
|
||||
inline void writeString(const String & s, WriteBuffer & buf)
|
||||
{
|
||||
buf.write(s.data(), s.size());
|
||||
|
@ -32,11 +32,11 @@ int main(int argc, char ** argv)
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
{
|
||||
col1->getData()[i] = 1;
|
||||
col2->getData()[i] = 1;
|
||||
col1->getData()[i] = 10;
|
||||
col2->getData()[i] = 3;
|
||||
}
|
||||
|
||||
DB::FunctionPlus f;
|
||||
DB::FunctionDivideFloating f;
|
||||
DB::DataTypes arg_types;
|
||||
arg_types.push_back(descr1.type);
|
||||
arg_types.push_back(descr2.type);
|
||||
@ -73,9 +73,9 @@ int main(int argc, char ** argv)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
size_t x = 0;
|
||||
DB::Float64 x = 0;
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
x += boost::get<DB::Int64>((*block.getByPosition(2).column)[i]);
|
||||
x += boost::get<DB::Float64>((*block.getByPosition(2).column)[i]);
|
||||
|
||||
std::cout << x << std::endl;
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ int main(int argc, char ** argv)
|
||||
printType(DB::NumberTraits::ResultOfAdditionMultiplication<DB::UInt8, DB::UInt8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfAdditionMultiplication<DB::UInt8, DB::Int32>::Type());
|
||||
printType(DB::NumberTraits::ResultOfAdditionMultiplication<DB::UInt8, DB::Float32>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubstraction<DB::UInt8, DB::UInt8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubstraction<DB::UInt16, DB::UInt8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubstraction<DB::UInt16, DB::Int8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubtraction<DB::UInt8, DB::UInt8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubtraction<DB::UInt16, DB::UInt8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfSubtraction<DB::UInt16, DB::Int8>::Type());
|
||||
printType(DB::NumberTraits::ResultOfFloatingPointDivision<DB::UInt16, DB::Int16>::Type());
|
||||
printType(DB::NumberTraits::ResultOfFloatingPointDivision<DB::UInt32, DB::Int16>::Type());
|
||||
printType(DB::NumberTraits::ResultOfIntegerDivision<DB::UInt8, DB::Int16>::Type());
|
||||
|
Loading…
Reference in New Issue
Block a user