From dadf5ac4005e7116cdfe69f1c4b8415ceae222b3 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sat, 5 Aug 2017 04:54:38 +0300 Subject: [PATCH] Fixed function if of FixedString arguments [#CLICKHOUSE-3202]. --- dbms/src/Functions/FunctionsConditional.h | 257 ---------------------- dbms/src/Functions/GatherUtils.h | 12 +- 2 files changed, 6 insertions(+), 263 deletions(-) diff --git a/dbms/src/Functions/FunctionsConditional.h b/dbms/src/Functions/FunctionsConditional.h index 8955215b0a5..5391cb053f3 100644 --- a/dbms/src/Functions/FunctionsConditional.h +++ b/dbms/src/Functions/FunctionsConditional.h @@ -140,263 +140,6 @@ public: }; -struct StringIfImpl -{ - static void vector_vector( - const PaddedPODArray & cond, - const ColumnString::Chars_t & a_data, const ColumnString::Offsets_t & a_offsets, - const ColumnString::Chars_t & b_data, const ColumnString::Offsets_t & b_offsets, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - size_t size = cond.size(); - c_offsets.resize(size); - c_data.reserve(std::max(a_data.size(), b_data.size())); - - ColumnString::Offset_t a_prev_offset = 0; - ColumnString::Offset_t b_prev_offset = 0; - ColumnString::Offset_t c_prev_offset = 0; - - for (size_t i = 0; i < size; ++i) - { - if (cond[i]) - { - size_t size_to_write = a_offsets[i] - a_prev_offset; - c_data.resize(c_data.size() + size_to_write); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &a_data[a_prev_offset], size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - else - { - size_t size_to_write = b_offsets[i] - b_prev_offset; - c_data.resize(c_data.size() + size_to_write); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &b_data[b_prev_offset], size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - - a_prev_offset = a_offsets[i]; - b_prev_offset = b_offsets[i]; - } - } - - static void vector_fixed_vector_fixed( - const PaddedPODArray & cond, - const ColumnFixedString::Chars_t & a_data, - const ColumnFixedString::Chars_t & b_data, - const size_t N, - ColumnFixedString::Chars_t & c_data) - { - size_t size = cond.size(); - c_data.resize(a_data.size()); - - for (size_t i = 0; i < size; ++i) - { - if (cond[i]) - memcpySmallAllowReadWriteOverflow15(&c_data[i * N], &a_data[i * N], N); - else - memcpySmallAllowReadWriteOverflow15(&c_data[i * N], &b_data[i * N], N); - } - } - - template - static void vector_vector_fixed_impl( - const PaddedPODArray & cond, - const ColumnString::Chars_t & a_data, const ColumnString::Offsets_t & a_offsets, - const ColumnFixedString::Chars_t & b_data, const size_t b_N, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - size_t size = cond.size(); - c_offsets.resize(size); - c_data.reserve(std::max(a_data.size(), b_data.size() + size)); - - ColumnString::Offset_t a_prev_offset = 0; - ColumnString::Offset_t c_prev_offset = 0; - - for (size_t i = 0; i < size; ++i) - { - if (negative != cond[i]) - { - size_t size_to_write = a_offsets[i] - a_prev_offset; - c_data.resize(c_data.size() + size_to_write); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &a_data[a_prev_offset], size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - else - { - size_t size_to_write = b_N; - c_data.resize(c_data.size() + size_to_write + 1); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &b_data[i * b_N], size_to_write); - c_data.back() = 0; - c_prev_offset += size_to_write + 1; - c_offsets[i] = c_prev_offset; - } - - a_prev_offset = a_offsets[i]; - } - } - - static void vector_vector_fixed( - const PaddedPODArray & cond, - const ColumnString::Chars_t & a_data, const ColumnString::Offsets_t & a_offsets, - const ColumnFixedString::Chars_t & b_data, const size_t b_N, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - vector_vector_fixed_impl(cond, a_data, a_offsets, b_data, b_N, c_data, c_offsets); - } - - static void vector_fixed_vector( - const PaddedPODArray & cond, - const ColumnFixedString::Chars_t & a_data, const size_t a_N, - const ColumnString::Chars_t & b_data, const ColumnString::Offsets_t & b_offsets, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - vector_vector_fixed_impl(cond, b_data, b_offsets, a_data, a_N, c_data, c_offsets); - } - - template - static void vector_constant_impl( - const PaddedPODArray & cond, - const ColumnString::Chars_t & a_data, const ColumnString::Offsets_t & a_offsets, - const String & b, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - size_t size = cond.size(); - c_offsets.resize(size); - c_data.reserve(a_data.size()); - - ColumnString::Offset_t a_prev_offset = 0; - ColumnString::Offset_t c_prev_offset = 0; - - for (size_t i = 0; i < size; ++i) - { - if (negative != cond[i]) - { - size_t size_to_write = a_offsets[i] - a_prev_offset; - c_data.resize(c_data.size() + size_to_write); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &a_data[a_prev_offset], size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - else - { - size_t size_to_write = b.size() + 1; - c_data.resize(c_data.size() + size_to_write); - memcpy(&c_data[c_prev_offset], b.data(), size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - - a_prev_offset = a_offsets[i]; - } - } - - static void vector_constant( - const PaddedPODArray & cond, - const ColumnString::Chars_t & a_data, const ColumnString::Offsets_t & a_offsets, - const String & b, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - return vector_constant_impl(cond, a_data, a_offsets, b, c_data, c_offsets); - } - - static void constant_vector( - const PaddedPODArray & cond, - const String & a, - const ColumnString::Chars_t & b_data, const ColumnString::Offsets_t & b_offsets, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - return vector_constant_impl(cond, b_data, b_offsets, a, c_data, c_offsets); - } - - template - static void vector_fixed_constant_impl( - const PaddedPODArray & cond, - const ColumnFixedString::Chars_t & a_data, const size_t a_N, - const String & b, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - size_t size = cond.size(); - c_offsets.resize(size); - c_data.reserve(a_data.size()); - - ColumnString::Offset_t c_prev_offset = 0; - - for (size_t i = 0; i < size; ++i) - { - if (negative != cond[i]) - { - size_t size_to_write = a_N; - c_data.resize(c_data.size() + size_to_write + 1); - memcpySmallAllowReadWriteOverflow15(&c_data[c_prev_offset], &a_data[i * a_N], size_to_write); - c_data.back() = 0; - c_prev_offset += size_to_write + 1; - c_offsets[i] = c_prev_offset; - } - else - { - size_t size_to_write = b.size() + 1; - c_data.resize(c_data.size() + size_to_write); - memcpy(&c_data[c_prev_offset], b.data(), size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - } - } - - static void vector_fixed_constant( - const PaddedPODArray & cond, - const ColumnFixedString::Chars_t & a_data, const size_t N, - const String & b, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - vector_fixed_constant_impl(cond, a_data, N, b, c_data, c_offsets); - } - - static void constant_vector_fixed( - const PaddedPODArray & cond, - const String & a, - const ColumnFixedString::Chars_t & b_data, const size_t N, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - vector_fixed_constant_impl(cond, b_data, N, a, c_data, c_offsets); - } - - static void constant_constant( - const PaddedPODArray & cond, - const String & a, const String & b, - ColumnString::Chars_t & c_data, ColumnString::Offsets_t & c_offsets) - { - size_t size = cond.size(); - c_offsets.resize(size); - c_data.reserve((std::max(a.size(), b.size()) + 1) * size); - - ColumnString::Offset_t c_prev_offset = 0; - - for (size_t i = 0; i < size; ++i) - { - if (cond[i]) - { - size_t size_to_write = a.size() + 1; - c_data.resize(c_data.size() + size_to_write); - memcpy(&c_data[c_prev_offset], a.data(), size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - else - { - size_t size_to_write = b.size() + 1; - c_data.resize(c_data.size() + size_to_write); - memcpy(&c_data[c_prev_offset], b.data(), size_to_write); - c_prev_offset += size_to_write; - c_offsets[i] = c_prev_offset; - } - } - } -}; - - template struct NumArrayIfImpl { diff --git a/dbms/src/Functions/GatherUtils.h b/dbms/src/Functions/GatherUtils.h index 91805713e29..22329fe3bd4 100644 --- a/dbms/src/Functions/GatherUtils.h +++ b/dbms/src/Functions/GatherUtils.h @@ -424,7 +424,7 @@ struct IStringSource }; template -struct DynamicStringSource : IStringSource +struct DynamicStringSource final : IStringSource { Impl impl; @@ -564,7 +564,7 @@ struct GenericArraySink /// Methods to copy Slice to Sink, overloaded for various combinations of types. template -void writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) +void ALWAYS_INLINE writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) { sink.elements.resize(sink.current_offset + slice.size); memcpySmallAllowReadWriteOverflow15(&sink.elements[sink.current_offset], slice.data, slice.size * sizeof(T)); @@ -572,7 +572,7 @@ void writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) } template -void writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) +void ALWAYS_INLINE writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) { sink.elements.resize(sink.current_offset + slice.size); for (size_t i = 0; i < slice.size; ++i) @@ -582,20 +582,20 @@ void writeSlice(const NumericArraySlice & slice, NumericArraySink & sink) } } -inline void writeSlice(const StringSource::Slice & slice, StringSink & sink) +inline ALWAYS_INLINE void writeSlice(const StringSource::Slice & slice, StringSink & sink) { sink.elements.resize(sink.current_offset + slice.size); memcpySmallAllowReadWriteOverflow15(&sink.elements[sink.current_offset], slice.data, slice.size); sink.current_offset += slice.size; } -inline void writeSlice(const StringSource::Slice & slice, FixedStringSink & sink) +inline ALWAYS_INLINE void writeSlice(const StringSource::Slice & slice, FixedStringSink & sink) { memcpySmallAllowReadWriteOverflow15(&sink.elements[sink.current_offset], slice.data, slice.size); } /// Assuming same types of underlying columns for slice and sink. -inline void writeSlice(const GenericArraySlice & slice, GenericArraySink & sink) +inline ALWAYS_INLINE void writeSlice(const GenericArraySlice & slice, GenericArraySink & sink) { sink.elements.insertRangeFrom(*slice.elements, slice.begin, slice.size); sink.current_offset += slice.size;