2021-03-24 16:31:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <common/types.h>
|
|
|
|
#include <Common/PODArray.h>
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
2021-03-25 13:23:19 +00:00
|
|
|
#include <Common/HashTable/HashSet.h>
|
2021-03-24 16:31:00 +00:00
|
|
|
|
|
|
|
#include <Columns/IColumn.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
|
|
|
|
#include <Dictionaries/IDictionary.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
namespace detail
|
2021-03-24 16:31:00 +00:00
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
template <typename KeyType>
|
|
|
|
struct ElementsAndOffsets
|
|
|
|
{
|
|
|
|
PaddedPODArray<KeyType> elements;
|
|
|
|
PaddedPODArray<IColumn::Offset> offsets;
|
|
|
|
};
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
template <typename T>
|
|
|
|
struct IsKeyValidFuncInterface
|
|
|
|
{
|
|
|
|
bool operator()(T key [[maybe_unused]]) { return false; }
|
|
|
|
};
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
template <typename T>
|
|
|
|
struct GetParentKeyFuncInterface
|
|
|
|
{
|
|
|
|
std::optional<T> operator()(T key [[maybe_unused]]) { return {}; }
|
|
|
|
};
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
template <typename KeyType, typename IsKeyValidFunc, typename GetParentKeyFunc>
|
|
|
|
ElementsAndOffsets<KeyType> getKeysHierarchy(
|
|
|
|
const PaddedPODArray<KeyType> & hierarchy_keys,
|
|
|
|
const KeyType & hierarchy_null_value,
|
|
|
|
IsKeyValidFunc && is_key_valid_func,
|
|
|
|
GetParentKeyFunc && get_key_func)
|
|
|
|
{
|
|
|
|
size_t hierarchy_keys_size = hierarchy_keys.size();
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
PaddedPODArray<KeyType> elements;
|
|
|
|
elements.reserve(hierarchy_keys_size);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
PaddedPODArray<IColumn::Offset> offsets;
|
|
|
|
offsets.reserve(hierarchy_keys_size);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
struct OffsetInArray
|
|
|
|
{
|
|
|
|
size_t offset_index;
|
|
|
|
size_t array_element_offset;
|
|
|
|
};
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
HashMap<KeyType, OffsetInArray> already_processes_keys_to_offset;
|
|
|
|
already_processes_keys_to_offset.reserve(hierarchy_keys_size);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
for (size_t i = 0; i < hierarchy_keys_size; ++i)
|
|
|
|
{
|
|
|
|
auto hierarchy_key = hierarchy_keys[i];
|
|
|
|
size_t current_hierarchy_depth = 0;
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
bool is_key_valid = std::forward<IsKeyValidFunc>(is_key_valid_func)(hierarchy_key);
|
|
|
|
|
|
|
|
if (!is_key_valid)
|
|
|
|
{
|
|
|
|
offsets.emplace_back(elements.size());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const auto * it = already_processes_keys_to_offset.find(hierarchy_key);
|
|
|
|
|
|
|
|
if (it)
|
|
|
|
{
|
|
|
|
const auto & index = it->getMapped();
|
|
|
|
|
|
|
|
size_t offset = index.offset_index;
|
|
|
|
|
|
|
|
bool is_loop = (offset == offsets.size());
|
|
|
|
|
|
|
|
if (unlikely(is_loop))
|
|
|
|
break;
|
|
|
|
|
|
|
|
size_t array_element_offset = index.array_element_offset;
|
|
|
|
|
|
|
|
size_t previous_offset_size = offset > 0 ? offsets[offset - 1] : 0;
|
|
|
|
size_t start_index = previous_offset_size + array_element_offset;
|
|
|
|
size_t end_index = offsets[offset];
|
|
|
|
|
|
|
|
current_hierarchy_depth += end_index - start_index;
|
|
|
|
|
|
|
|
/// TODO: Insert part of pod array into itself
|
|
|
|
while (start_index < end_index)
|
|
|
|
{
|
|
|
|
elements.emplace_back(elements[start_index]);
|
|
|
|
++start_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hierarchy_key == hierarchy_null_value || current_hierarchy_depth >= DBMS_HIERARCHICAL_DICTIONARY_MAX_DEPTH)
|
|
|
|
break;
|
|
|
|
|
|
|
|
already_processes_keys_to_offset[hierarchy_key] = {offsets.size(), current_hierarchy_depth};
|
|
|
|
elements.emplace_back(hierarchy_key);
|
|
|
|
++current_hierarchy_depth;
|
|
|
|
|
|
|
|
std::optional<KeyType> parent_key = std::forward<GetParentKeyFunc>(get_key_func)(hierarchy_key);
|
|
|
|
|
|
|
|
if (!parent_key.has_value())
|
|
|
|
break;
|
|
|
|
|
|
|
|
hierarchy_key = *parent_key;
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
|
|
|
offsets.emplace_back(elements.size());
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
ElementsAndOffsets<KeyType> result = {std::move(elements), std::move(offsets)};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GetAllDescendantsStrategy { size_t level = 0; };
|
|
|
|
struct GetDescendantsAtSpecificLevelStrategy { size_t level = 0; };
|
|
|
|
|
|
|
|
template <typename KeyType, typename Strategy>
|
|
|
|
ElementsAndOffsets<KeyType> getDescendants(
|
|
|
|
const PaddedPODArray<KeyType> & requested_keys,
|
|
|
|
const HashMap<KeyType, PaddedPODArray<KeyType>> & parent_to_child,
|
|
|
|
Strategy strategy)
|
|
|
|
{
|
|
|
|
PaddedPODArray<KeyType> descendants;
|
|
|
|
descendants.reserve(requested_keys.size());
|
|
|
|
|
|
|
|
PaddedPODArray<size_t> descendants_offsets;
|
|
|
|
descendants_offsets.reserve(requested_keys.size());
|
|
|
|
|
|
|
|
struct Range
|
|
|
|
{
|
|
|
|
size_t start_index;
|
|
|
|
size_t end_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr Int64 key_range_requires_update = -1;
|
|
|
|
HashMap<KeyType, Range> already_processed_keys_to_range [[maybe_unused]];
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<Strategy, GetAllDescendantsStrategy>)
|
|
|
|
already_processed_keys_to_range.reserve(requested_keys.size());
|
|
|
|
|
|
|
|
struct KeyAndDepth
|
2021-03-24 16:31:00 +00:00
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
KeyType key;
|
|
|
|
Int64 depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
HashSet<KeyType> already_processed_keys_during_loop;
|
|
|
|
already_processed_keys_during_loop.reserve(requested_keys.size());
|
|
|
|
|
|
|
|
PaddedPODArray<KeyAndDepth> next_keys_to_process_stack;
|
|
|
|
next_keys_to_process_stack.reserve(requested_keys.size());
|
|
|
|
|
|
|
|
Int64 level = static_cast<Int64>(strategy.level);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < requested_keys.size(); ++i)
|
|
|
|
{
|
|
|
|
const KeyType & requested_key = requested_keys[i];
|
|
|
|
|
|
|
|
if (parent_to_child.find(requested_key) == nullptr)
|
|
|
|
{
|
|
|
|
descendants_offsets.emplace_back(descendants.size());
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
next_keys_to_process_stack.emplace_back(KeyAndDepth{requested_key, 0});
|
|
|
|
|
|
|
|
while (!next_keys_to_process_stack.empty())
|
2021-03-24 16:31:00 +00:00
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
KeyAndDepth key_to_process = next_keys_to_process_stack.back();
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
KeyType key = key_to_process.key;
|
|
|
|
Int64 depth = key_to_process.depth;
|
|
|
|
next_keys_to_process_stack.pop_back();
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if constexpr (std::is_same_v<Strategy, GetAllDescendantsStrategy>)
|
|
|
|
{
|
|
|
|
if (depth == key_range_requires_update)
|
|
|
|
{
|
|
|
|
auto * it = already_processed_keys_to_range.find(key);
|
|
|
|
assert(it);
|
|
|
|
|
|
|
|
auto & range_to_update = it->getMapped();
|
|
|
|
range_to_update.end_index = descendants.size();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if (unlikely(already_processed_keys_during_loop.find(key) != nullptr))
|
|
|
|
{
|
|
|
|
next_keys_to_process_stack.clear();
|
2021-03-24 16:31:00 +00:00
|
|
|
break;
|
2021-03-25 13:23:19 +00:00
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if constexpr (std::is_same_v<Strategy, GetAllDescendantsStrategy>)
|
|
|
|
{
|
|
|
|
const auto * already_processed_it = already_processed_keys_to_range.find(key);
|
|
|
|
|
|
|
|
if (already_processed_it)
|
|
|
|
{
|
|
|
|
Range range = already_processed_it->getMapped();
|
|
|
|
|
|
|
|
if (unlikely(range.start_index > range.end_index))
|
|
|
|
{
|
|
|
|
/// Broken range because there was loop
|
|
|
|
already_processed_keys_to_range.erase(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// TODO: Insert part of pod array
|
|
|
|
while (range.start_index != range.end_index)
|
|
|
|
{
|
|
|
|
descendants.emplace_back(descendants[range.start_index]);
|
|
|
|
++range.start_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
const auto * it = parent_to_child.find(key);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if (!it || depth >= DBMS_HIERARCHICAL_DICTIONARY_MAX_DEPTH)
|
|
|
|
continue;
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if constexpr (std::is_same_v<Strategy, GetDescendantsAtSpecificLevelStrategy>)
|
2021-03-24 16:31:00 +00:00
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
if (depth > level)
|
|
|
|
continue;
|
2021-03-24 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
if constexpr (std::is_same_v<Strategy, GetAllDescendantsStrategy>)
|
|
|
|
{
|
|
|
|
size_t range_start_index = descendants.size();
|
|
|
|
already_processed_keys_to_range[key].start_index = range_start_index;
|
|
|
|
next_keys_to_process_stack.emplace_back(KeyAndDepth{key, -1});
|
|
|
|
}
|
|
|
|
|
|
|
|
already_processed_keys_during_loop.insert(key);
|
|
|
|
|
|
|
|
++depth;
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
const auto & childs = it->getMapped();
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
for (auto child_key : childs)
|
|
|
|
{
|
|
|
|
if (std::is_same_v<Strategy, GetAllDescendantsStrategy> || depth == level)
|
|
|
|
descendants.emplace_back(child_key);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
next_keys_to_process_stack.emplace_back(KeyAndDepth{child_key, depth});
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
already_processed_keys_during_loop.clear();
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
descendants_offsets.emplace_back(descendants.size());
|
2021-03-24 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
ElementsAndOffsets<KeyType> result = {std::move(descendants), std::move(descendants_offsets)};
|
|
|
|
return result;
|
2021-03-24 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
template<typename KeyType>
|
|
|
|
ColumnPtr convertElementsAndOffsetsIntoArray(ElementsAndOffsets<KeyType> && elements_and_offsets)
|
|
|
|
{
|
|
|
|
auto elements_column = ColumnVector<KeyType>::create();
|
|
|
|
elements_column->getData() = std::move(elements_and_offsets.elements);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
auto offsets_column = ColumnVector<IColumn::Offset>::create();
|
|
|
|
offsets_column->getData() = std::move(elements_and_offsets.offsets);
|
|
|
|
|
|
|
|
auto column_array = ColumnArray::create(std::move(elements_column), std::move(offsets_column));
|
|
|
|
|
|
|
|
return column_array;
|
|
|
|
}
|
2021-03-24 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename KeyType, typename IsKeyValidFunc, typename GetParentKeyFunc>
|
|
|
|
ColumnPtr getKeysHierarchyArray(
|
|
|
|
const PaddedPODArray<KeyType> & hierarchy_keys,
|
|
|
|
const KeyType & hierarchy_null_value,
|
|
|
|
IsKeyValidFunc && is_key_valid_func,
|
|
|
|
GetParentKeyFunc && get_parent_func)
|
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
auto elements_and_offsets = detail::getKeysHierarchy(hierarchy_keys, hierarchy_null_value, std::forward<IsKeyValidFunc>(is_key_valid_func), std::forward<GetParentKeyFunc>(get_parent_func));
|
|
|
|
return detail::convertElementsAndOffsetsIntoArray(std::move(elements_and_offsets));
|
2021-03-24 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename KeyType, typename IsKeyValidFunc, typename GetParentKeyFunc>
|
|
|
|
PaddedPODArray<UInt8> isInKeysHierarchy(
|
|
|
|
const PaddedPODArray<KeyType> & hierarchy_keys,
|
|
|
|
const PaddedPODArray<KeyType> & hierarchy_in_keys,
|
|
|
|
const KeyType & hierarchy_null_value,
|
|
|
|
IsKeyValidFunc && is_key_valid_func,
|
|
|
|
GetParentKeyFunc && get_parent_func)
|
|
|
|
{
|
|
|
|
assert(hierarchy_keys.size() == hierarchy_in_keys.size());
|
|
|
|
|
|
|
|
PaddedPODArray<UInt8> result;
|
|
|
|
result.resize_fill(hierarchy_keys.size());
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
detail::ElementsAndOffsets<KeyType> hierarchy = detail::getKeysHierarchy(
|
2021-03-24 19:55:06 +00:00
|
|
|
hierarchy_keys,
|
|
|
|
hierarchy_null_value,
|
|
|
|
std::forward<IsKeyValidFunc>(is_key_valid_func),
|
|
|
|
std::forward<GetParentKeyFunc>(get_parent_func));
|
2021-03-24 16:31:00 +00:00
|
|
|
|
|
|
|
auto & offsets = hierarchy.offsets;
|
|
|
|
auto & elements = hierarchy.elements;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
|
|
|
{
|
|
|
|
size_t i_elements_start = i > 0 ? offsets[i - 1] : 0;
|
|
|
|
size_t i_elements_end = offsets[i];
|
|
|
|
|
|
|
|
auto & key_to_find = hierarchy_in_keys[i];
|
|
|
|
|
|
|
|
const auto * begin = elements.begin() + i_elements_start;
|
|
|
|
const auto * end = elements.begin() + i_elements_end;
|
|
|
|
|
|
|
|
const auto * it = std::find(begin, end, key_to_find);
|
|
|
|
|
|
|
|
bool contains_key = (it != end);
|
|
|
|
result[i] = contains_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
template <typename KeyType>
|
2021-03-25 07:31:12 +00:00
|
|
|
ColumnPtr getDescendantsArray(
|
2021-03-25 13:23:19 +00:00
|
|
|
const PaddedPODArray<KeyType> & requested_keys,
|
|
|
|
const HashMap<KeyType, PaddedPODArray<KeyType>> & parent_to_child,
|
|
|
|
size_t level)
|
2021-03-24 19:55:06 +00:00
|
|
|
{
|
2021-03-25 13:23:19 +00:00
|
|
|
if (level == 0)
|
|
|
|
{
|
|
|
|
detail::GetAllDescendantsStrategy strategy { .level = level };
|
|
|
|
auto elements_and_offsets = detail::getDescendants(requested_keys, parent_to_child, strategy);
|
|
|
|
return detail::convertElementsAndOffsetsIntoArray(std::move(elements_and_offsets));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
detail::GetDescendantsAtSpecificLevelStrategy strategy { .level = level };
|
|
|
|
auto elements_and_offsets = detail::getDescendants(requested_keys, parent_to_child, strategy);
|
|
|
|
return detail::convertElementsAndOffsetsIntoArray(std::move(elements_and_offsets));
|
|
|
|
}
|
2021-03-24 19:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:19 +00:00
|
|
|
ColumnPtr getHierarchyDefaultImplementation(
|
|
|
|
const IDictionary * dictionary,
|
|
|
|
ColumnPtr key_column,
|
|
|
|
const DataTypePtr & key_type);
|
2021-03-24 16:31:00 +00:00
|
|
|
|
|
|
|
ColumnUInt8::Ptr isInHierarchyDefaultImplementation(
|
|
|
|
const IDictionary * dictionary,
|
|
|
|
ColumnPtr key_column,
|
|
|
|
ColumnPtr in_key_column,
|
|
|
|
const DataTypePtr & key_type);
|
|
|
|
|
|
|
|
}
|