diff --git a/src/Functions/byteSize.cpp b/src/Functions/byteSize.cpp index 86ea5a1a5dc..1bc9cb0a0c2 100644 --- a/src/Functions/byteSize.cpp +++ b/src/Functions/byteSize.cpp @@ -25,12 +25,17 @@ namespace ErrorCodes extern const int ILLEGAL_COLUMN; } +template <> struct NativeType { using Type = DataTypeDate::FieldType; }; +template <> struct NativeType { using Type = DataTypeDateTime::FieldType; }; +template <> struct NativeType { using Type = NativeType::Type; }; +template <> struct NativeType { using Type = DataTypeUUID::FieldType; }; +template <> struct NativeType { using Type = DataTypeEnum8::FieldType; }; +template <> struct NativeType { using Type = DataTypeEnum16::FieldType; }; + namespace { template struct ByteSizeForNative { static constexpr const UInt64 value = sizeof(typename NativeType::Type); }; -template struct ByteSizeForNumberBase { static constexpr const UInt64 value = sizeof(typename NativeType::Type); }; -template struct ByteSizeForEnum { static constexpr const UInt64 value = sizeof(typename T::FieldType); }; /** byteSize() - get the columns size in number of bytes. */ @@ -68,6 +73,12 @@ public: return std::make_shared(); } + // TODO: + // case TypeIndex::Tuple: return "Tuple"; + // case TypeIndex::Nullable: return "Nullable"; + // case TypeIndex::Function: return "Function"; + // case TypeIndex::AggregateFunction: return "AggregateFunction"; + // case TypeIndex::LowCardinality: return "LowCardinality"; ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override { auto result_col = ColumnUInt64::create(input_rows_count, 0); @@ -76,8 +87,6 @@ public: { const ColumnPtr & column = arg.column; const IDataType * data_type = arg.type.get(); - WhichDataType which(data_type); - byteSizeOne(data_type, column, vec_res); } return result_col; @@ -101,6 +110,22 @@ public: } } + static bool byteSizeByDataType(const IDataType * data_type, UInt64 & byte_size) + { + TypeIndex type_id = data_type->getTypeId(); + if (byteSizeByTypeId(type_id, byte_size)) + return true; + + switch (type_id) + { + case TypeIndex::FixedString: + byte_size = typeid_cast(data_type)->getN(); + break; + default: return false; + } + return true; + } + static bool byteSizeByTypeId(TypeIndex type_id, UInt64 & byte_size) { switch (type_id) @@ -120,55 +145,31 @@ public: case TypeIndex::Int256: byte_size = ByteSizeForNative::value; break; case TypeIndex::Float32: byte_size = ByteSizeForNative::value; break; case TypeIndex::Float64: byte_size = ByteSizeForNative::value; break; - case TypeIndex::Date: byte_size = ByteSizeForNumberBase::value; break; - case TypeIndex::DateTime: byte_size = ByteSizeForNumberBase::value; break; - case TypeIndex::DateTime64: byte_size = ByteSizeForNumberBase::value; break; -// case TypeIndex::String: return TypeName::get(); -// case TypeIndex::FixedString: - case TypeIndex::Enum8: byte_size = ByteSizeForEnum::value; break; - case TypeIndex::Enum16: byte_size = ByteSizeForEnum::value; break; + case TypeIndex::Date: byte_size = ByteSizeForNative::value; break; + case TypeIndex::DateTime: byte_size = ByteSizeForNative::value; break; + case TypeIndex::DateTime64: byte_size = ByteSizeForNative::value; break; + case TypeIndex::Enum8: byte_size = ByteSizeForNative::value; break; + case TypeIndex::Enum16: byte_size = ByteSizeForNative::value; break; case TypeIndex::Decimal32: byte_size = ByteSizeForNative::value; break; case TypeIndex::Decimal64: byte_size = ByteSizeForNative::value; break; case TypeIndex::Decimal128: byte_size = ByteSizeForNative::value; break; case TypeIndex::Decimal256: byte_size = ByteSizeForNative::value; break; - case TypeIndex::UUID: byte_size = ByteSizeForNumberBase::value; break; -// case TypeIndex::Array: return "Array"; -// case TypeIndex::Tuple: return "Tuple"; -// case TypeIndex::Set: return "Set"; -// case TypeIndex::Interval: byte_size = ByteSizeForNumberBase::value; break; -// case TypeIndex::Nullable: return "Nullable"; -// case TypeIndex::Function: return "Function"; -// case TypeIndex::AggregateFunction: return "AggregateFunction"; -// case TypeIndex::LowCardinality: return "LowCardinality"; + case TypeIndex::UUID: byte_size = ByteSizeForNative::value; break; + // case TypeIndex::Interval: internal use only. + // case TypeIndex::Set: internal use only. default: return false; } return true; } - static bool byteSizeByDataType(const IDataType * data_type, UInt64 & byte_size) - { - TypeIndex type_id = data_type->getTypeId(); - if (byteSizeByTypeId(type_id, byte_size)) - return true; - - switch (type_id) - { - case TypeIndex::FixedString: - byte_size = typeid_cast(data_type)->getN(); - break; - default: return false; - } - return true; - } - #define INTEGRAL_TPL_PACK UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, \ Int8, Int16, Int32, Int64, Int128, Int256, \ Float32, Float64 // The following is not supported by ColumnVector: // Decimal32, Decimal64, Decimal128, Decimal256 // DataTypeEnum8, DataTypeEnum16, DataTypeDate, DataTypeDateTime, DataTypeDateTime64 - // DataTypeUUID, DataTypeInterval + // DataTypeUUID static bool byteSizeByColumn(const IDataType * data_type, const ColumnPtr & column, ColumnUInt64::Container & vec_res) { @@ -201,17 +202,6 @@ public: return false; } - static inline bool byteSizeForConstArray(const IDataType * /*data_type*/, const ColumnPtr & column, ColumnUInt64::Container & vec_res) { - const ColumnConst * col_arr = checkAndGetColumnConst(column.get()); - if (!col_arr) - return false; - size_t vec_size = vec_res.size(); - const UInt64 byte_size = col_arr->byteSize(); - for (size_t i = 0; i < vec_size; ++i) - vec_res[i] += byte_size; - return true; - } - #undef INTEGRAL_TPL_PACK template @@ -243,6 +233,17 @@ public: return true; } + static inline bool byteSizeForConstArray(const IDataType * /*data_type*/, const ColumnPtr & column, ColumnUInt64::Container & vec_res) { + const ColumnConst * col_arr = checkAndGetColumnConst(column.get()); + if (!col_arr) + return false; + size_t vec_size = vec_res.size(); + const UInt64 byte_size = col_arr->byteSize(); + for (size_t i = 0; i < vec_size; ++i) + vec_res[i] += byte_size; + return true; + } + static bool byteSizeForStringArray(const ColumnArray * col_arr, const ColumnPtr & /*column*/, ColumnUInt64::Container & vec_res) { const ColumnString * col_nested = checkAndGetColumn(&(col_arr->getData()));