Skip to content

Commit

Permalink
Generic constructor of ColumnVector from initializer list (#9393)
Browse files Browse the repository at this point in the history
* Generic constructor of ColumnVector from initializer list #9367

* Fixed build

* Fixed error
  • Loading branch information
alexey-milovidov committed Feb 28, 2020
1 parent c4fc8e4 commit 6691302
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion dbms/src/Columns/ColumnVector.h
Expand Up @@ -114,7 +114,8 @@ class ColumnVector final : public COWHelper<ColumnVectorHelper, ColumnVector<T>>
ColumnVector(const ColumnVector & src) : data(src.data.begin(), src.data.end()) {}

/// Sugar constructor.
ColumnVector(std::initializer_list<T> il) : data{il} {}
template <typename U>
ColumnVector(std::initializer_list<U> il) : data{il} {}

public:
bool isNumeric() const override { return IsNumber<T>; }
Expand Down
39 changes: 28 additions & 11 deletions dbms/src/Common/PODArray.h
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <algorithm>
#include <memory>
#include <iterator>

#include <boost/noncopyable.hpp>
#include <boost/iterator_adaptors.hpp>
Expand Down Expand Up @@ -303,13 +304,17 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
assign(n, x);
}

PODArray(const_iterator from_begin, const_iterator from_end)
template <typename It1, typename It2,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types
PODArray(It1 from_begin, It2 from_end)
{
this->alloc_for_num_elements(from_end - from_begin);
insert(from_begin, from_end);
}

PODArray(std::initializer_list<T> il) : PODArray(std::begin(il), std::end(il)) {}
template <typename U>
PODArray(std::initializer_list<U> il) : PODArray(std::begin(il), std::end(il)) {}

PODArray(PODArray && other)
{
Expand Down Expand Up @@ -403,7 +408,9 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
}

/// Do not insert into the array a piece of itself. Because with the resize, the iterators on themselves can be invalidated.
template <typename It1, typename It2, typename ... TAllocatorParams>
template <typename It1, typename It2, typename ... TAllocatorParams,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types>
void insertPrepare(It1 from_begin, It2 from_end, TAllocatorParams &&... allocator_params)
{
size_t required_capacity = this->size() + (from_end - from_begin);
Expand All @@ -412,28 +419,34 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
}

/// Do not insert into the array a piece of itself. Because with the resize, the iterators on themselves can be invalidated.
template <typename It1, typename It2, typename ... TAllocatorParams>
template <typename It1, typename It2, typename ... TAllocatorParams,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types
void insert(It1 from_begin, It2 from_end, TAllocatorParams &&... allocator_params)
{
insertPrepare(from_begin, from_end, std::forward<TAllocatorParams>(allocator_params)...);
insert_assume_reserved(from_begin, from_end);
}

/// Works under assumption, that it's possible to read up to 15 excessive bytes after `from_end` and this PODArray is padded.
template <typename It1, typename It2, typename ... TAllocatorParams>
template <typename It1, typename It2, typename ... TAllocatorParams,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for types with operator*
void insertSmallAllowReadWriteOverflow15(It1 from_begin, It2 from_end, TAllocatorParams &&... allocator_params)
{
static_assert(pad_right_ >= 15);
insertPrepare(from_begin, from_end, std::forward<TAllocatorParams>(allocator_params)...);
size_t bytes_to_copy = this->byte_size(from_end - from_begin);
size_t bytes_to_copy = (from_end - from_begin) * sizeof(*from_begin);
memcpySmallAllowReadWriteOverflow15(this->c_end, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
this->c_end += bytes_to_copy;
}

template <typename It1, typename It2>
template <typename It1, typename It2,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types
void insert(iterator it, It1 from_begin, It2 from_end)
{
size_t bytes_to_copy = this->byte_size(from_end - from_begin);
size_t bytes_to_copy = (from_end - from_begin) * sizeof(*from_begin);
size_t bytes_to_move = (end() - it) * sizeof(T);

insertPrepare(from_begin, from_end);
Expand All @@ -445,10 +458,12 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
this->c_end += bytes_to_copy;
}

template <typename It1, typename It2>
template <typename It1, typename It2,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types
void insert_assume_reserved(It1 from_begin, It2 from_end)
{
size_t bytes_to_copy = this->byte_size(from_end - from_begin);
size_t bytes_to_copy = (from_end - from_begin) * sizeof(*from_begin);
memcpy(this->c_end, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
this->c_end += bytes_to_copy;
}
Expand Down Expand Up @@ -577,7 +592,9 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
std::fill(begin(), end(), x);
}

template <typename It1, typename It2, typename... TAllocatorParams>
template <typename It1, typename It2, typename... TAllocatorParams,
typename std::iterator_traits<It1>::pointer * = nullptr,
typename std::iterator_traits<It2>::pointer * = nullptr> /// Only for iterator-like types
void assign(It1 from_begin, It2 from_end, TAllocatorParams &&... allocator_params)
{
size_t required_capacity = from_end - from_begin;
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/DataStreams/tests/collapsing_sorted_stream.cpp
Expand Up @@ -30,13 +30,13 @@ try
ColumnWithTypeAndName column1;
column1.name = "Sign";
column1.type = std::make_shared<DataTypeInt8>();
column1.column = ColumnInt8::create({static_cast<int8_t>(1), static_cast<int8_t>(-1)});
column1.column = ColumnInt8::create({1, -1});
block1.insert(column1);

ColumnWithTypeAndName column2;
column2.name = "CounterID";
column2.type = std::make_shared<DataTypeUInt32>();
column2.column = ColumnUInt32::create({static_cast<uint32_t>(123), static_cast<uint32_t>(123)});
column2.column = ColumnUInt32::create({123, 123});
block1.insert(column2);
}

Expand All @@ -46,13 +46,13 @@ try
ColumnWithTypeAndName column1;
column1.name = "Sign";
column1.type = std::make_shared<DataTypeInt8>();
column1.column = ColumnInt8::create({static_cast<int8_t>(1), static_cast<int8_t>(1)});
column1.column = ColumnInt8::create({1, 1});
block2.insert(column1);

ColumnWithTypeAndName column2;
column2.name = "CounterID";
column2.type = std::make_shared<DataTypeUInt32>();
column2.column = ColumnUInt32::create({static_cast<uint32_t>(123), static_cast<uint32_t>(456)});
column2.column = ColumnUInt32::create({123, 456});
block2.insert(column2);
}

Expand Down

0 comments on commit 6691302

Please sign in to comment.