2012-08-22 20:29:01 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-07-28 17:34:02 +00:00
|
|
|
#include <shared_mutex>
|
2018-03-11 00:15:26 +00:00
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <DataStreams/SizeLimits.h>
|
|
|
|
#include <DataTypes/IDataType.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/SetVariants.h>
|
2018-03-11 00:15:26 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Storages/MergeTree/BoolMask.h>
|
2012-08-22 20:29:01 +00:00
|
|
|
|
2016-12-21 12:10:31 +00:00
|
|
|
#include <common/logger_useful.h>
|
2015-03-02 01:11:37 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2016-12-21 12:10:31 +00:00
|
|
|
namespace DB
|
2015-03-02 01:11:37 +00:00
|
|
|
{
|
|
|
|
|
2017-01-14 09:11:11 +00:00
|
|
|
struct Range;
|
2018-02-08 15:25:46 +00:00
|
|
|
class FieldWithInfinity;
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2018-02-02 13:19:40 +00:00
|
|
|
using SetElements = std::vector<std::vector<Field>>;
|
2018-02-08 14:15:21 +00:00
|
|
|
using SetElementsPtr = std::unique_ptr<SetElements>;
|
2018-02-02 13:19:40 +00:00
|
|
|
|
2018-02-08 17:46:22 +00:00
|
|
|
class IFunctionBase;
|
|
|
|
using FunctionBasePtr = std::shared_ptr<IFunctionBase>;
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2017-03-03 21:15:46 +00:00
|
|
|
/** Data structure for implementation of IN expression.
|
2012-08-22 20:29:01 +00:00
|
|
|
*/
|
2012-08-23 22:40:51 +00:00
|
|
|
class Set
|
2012-08-22 20:29:01 +00:00
|
|
|
{
|
2012-08-23 22:40:51 +00:00
|
|
|
public:
|
2018-03-11 00:15:26 +00:00
|
|
|
Set(const SizeLimits & limits) :
|
2017-04-01 07:20:54 +00:00
|
|
|
log(&Logger::get("Set")),
|
2018-03-11 00:15:26 +00:00
|
|
|
limits(limits),
|
2018-02-08 14:15:21 +00:00
|
|
|
set_elements(std::make_unique<SetElements>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
2014-07-06 19:48:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool empty() const { return data.empty(); }
|
2012-08-23 20:22:44 +00:00
|
|
|
|
2018-04-19 21:34:04 +00:00
|
|
|
/** Set can be created either from AST or from a stream of data (subquery result).
|
|
|
|
*/
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Create a Set from expression (specified literally in the query).
|
|
|
|
* 'types' - types of what are on the left hand side of IN.
|
|
|
|
* 'node' - list of values: 1, 2, 3 or list of tuples: (1, 2), (3, 4), (5, 6).
|
2018-02-02 13:19:40 +00:00
|
|
|
* 'fill_set_elements' - if true, fill vector of elements. For primary key to work.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2018-02-02 13:19:40 +00:00
|
|
|
void createFromAST(const DataTypes & types, ASTPtr node, const Context & context, bool fill_set_elements);
|
2014-03-04 11:26:55 +00:00
|
|
|
|
2018-04-19 21:34:04 +00:00
|
|
|
/** Create a Set from stream.
|
|
|
|
* Call setHeader, then call insertFromBlock for each block.
|
2018-01-21 07:30:07 +00:00
|
|
|
*/
|
2018-04-19 21:34:04 +00:00
|
|
|
void setHeader(const Block & header);
|
|
|
|
|
|
|
|
/// Returns false, if some limit was exceeded and no need to insert more data.
|
2018-02-08 14:15:21 +00:00
|
|
|
bool insertFromBlock(const Block & block, bool fill_set_elements);
|
2014-03-04 11:26:55 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** For columns of 'block', check belonging of corresponding rows to the set.
|
|
|
|
* Return UInt8 column with the result.
|
|
|
|
*/
|
|
|
|
ColumnPtr execute(const Block & block, bool negative) const;
|
2014-03-26 10:56:21 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getTotalRowCount() const { return data.getTotalRowCount(); }
|
|
|
|
size_t getTotalByteCount() const { return data.getTotalByteCount(); }
|
2018-04-18 19:38:40 +00:00
|
|
|
|
|
|
|
const DataTypes & getDataTypes() const { return data_types; }
|
|
|
|
|
2018-02-08 14:15:21 +00:00
|
|
|
SetElements & getSetElements() { return *set_elements.get(); }
|
2014-06-18 20:08:31 +00:00
|
|
|
|
2012-08-23 20:22:44 +00:00
|
|
|
private:
|
2018-04-19 21:34:04 +00:00
|
|
|
size_t keys_size;
|
2017-04-01 07:20:54 +00:00
|
|
|
Sizes key_sizes;
|
|
|
|
|
|
|
|
SetVariants data;
|
|
|
|
|
|
|
|
/** How IN works with Nullable types.
|
|
|
|
*
|
|
|
|
* For simplicity reasons, all NULL values and any tuples with at least one NULL element are ignored in the Set.
|
|
|
|
* And for left hand side values, that are NULLs or contain any NULLs, we return 0 (means that element is not in Set).
|
|
|
|
*
|
|
|
|
* If we want more standard compliant behaviour, we must return NULL
|
|
|
|
* if lhs is NULL and set is not empty or if lhs is not in set, but set contains at least one NULL.
|
|
|
|
* It is more complicated with tuples.
|
|
|
|
* For example,
|
|
|
|
* (1, NULL, 2) IN ((1, NULL, 3)) must return 0,
|
|
|
|
* but (1, NULL, 2) IN ((1, 1111, 2)) must return NULL.
|
|
|
|
*
|
|
|
|
* We have not implemented such sophisticated behaviour.
|
|
|
|
*/
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** The data types from which the set was created.
|
|
|
|
* When checking for belonging to a set, the types of columns to be checked must match with them.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
DataTypes data_types;
|
|
|
|
|
|
|
|
Logger * log;
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Limitations on the maximum size of the set
|
2018-03-11 00:15:26 +00:00
|
|
|
SizeLimits limits;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// If in the left part columns contains the same types as the elements of the set.
|
2017-04-01 07:20:54 +00:00
|
|
|
void executeOrdinary(
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt8::Container & vec_res,
|
2017-04-01 07:20:54 +00:00
|
|
|
bool negative,
|
|
|
|
const PaddedPODArray<UInt8> * null_map) const;
|
|
|
|
|
2018-02-02 13:19:40 +00:00
|
|
|
/// Vector of elements of `Set`.
|
2017-06-02 21:37:28 +00:00
|
|
|
/// It is necessary for the index to work on the primary key in the IN statement.
|
2018-02-02 13:19:40 +00:00
|
|
|
SetElementsPtr set_elements;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Protects work with the set in the functions `insertFromBlock` and `execute`.
|
|
|
|
* These functions can be called simultaneously from different threads only when using StorageSet,
|
|
|
|
* and StorageSet calls only these two functions.
|
|
|
|
* Therefore, the rest of the functions for working with set are not protected.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-07-28 17:34:02 +00:00
|
|
|
mutable std::shared_mutex rwlock;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
template <typename Method>
|
|
|
|
void insertFromBlockImpl(
|
|
|
|
Method & method,
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t rows,
|
|
|
|
SetVariants & variants,
|
|
|
|
ConstNullMapPtr null_map);
|
|
|
|
|
|
|
|
template <typename Method, bool has_null_map>
|
|
|
|
void insertFromBlockImplCase(
|
|
|
|
Method & method,
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t rows,
|
|
|
|
SetVariants & variants,
|
|
|
|
ConstNullMapPtr null_map);
|
|
|
|
|
|
|
|
template <typename Method>
|
|
|
|
void executeImpl(
|
|
|
|
Method & method,
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt8::Container & vec_res,
|
2017-04-01 07:20:54 +00:00
|
|
|
bool negative,
|
|
|
|
size_t rows,
|
|
|
|
ConstNullMapPtr null_map) const;
|
|
|
|
|
|
|
|
template <typename Method, bool has_null_map>
|
|
|
|
void executeImplCase(
|
|
|
|
Method & method,
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt8::Container & vec_res,
|
2017-04-01 07:20:54 +00:00
|
|
|
bool negative,
|
|
|
|
size_t rows,
|
|
|
|
ConstNullMapPtr null_map) const;
|
2012-08-22 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using SetPtr = std::shared_ptr<Set>;
|
|
|
|
using ConstSetPtr = std::shared_ptr<const Set>;
|
|
|
|
using Sets = std::vector<SetPtr>;
|
2012-08-22 20:29:01 +00:00
|
|
|
|
2018-02-02 13:19:40 +00:00
|
|
|
class IFunction;
|
|
|
|
using FunctionPtr = std::shared_ptr<IFunction>;
|
|
|
|
|
2018-02-08 14:15:21 +00:00
|
|
|
/// Class for mayBeTrueInRange function.
|
2018-02-08 15:31:37 +00:00
|
|
|
class MergeTreeSetIndex
|
|
|
|
{
|
2018-02-02 13:19:40 +00:00
|
|
|
public:
|
2018-02-08 14:15:21 +00:00
|
|
|
/** Mapping for tuple positions from Set::set_elements to
|
|
|
|
* position of pk index and data type of this pk column
|
|
|
|
* and functions chain applied to this column.
|
|
|
|
*/
|
2018-02-08 15:31:37 +00:00
|
|
|
struct PKTuplePositionMapping
|
|
|
|
{
|
2018-02-02 13:19:40 +00:00
|
|
|
size_t tuple_index;
|
2018-04-20 00:20:36 +00:00
|
|
|
size_t key_index;
|
2018-02-08 17:46:22 +00:00
|
|
|
std::vector<FunctionBasePtr> functions;
|
2018-02-02 13:19:40 +00:00
|
|
|
};
|
|
|
|
|
2018-02-08 14:15:21 +00:00
|
|
|
MergeTreeSetIndex(const SetElements & set_elements, std::vector<PKTuplePositionMapping> && indexes_mapping_);
|
2018-02-02 13:19:40 +00:00
|
|
|
|
2018-02-20 20:03:15 +00:00
|
|
|
BoolMask mayBeTrueInRange(const std::vector<Range> & key_ranges, const DataTypes & data_types);
|
2018-02-02 13:19:40 +00:00
|
|
|
private:
|
2018-02-08 14:15:21 +00:00
|
|
|
using OrderedTuples = std::vector<std::vector<FieldWithInfinity>>;
|
|
|
|
OrderedTuples ordered_set;
|
2018-02-02 13:19:40 +00:00
|
|
|
|
2018-02-08 14:15:21 +00:00
|
|
|
std::vector<PKTuplePositionMapping> indexes_mapping;
|
2018-02-02 13:19:40 +00:00
|
|
|
};
|
|
|
|
|
2018-02-08 14:15:21 +00:00
|
|
|
}
|