ClickHouse/dbms/src/Interpreters/Set.cpp

608 lines
20 KiB
C++
Raw Normal View History

2018-06-30 21:35:01 +00:00
#include <optional>
#include <Core/Field.h>
#include <Common/FieldVisitors.h>
#include <Core/Row.h>
2012-08-23 20:22:44 +00:00
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnTuple.h>
2012-08-23 20:22:44 +00:00
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
#include <DataStreams/IBlockInputStream.h>
2012-08-24 19:42:03 +00:00
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeNullable.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
2012-08-23 23:49:28 +00:00
#include <Interpreters/Set.h>
#include <Interpreters/convertFieldToType.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/NullableUtils.h>
2018-06-30 21:35:01 +00:00
#include <Interpreters/sortBlock.h>
2012-08-23 20:22:44 +00:00
#include <Storages/MergeTree/KeyCondition.h>
Squashed commit of the following: commit e712f469a55ff34ad34b482b15cc4153b7ad7233 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:59:13 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a002823084e3a79bffcc17d479620a68eb0644b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:58:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9e06f407c8ee781ed8ddf98bdfcc31846bf2a0fe Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:55:14 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9581620f1e839f456fa7894aa1f996d5162ac6cd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:54:22 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a8564c68cb6cc3649fafaf401256d43c9a2e777 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:47:34 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit cf60632d78ec656be3304ef4565e859bb6ce80ba Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:40:09 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit ee3d1dc6e0c4ca60e3ac1e0c30d4b3ed1e66eca0 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:22:49 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 65592ef7116a90104fcd524b53ef8b7cf22640f2 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:18:17 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 37972c257320d3b7e7b294e0fdeffff218647bfd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:17:06 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit dd909d149974ce5bed2456de1261aa5a368fd3ff Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:16:28 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 3cf43266ca7e30adf01212b1a739ba5fe43639fd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:15:42 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 6731a3df96d1609286e2536b6432916af7743f0f Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:13:35 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 1b5727e0d56415b7add4cb76110105358663602c Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:11:18 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit bbcf726a55685b8e72f5b40ba0bf1904bd1c0407 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:09:04 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit c03b477d5e2e65014e8906ecfa2efb67ee295af1 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:06:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2986e2fb0466bc18d73693dcdded28fccc0dc66b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:05:44 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 5d6cdef13d2e02bd5c4954983334e9162ab2635b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:04:53 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit f2b819b25ce8b2ccdcb201eefb03e1e6f5aab590 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:01:47 2017 +0300 Less dependencies [#CLICKHOUSE-2]
2017-01-14 09:00:19 +00:00
2018-06-30 21:35:01 +00:00
#include <ext/range.h>
#include <DataTypes/DataTypeLowCardinality.h>
2018-06-30 21:35:01 +00:00
2012-08-23 20:22:44 +00:00
namespace DB
{
2016-01-12 02:21:15 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int SET_SIZE_LIMIT_EXCEEDED;
extern const int TYPE_MISMATCH;
extern const int INCORRECT_ELEMENT_OF_SET;
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
2016-01-12 02:21:15 +00:00
}
template <typename Method>
void NO_INLINE Set::insertFromBlockImpl(
Method & method,
const ColumnRawPtrs & key_columns,
size_t rows,
SetVariants & variants,
2018-06-30 21:35:01 +00:00
ConstNullMapPtr null_map,
ColumnUInt8::Container * out_filter)
{
if (null_map)
2018-06-30 21:35:01 +00:00
{
if (out_filter)
insertFromBlockImplCase<Method, true, true>(method, key_columns, rows, variants, null_map, out_filter);
else
insertFromBlockImplCase<Method, true, false>(method, key_columns, rows, variants, null_map, out_filter);
}
else
2018-06-30 21:35:01 +00:00
{
if (out_filter)
insertFromBlockImplCase<Method, false, true>(method, key_columns, rows, variants, null_map, out_filter);
else
insertFromBlockImplCase<Method, false, false>(method, key_columns, rows, variants, null_map, out_filter);
}
}
2018-06-30 21:35:01 +00:00
template <typename Method, bool has_null_map, bool build_filter>
void NO_INLINE Set::insertFromBlockImplCase(
Method & method,
const ColumnRawPtrs & key_columns,
size_t rows,
SetVariants & variants,
2019-02-04 16:48:43 +00:00
[[maybe_unused]] ConstNullMapPtr null_map,
[[maybe_unused]] ColumnUInt8::Container * out_filter)
{
2019-02-04 14:36:15 +00:00
typename Method::State state(key_columns, key_sizes, nullptr);
/// For all rows
for (size_t i = 0; i < rows; ++i)
{
2019-02-04 16:48:43 +00:00
if constexpr (has_null_map)
{
2019-02-04 16:48:43 +00:00
if ((*null_map)[i])
{
if constexpr (build_filter)
{
(*out_filter)[i] = false;
}
2019-02-04 16:48:43 +00:00
continue;
}
}
2019-02-04 16:48:43 +00:00
[[maybe_unused]] auto emplace_result = state.emplaceKey(method.data, i, variants.string_pool);
2018-06-30 21:35:01 +00:00
2019-02-04 16:48:43 +00:00
if constexpr (build_filter)
2019-02-04 14:36:15 +00:00
(*out_filter)[i] = emplace_result.isInserted();
}
2012-08-23 20:22:44 +00:00
}
2018-04-19 21:34:04 +00:00
void Set::setHeader(const Block & block)
2012-08-23 20:22:44 +00:00
{
std::unique_lock lock(rwlock);
2018-04-19 21:34:04 +00:00
if (!empty())
return;
keys_size = block.columns();
ColumnRawPtrs key_columns;
key_columns.reserve(keys_size);
2018-04-19 21:34:04 +00:00
data_types.reserve(keys_size);
2019-10-31 16:14:06 +00:00
set_elements_types.reserve(keys_size);
2018-04-19 21:34:04 +00:00
/// The constant columns to the right of IN are not supported directly. For this, they first materialize.
Columns materialized_columns;
/// Remember the columns we will work with
for (size_t i = 0; i < keys_size; ++i)
{
materialized_columns.emplace_back(block.safeGetByPosition(i).column->convertToFullColumnIfConst());
key_columns.emplace_back(materialized_columns.back().get());
2018-04-19 21:34:04 +00:00
data_types.emplace_back(block.safeGetByPosition(i).type);
2019-10-31 16:14:06 +00:00
set_elements_types.emplace_back(block.safeGetByPosition(i).type);
2018-04-19 21:34:04 +00:00
/// Convert low cardinality column to full.
if (auto * low_cardinality_type = typeid_cast<const DataTypeLowCardinality *>(data_types.back().get()))
{
data_types.back() = low_cardinality_type->getDictionaryType();
materialized_columns.emplace_back(key_columns.back()->convertToFullColumnIfLowCardinality());
key_columns.back() = materialized_columns.back().get();
}
}
2018-04-19 21:34:04 +00:00
/// We will insert to the Set only keys, where all components are not NULL.
ConstNullMapPtr null_map{};
2019-07-03 19:06:34 +00:00
ColumnPtr null_map_holder = extractNestedColumnsAndNullMap(key_columns, null_map);
2018-04-19 21:34:04 +00:00
2018-07-02 18:57:14 +00:00
if (fill_set_elements)
{
/// Create empty columns with set values in advance.
/// It is needed because set may be empty, so method 'insertFromBlock' will be never called.
set_elements.reserve(keys_size);
2019-10-31 16:14:06 +00:00
for (const auto & type : set_elements_types)
set_elements.emplace_back(type->createColumn());
2018-07-02 18:57:14 +00:00
}
2018-04-19 21:34:04 +00:00
/// Choose data structure to use for the set.
data.init(data.chooseMethod(key_columns, key_sizes));
}
2018-07-02 18:57:14 +00:00
bool Set::insertFromBlock(const Block & block)
2018-04-19 21:34:04 +00:00
{
std::unique_lock lock(rwlock);
if (empty())
throw Exception("Method Set::setHeader must be called before Set::insertFromBlock", ErrorCodes::LOGICAL_ERROR);
ColumnRawPtrs key_columns;
key_columns.reserve(keys_size);
2017-04-02 17:37:49 +00:00
/// The constant columns to the right of IN are not supported directly. For this, they first materialize.
Columns materialized_columns;
2017-04-02 17:37:49 +00:00
/// Remember the columns we will work with
for (size_t i = 0; i < keys_size; ++i)
{
materialized_columns.emplace_back(block.safeGetByPosition(i).column->convertToFullColumnIfConst()->convertToFullColumnIfLowCardinality());
key_columns.emplace_back(materialized_columns.back().get());
}
size_t rows = block.rows();
/// We will insert to the Set only keys, where all components are not NULL.
ConstNullMapPtr null_map{};
2019-07-03 19:06:34 +00:00
ColumnPtr null_map_holder = extractNestedColumnsAndNullMap(key_columns, null_map);
2018-06-30 21:35:01 +00:00
/// Filter to extract distinct values from the block.
ColumnUInt8::MutablePtr filter;
if (fill_set_elements)
filter = ColumnUInt8::create(block.rows());
switch (data.type)
{
case SetVariants::Type::EMPTY:
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
2018-06-30 21:35:01 +00:00
insertFromBlockImpl(*data.NAME, key_columns, rows, data, null_map, filter ? &filter->getData() : nullptr); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
}
2012-08-23 23:49:28 +00:00
if (fill_set_elements)
{
2018-06-30 21:35:01 +00:00
for (size_t i = 0; i < keys_size; ++i)
{
auto filtered_column = block.getByPosition(i).column->filter(filter->getData(), rows);
2018-07-02 18:57:14 +00:00
if (set_elements[i]->empty())
2018-06-30 21:56:45 +00:00
set_elements[i] = filtered_column;
else
set_elements[i]->insertRangeFrom(*filtered_column, 0, filtered_column->size());
}
}
return limits.check(getTotalRowCount(), getTotalByteCount(), "IN-set", ErrorCodes::SET_SIZE_LIMIT_EXCEEDED);
2012-08-23 20:22:44 +00:00
}
2018-04-19 21:34:04 +00:00
static Field extractValueFromNode(const ASTPtr & node, const IDataType & type, const Context & context)
2012-08-24 19:42:03 +00:00
{
2019-03-11 13:22:51 +00:00
if (const auto * lit = node->as<ASTLiteral>())
{
return convertFieldToType(lit->value, type);
}
2019-03-11 13:22:51 +00:00
else if (node->as<ASTFunction>())
{
std::pair<Field, DataTypePtr> value_raw = evaluateConstantExpression(node, context);
return convertFieldToType(value_raw.first, type, value_raw.second.get());
}
else
throw Exception("Incorrect element of set. Must be literal or constant expression.", ErrorCodes::INCORRECT_ELEMENT_OF_SET);
}
2018-07-02 18:57:14 +00:00
void Set::createFromAST(const DataTypes & types, ASTPtr node, const Context & context)
{
/// Will form a block with values from the set.
2018-04-19 21:34:04 +00:00
Block header;
size_t num_columns = types.size();
for (size_t i = 0; i < num_columns; ++i)
header.insert(ColumnWithTypeAndName(types[i]->createColumn(), types[i], "_" + toString(i)));
setHeader(header);
MutableColumns columns = header.cloneEmptyColumns();
DataTypePtr tuple_type;
Row tuple_values;
const auto & list = node->as<ASTExpressionList &>();
for (auto & elem : list.children)
{
2018-04-19 21:34:04 +00:00
if (num_columns == 1)
{
Field value = extractValueFromNode(elem, *types[0], context);
if (!value.isNull())
columns[0]->insert(value);
}
2019-03-11 13:22:51 +00:00
else if (const auto * func = elem->as<ASTFunction>())
{
Field function_result;
const Tuple * tuple = nullptr;
if (func->name != "tuple")
{
if (!tuple_type)
tuple_type = std::make_shared<DataTypeTuple>(types);
function_result = extractValueFromNode(elem, *tuple_type, context);
if (function_result.getType() != Field::Types::Tuple)
throw Exception("Invalid type of set. Expected tuple, got " + String(function_result.getTypeName()),
ErrorCodes::INCORRECT_ELEMENT_OF_SET);
tuple = &function_result.get<Tuple>();
}
size_t tuple_size = tuple ? tuple->size() : func->arguments->children.size();
2018-04-19 21:34:04 +00:00
if (tuple_size != num_columns)
throw Exception("Incorrect size of tuple in set: " + toString(tuple_size) + " instead of " + toString(num_columns),
ErrorCodes::INCORRECT_ELEMENT_OF_SET);
if (tuple_values.empty())
tuple_values.resize(tuple_size);
size_t i = 0;
for (; i < tuple_size; ++i)
{
Field value = tuple ? (*tuple)[i]
: extractValueFromNode(func->arguments->children[i], *types[i], context);
2017-04-02 17:37:49 +00:00
/// If at least one of the elements of the tuple has an impossible (outside the range of the type) value, then the entire tuple too.
if (value.isNull())
break;
tuple_values[i] = value;
}
if (i == tuple_size)
for (i = 0; i < tuple_size; ++i)
columns[i]->insert(tuple_values[i]);
}
else
throw Exception("Incorrect element of set", ErrorCodes::INCORRECT_ELEMENT_OF_SET);
}
2018-04-19 21:34:04 +00:00
Block block = header.cloneWithColumns(std::move(columns));
2018-07-02 18:57:14 +00:00
insertFromBlock(block);
2019-11-01 10:58:29 +00:00
finishInsert();
2012-08-24 19:42:03 +00:00
}
ColumnPtr Set::execute(const Block & block, bool negative) const
2012-08-23 20:22:44 +00:00
{
size_t num_key_columns = block.columns();
if (0 == num_key_columns)
throw Exception("Logical error: no columns passed to Set::execute method.", ErrorCodes::LOGICAL_ERROR);
auto res = ColumnUInt8::create();
ColumnUInt8::Container & vec_res = res->getData();
vec_res.resize(block.safeGetByPosition(0).column->size());
if (vec_res.empty())
return res;
std::shared_lock lock(rwlock);
/// If the set is empty.
if (data_types.empty())
{
if (negative)
memset(vec_res.data(), 1, vec_res.size());
else
memset(vec_res.data(), 0, vec_res.size());
return res;
}
2019-08-19 16:51:50 +00:00
checkColumnsNumber(num_key_columns);
/// Remember the columns we will work with. Also check that the data types are correct.
ColumnRawPtrs key_columns;
key_columns.reserve(num_key_columns);
/// The constant columns to the left of IN are not supported directly. For this, they first materialize.
Columns materialized_columns;
for (size_t i = 0; i < num_key_columns; ++i)
{
2019-08-19 16:51:50 +00:00
checkTypesEqual(i, block.safeGetByPosition(i).type);
materialized_columns.emplace_back(block.safeGetByPosition(i).column->convertToFullColumnIfConst());
key_columns.emplace_back() = materialized_columns.back().get();
}
/// We will check existence in Set only for keys, where all components are not NULL.
ConstNullMapPtr null_map{};
2019-07-03 19:06:34 +00:00
ColumnPtr null_map_holder = extractNestedColumnsAndNullMap(key_columns, null_map);
executeOrdinary(key_columns, vec_res, negative, null_map);
return res;
}
2012-08-23 22:40:51 +00:00
template <typename Method>
void NO_INLINE Set::executeImpl(
Method & method,
const ColumnRawPtrs & key_columns,
ColumnUInt8::Container & vec_res,
bool negative,
size_t rows,
ConstNullMapPtr null_map) const
{
if (null_map)
executeImplCase<Method, true>(method, key_columns, vec_res, negative, rows, null_map);
else
executeImplCase<Method, false>(method, key_columns, vec_res, negative, rows, null_map);
}
template <typename Method, bool has_null_map>
void NO_INLINE Set::executeImplCase(
Method & method,
const ColumnRawPtrs & key_columns,
ColumnUInt8::Container & vec_res,
bool negative,
size_t rows,
ConstNullMapPtr null_map) const
{
2019-02-04 14:36:15 +00:00
Arena pool;
typename Method::State state(key_columns, key_sizes, nullptr);
2019-02-04 19:40:06 +00:00
/// NOTE Optimization is not used for consecutive identical strings.
2017-04-02 17:37:49 +00:00
/// For all rows
for (size_t i = 0; i < rows; ++i)
{
if (has_null_map && (*null_map)[i])
vec_res[i] = negative;
else
{
2019-02-04 14:36:15 +00:00
auto find_result = state.findKey(method.data, i, pool);
vec_res[i] = negative ^ find_result.isFound();
}
}
}
void Set::executeOrdinary(
const ColumnRawPtrs & key_columns,
ColumnUInt8::Container & vec_res,
bool negative,
ConstNullMapPtr null_map) const
{
size_t rows = key_columns[0]->size();
switch (data.type)
{
case SetVariants::Type::EMPTY:
break;
#define M(NAME) \
case SetVariants::Type::NAME: \
executeImpl(*data.NAME, key_columns, vec_res, negative, rows, null_map); \
break;
APPLY_FOR_SET_VARIANTS(M)
#undef M
}
2012-08-23 20:22:44 +00:00
}
2019-08-19 16:51:50 +00:00
void Set::checkColumnsNumber(size_t num_key_columns) const
{
if (data_types.size() != num_key_columns)
{
std::stringstream message;
message << "Number of columns in section IN doesn't match. "
<< num_key_columns << " at left, " << data_types.size() << " at right.";
throw Exception(message.str(), ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH);
}
}
void Set::checkTypesEqual(size_t set_type_idx, const DataTypePtr & other_type) const
{
if (!removeNullable(recursiveRemoveLowCardinality(data_types[set_type_idx]))->equals(*removeNullable(recursiveRemoveLowCardinality(other_type))))
2019-08-19 16:51:50 +00:00
throw Exception("Types of column " + toString(set_type_idx + 1) + " in section IN don't match: "
+ other_type->getName() + " on the left, "
+ data_types[set_type_idx]->getName() + " on the right", ErrorCodes::TYPE_MISMATCH);
2019-08-19 16:51:50 +00:00
}
2018-06-30 21:35:01 +00:00
MergeTreeSetIndex::MergeTreeSetIndex(const Columns & set_elements, std::vector<KeyTuplePositionMapping> && index_mapping_)
: indexes_mapping(std::move(index_mapping_))
{
2018-02-08 15:31:37 +00:00
std::sort(indexes_mapping.begin(), indexes_mapping.end(),
[](const KeyTuplePositionMapping & l, const KeyTuplePositionMapping & r)
2018-02-08 15:31:37 +00:00
{
return std::forward_as_tuple(l.key_index, l.tuple_index) < std::forward_as_tuple(r.key_index, r.tuple_index);
2018-02-08 15:31:37 +00:00
});
2018-04-18 18:44:02 +00:00
indexes_mapping.erase(std::unique(
indexes_mapping.begin(), indexes_mapping.end(),
[](const KeyTuplePositionMapping & l, const KeyTuplePositionMapping & r)
{
return l.key_index == r.key_index;
2018-04-18 18:44:02 +00:00
}), indexes_mapping.end());
2018-06-30 21:35:01 +00:00
size_t tuple_size = indexes_mapping.size();
ordered_set.resize(tuple_size);
/// Create columns for points here to avoid extra allocations at 'checkInRange'.
left_point.reserve(tuple_size);
right_point.reserve(tuple_size);
2018-06-30 21:35:01 +00:00
for (size_t i = 0; i < tuple_size; ++i)
{
2018-06-30 21:35:01 +00:00
ordered_set[i] = set_elements[indexes_mapping[i].tuple_index];
2018-04-18 18:44:02 +00:00
left_point.emplace_back(ordered_set[i]->cloneEmpty());
right_point.emplace_back(ordered_set[i]->cloneEmpty());
}
2018-06-30 21:35:01 +00:00
Block block_to_sort;
SortDescription sort_description;
for (size_t i = 0; i < tuple_size; ++i)
{
block_to_sort.insert({ ordered_set[i], nullptr, "" });
sort_description.emplace_back(i, 1, 1);
}
2018-06-30 21:35:01 +00:00
sortBlock(block_to_sort, sort_description);
for (size_t i = 0; i < tuple_size; ++i)
ordered_set[i] = block_to_sort.getByPosition(i).column;
}
2018-02-08 14:15:21 +00:00
/** Return the BoolMask where:
* 1: the intersection of the set and the range is non-empty
* 2: the range contains elements not in the set
*/
BoolMask MergeTreeSetIndex::checkInRange(const std::vector<Range> & key_ranges, const DataTypes & data_types)
{
2018-06-30 21:35:01 +00:00
size_t tuple_size = indexes_mapping.size();
2018-02-08 14:15:21 +00:00
bool invert_left_infinities = false;
bool invert_right_infinities = false;
2018-06-30 21:35:01 +00:00
for (size_t i = 0; i < tuple_size; ++i)
{
std::optional<Range> new_range = KeyCondition::applyMonotonicFunctionsChainToRange(
key_ranges[indexes_mapping[i].key_index],
2018-02-08 15:31:37 +00:00
indexes_mapping[i].functions,
data_types[indexes_mapping[i].key_index]);
if (!new_range)
return {true, true};
2014-04-01 10:09:22 +00:00
2018-02-08 14:15:21 +00:00
/** A range that ends in (x, y, ..., +inf) exclusive is the same as a range
* that ends in (x, y, ..., -inf) inclusive and vice versa for the left bound.
*/
if (new_range->left_bounded)
{
2018-02-08 14:15:21 +00:00
if (!new_range->left_included)
invert_left_infinities = true;
2018-02-08 15:31:37 +00:00
left_point[i].update(new_range->left);
}
else
{
2018-02-08 14:15:21 +00:00
if (invert_left_infinities)
left_point[i].update(ValueWithInfinity::PLUS_INFINITY);
2018-02-08 15:31:37 +00:00
else
left_point[i].update(ValueWithInfinity::MINUS_INFINITY);
}
if (new_range->right_bounded)
{
2018-02-08 14:15:21 +00:00
if (!new_range->right_included)
invert_right_infinities = true;
2018-02-08 15:31:37 +00:00
right_point[i].update(new_range->right);
}
else
{
2018-02-08 14:15:21 +00:00
if (invert_right_infinities)
right_point[i].update(ValueWithInfinity::MINUS_INFINITY);
2018-02-08 15:31:37 +00:00
else
right_point[i].update(ValueWithInfinity::PLUS_INFINITY);
}
}
auto compare = [](const IColumn & lhs, const ValueWithInfinity & rhs, size_t row)
2018-06-30 21:35:01 +00:00
{
auto type = rhs.getType();
2020-02-27 11:23:55 +00:00
/// Return inverted infinity sign, because in 'lhs' all values are finite.
if (type != ValueWithInfinity::NORMAL)
2020-02-27 11:23:55 +00:00
return -static_cast<int>(type);
2018-06-30 21:35:01 +00:00
return lhs.compareAt(row, 0, rhs.getColumnIfFinite(), 1);
2018-06-30 21:35:01 +00:00
};
auto less = [this, &compare, tuple_size](size_t row, const auto & point)
{
for (size_t i = 0; i < tuple_size; ++i)
{
int res = compare(*ordered_set[i], point[i], row);
if (res)
return res < 0;
}
return false;
};
auto equals = [this, &compare, tuple_size](size_t row, const auto & point)
{
for (size_t i = 0; i < tuple_size; ++i)
if (compare(*ordered_set[i], point[i], row) != 0)
return false;
return true;
};
2018-02-08 14:15:21 +00:00
/** Because each parallelogram maps to a contiguous sequence of elements
* layed out in the lexicographically increasing order, the set intersects the range
* if and only if either bound coincides with an element or at least one element
* is between the lower bounds
*/
auto indices = ext::range(0, size());
auto left_lower = std::lower_bound(indices.begin(), indices.end(), left_point, less);
auto right_lower = std::lower_bound(indices.begin(), indices.end(), right_point, less);
2018-06-30 21:35:01 +00:00
return
{
left_lower != right_lower
|| (left_lower != indices.end() && equals(*left_lower, left_point))
|| (right_lower != indices.end() && equals(*right_lower, right_point)),
2018-06-30 21:35:01 +00:00
true
};
}
void ValueWithInfinity::update(const Field & x)
{
/// Keep at most one element in column.
if (!column->empty())
column->popBack(1);
column->insert(x);
type = NORMAL;
}
const IColumn & ValueWithInfinity::getColumnIfFinite() const
{
if (type != NORMAL)
2020-02-27 11:23:55 +00:00
throw Exception("Trying to get column of infinite type", ErrorCodes::LOGICAL_ERROR);
return *column;
}
2012-08-23 20:22:44 +00:00
}