2019-03-30 21:30:21 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-14 14:39:03 +00:00
|
|
|
#include <Common/Arena.h>
|
2019-03-30 21:30:21 +00:00
|
|
|
#include <Columns/IColumn.h>
|
2019-10-11 17:56:26 +00:00
|
|
|
#include <Interpreters/asof.h>
|
2019-03-30 21:30:21 +00:00
|
|
|
|
|
|
|
#include <optional>
|
2019-04-01 16:44:15 +00:00
|
|
|
#include <variant>
|
2019-04-02 18:50:35 +00:00
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
2019-04-05 17:59:48 +00:00
|
|
|
#include <algorithm>
|
2019-03-30 21:30:21 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Block;
|
|
|
|
|
|
|
|
/// Reference to the row in block.
|
|
|
|
struct RowRef
|
|
|
|
{
|
2020-04-21 19:01:34 +00:00
|
|
|
using SizeT = uint32_t; /// Do not use size_t cause of memory economy
|
|
|
|
|
2019-03-30 21:30:21 +00:00
|
|
|
const Block * block = nullptr;
|
2020-04-21 19:01:34 +00:00
|
|
|
SizeT row_num = 0;
|
2019-03-30 21:30:21 +00:00
|
|
|
|
|
|
|
RowRef() {}
|
|
|
|
RowRef(const Block * block_, size_t row_num_) : block(block_), row_num(row_num_) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Single linked list of references to rows. Used for ALL JOINs (non-unique JOINs)
|
|
|
|
struct RowRefList : RowRef
|
|
|
|
{
|
2019-05-14 14:40:43 +00:00
|
|
|
/// Portion of RowRefs, 16 * (MAX_SIZE + 1) bytes sized.
|
|
|
|
struct Batch
|
|
|
|
{
|
|
|
|
static constexpr size_t MAX_SIZE = 7; /// Adequate values are 3, 7, 15, 31.
|
|
|
|
|
2020-04-21 19:01:34 +00:00
|
|
|
SizeT size = 0; /// It's smaller than size_t but keeps align in Arena.
|
2019-05-14 14:40:43 +00:00
|
|
|
Batch * next;
|
|
|
|
RowRef row_refs[MAX_SIZE];
|
|
|
|
|
|
|
|
Batch(Batch * parent)
|
|
|
|
: next(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool full() const { return size == MAX_SIZE; }
|
|
|
|
|
|
|
|
Batch * insert(RowRef && row_ref, Arena & pool)
|
|
|
|
{
|
|
|
|
if (full())
|
|
|
|
{
|
|
|
|
auto batch = pool.alloc<Batch>();
|
|
|
|
*batch = Batch(this);
|
|
|
|
batch->insert(std::move(row_ref), pool);
|
|
|
|
return batch;
|
|
|
|
}
|
|
|
|
|
|
|
|
row_refs[size++] = std::move(row_ref);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-14 14:39:03 +00:00
|
|
|
class ForwardIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ForwardIterator(const RowRefList * begin)
|
2019-05-14 14:40:43 +00:00
|
|
|
: root(begin)
|
|
|
|
, first(true)
|
|
|
|
, batch(root->next)
|
|
|
|
, position(0)
|
2019-05-14 14:39:03 +00:00
|
|
|
{}
|
|
|
|
|
2019-05-14 14:40:43 +00:00
|
|
|
const RowRef * operator -> () const
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
return root;
|
|
|
|
return &batch->row_refs[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator ++ ()
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batch)
|
|
|
|
{
|
|
|
|
++position;
|
|
|
|
if (position >= batch->size)
|
|
|
|
{
|
|
|
|
batch = batch->next;
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok() const { return first || (batch && position < batch->size); }
|
2019-05-14 14:39:03 +00:00
|
|
|
|
|
|
|
private:
|
2019-05-14 14:40:43 +00:00
|
|
|
const RowRefList * root;
|
|
|
|
bool first;
|
|
|
|
Batch * batch;
|
|
|
|
size_t position;
|
2019-05-14 14:39:03 +00:00
|
|
|
};
|
2019-03-30 21:30:21 +00:00
|
|
|
|
|
|
|
RowRefList() {}
|
|
|
|
RowRefList(const Block * block_, size_t row_num_) : RowRef(block_, row_num_) {}
|
2019-05-14 14:39:03 +00:00
|
|
|
|
|
|
|
ForwardIterator begin() const { return ForwardIterator(this); }
|
|
|
|
|
|
|
|
/// insert element after current one
|
|
|
|
void insert(RowRef && row_ref, Arena & pool)
|
|
|
|
{
|
2019-05-14 14:40:43 +00:00
|
|
|
if (!next)
|
|
|
|
{
|
|
|
|
next = pool.alloc<Batch>();
|
|
|
|
*next = Batch(nullptr);
|
|
|
|
}
|
|
|
|
next = next->insert(std::move(row_ref), pool);
|
2019-05-14 14:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-14 14:40:43 +00:00
|
|
|
Batch * next = nullptr;
|
2019-03-30 21:30:21 +00:00
|
|
|
};
|
|
|
|
|
2019-04-05 17:59:48 +00:00
|
|
|
/**
|
|
|
|
* This class is intended to push sortable data into.
|
|
|
|
* When looking up values the container ensures that it is sorted for log(N) lookup
|
|
|
|
* After calling any of the lookup methods, it is no longer allowed to insert more data as this would invalidate the
|
|
|
|
* references that can be returned by the lookup methods
|
|
|
|
*/
|
|
|
|
|
2019-04-25 01:16:26 +00:00
|
|
|
template <typename TEntry, typename TKey>
|
2019-04-05 17:59:48 +00:00
|
|
|
class SortedLookupVector
|
|
|
|
{
|
|
|
|
public:
|
2019-04-25 01:16:26 +00:00
|
|
|
using Base = std::vector<TEntry>;
|
2019-04-05 17:59:48 +00:00
|
|
|
|
|
|
|
// First stage, insertions into the vector
|
|
|
|
template <typename U, typename ... TAllocatorParams>
|
|
|
|
void insert(U && x, TAllocatorParams &&... allocator_params)
|
|
|
|
{
|
|
|
|
assert(!sorted.load(std::memory_order_acquire));
|
|
|
|
array.push_back(std::forward<U>(x), std::forward<TAllocatorParams>(allocator_params)...);
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:56:26 +00:00
|
|
|
const RowRef * upperBound(const TEntry & k, bool ascending)
|
2019-04-05 17:59:48 +00:00
|
|
|
{
|
2019-10-11 17:56:26 +00:00
|
|
|
sort(ascending);
|
2019-10-15 15:49:00 +00:00
|
|
|
auto it = std::upper_bound(array.cbegin(), array.cend(), k, (ascending ? less : greater));
|
2019-10-11 17:56:26 +00:00
|
|
|
if (it != array.cend())
|
|
|
|
return &(it->row_ref);
|
|
|
|
return nullptr;
|
2019-04-05 17:59:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:56:26 +00:00
|
|
|
const RowRef * lowerBound(const TEntry & k, bool ascending)
|
|
|
|
{
|
|
|
|
sort(ascending);
|
2019-10-15 15:49:00 +00:00
|
|
|
auto it = std::lower_bound(array.cbegin(), array.cend(), k, (ascending ? less : greater));
|
2019-10-11 17:56:26 +00:00
|
|
|
if (it != array.cend())
|
|
|
|
return &(it->row_ref);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-05 17:59:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<bool> sorted = false;
|
2019-04-05 20:22:57 +00:00
|
|
|
Base array;
|
2019-04-05 17:59:48 +00:00
|
|
|
mutable std::mutex lock;
|
|
|
|
|
2019-10-11 17:56:26 +00:00
|
|
|
static bool less(const TEntry & a, const TEntry & b)
|
2019-04-25 01:16:26 +00:00
|
|
|
{
|
2019-10-15 15:49:00 +00:00
|
|
|
return a.asof_value < b.asof_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool greater(const TEntry & a, const TEntry & b)
|
|
|
|
{
|
|
|
|
return a.asof_value > b.asof_value;
|
2019-10-11 17:56:26 +00:00
|
|
|
}
|
2019-04-25 01:16:26 +00:00
|
|
|
|
2019-04-05 17:59:48 +00:00
|
|
|
// Double checked locking with SC atomics works in C++
|
|
|
|
// https://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/
|
|
|
|
// The first thread that calls one of the lookup methods sorts the data
|
|
|
|
// After calling the first lookup method it is no longer allowed to insert any data
|
|
|
|
// the array becomes immutable
|
2019-10-11 17:56:26 +00:00
|
|
|
void sort(bool ascending)
|
2019-04-05 17:59:48 +00:00
|
|
|
{
|
2019-04-05 18:05:24 +00:00
|
|
|
if (!sorted.load(std::memory_order_acquire))
|
|
|
|
{
|
2019-04-05 17:59:48 +00:00
|
|
|
std::lock_guard<std::mutex> l(lock);
|
2019-04-05 18:05:24 +00:00
|
|
|
if (!sorted.load(std::memory_order_relaxed))
|
|
|
|
{
|
2019-04-25 01:16:26 +00:00
|
|
|
if (!array.empty())
|
2019-10-15 15:49:00 +00:00
|
|
|
std::sort(array.begin(), array.end(), (ascending ? less : greater));
|
2019-04-15 14:09:39 +00:00
|
|
|
|
2019-04-05 17:59:48 +00:00
|
|
|
sorted.store(true, std::memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-30 21:30:21 +00:00
|
|
|
class AsofRowRefs
|
|
|
|
{
|
|
|
|
public:
|
2019-04-02 16:22:14 +00:00
|
|
|
template <typename T>
|
2019-03-30 21:30:21 +00:00
|
|
|
struct Entry
|
|
|
|
{
|
2019-04-12 12:48:00 +00:00
|
|
|
using LookupType = SortedLookupVector<Entry<T>, T>;
|
2019-04-05 17:59:48 +00:00
|
|
|
using LookupPtr = std::unique_ptr<LookupType>;
|
2019-03-30 21:30:21 +00:00
|
|
|
T asof_value;
|
|
|
|
RowRef row_ref;
|
|
|
|
|
|
|
|
Entry(T v) : asof_value(v) {}
|
|
|
|
Entry(T v, RowRef rr) : asof_value(v), row_ref(rr) {}
|
|
|
|
};
|
|
|
|
|
2019-04-01 16:44:15 +00:00
|
|
|
using Lookups = std::variant<
|
2019-04-05 17:59:48 +00:00
|
|
|
Entry<UInt32>::LookupPtr,
|
|
|
|
Entry<UInt64>::LookupPtr,
|
2019-05-02 23:46:04 +00:00
|
|
|
Entry<Int32>::LookupPtr,
|
|
|
|
Entry<Int64>::LookupPtr,
|
2019-04-05 17:59:48 +00:00
|
|
|
Entry<Float32>::LookupPtr,
|
2019-11-20 09:00:10 +00:00
|
|
|
Entry<Float64>::LookupPtr,
|
|
|
|
Entry<Decimal32>::LookupPtr,
|
|
|
|
Entry<Decimal64>::LookupPtr,
|
|
|
|
Entry<Decimal128>::LookupPtr>;
|
2019-03-30 21:30:21 +00:00
|
|
|
|
2019-04-01 16:44:15 +00:00
|
|
|
enum class Type
|
|
|
|
{
|
2019-05-02 23:46:04 +00:00
|
|
|
keyu32,
|
|
|
|
keyu64,
|
|
|
|
keyi32,
|
|
|
|
keyi64,
|
2019-04-01 16:44:15 +00:00
|
|
|
keyf32,
|
|
|
|
keyf64,
|
2019-11-20 09:00:10 +00:00
|
|
|
keyDecimal32,
|
|
|
|
keyDecimal64,
|
|
|
|
keyDecimal128,
|
2019-03-30 21:30:21 +00:00
|
|
|
};
|
|
|
|
|
2019-04-05 17:59:48 +00:00
|
|
|
AsofRowRefs() {}
|
|
|
|
AsofRowRefs(Type t);
|
|
|
|
|
2019-04-01 16:44:15 +00:00
|
|
|
static std::optional<Type> getTypeSize(const IColumn * asof_column, size_t & type_size);
|
|
|
|
|
2019-04-05 17:59:48 +00:00
|
|
|
// This will be synchronized by the rwlock mutex in Join.h
|
|
|
|
void insert(Type type, const IColumn * asof_column, const Block * block, size_t row_num);
|
|
|
|
|
|
|
|
// This will internally synchronize
|
2019-10-11 17:56:26 +00:00
|
|
|
const RowRef * findAsof(Type type, ASOF::Inequality inequality, const IColumn * asof_column, size_t row_num) const;
|
2019-03-30 21:30:21 +00:00
|
|
|
|
|
|
|
private:
|
2019-04-05 17:59:48 +00:00
|
|
|
// Lookups can be stored in a HashTable because it is memmovable
|
|
|
|
// A std::variant contains a currently active type id (memmovable), together with a union of the types
|
|
|
|
// The types are all std::unique_ptr, which contains a single pointer, which is memmovable.
|
2019-09-23 16:18:19 +00:00
|
|
|
// Source: https://github.com/ClickHouse/ClickHouse/issues/4906
|
2019-04-05 17:59:48 +00:00
|
|
|
Lookups lookups;
|
2019-03-30 21:30:21 +00:00
|
|
|
};
|
|
|
|
|
2019-03-31 10:56:54 +00:00
|
|
|
}
|