2014-06-12 02:31:30 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-07-02 14:38:31 +00:00
|
|
|
|
#include <variant>
|
2019-03-20 16:58:28 +00:00
|
|
|
|
#include <optional>
|
2017-07-28 17:34:02 +00:00
|
|
|
|
#include <shared_mutex>
|
2019-07-04 12:12:39 +00:00
|
|
|
|
#include <deque>
|
2014-06-12 02:31:30 +00:00
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2014-06-18 18:31:35 +00:00
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
|
#include <Interpreters/IJoin.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <Interpreters/AggregationCommon.h>
|
2019-03-30 21:30:21 +00:00
|
|
|
|
#include <Interpreters/RowRefs.h>
|
2014-06-12 02:31:30 +00:00
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <Common/Arena.h>
|
2019-01-24 14:56:04 +00:00
|
|
|
|
#include <Common/ColumnsHashing.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
2019-02-28 09:35:38 +00:00
|
|
|
|
#include <Common/HashTable/FixedHashMap.h>
|
2014-06-12 02:31:30 +00:00
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
2017-03-28 06:51:22 +00:00
|
|
|
|
|
2018-03-11 00:15:26 +00:00
|
|
|
|
#include <DataStreams/SizeLimits.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
|
#include <DataStreams/IBlockStream_fwd.h>
|
2014-06-12 02:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
2019-05-07 18:21:44 +00:00
|
|
|
|
|
2020-04-07 09:48:47 +00:00
|
|
|
|
class TableJoin;
|
2020-04-08 18:59:52 +00:00
|
|
|
|
class DictionaryReader;
|
2019-09-02 19:58:45 +00:00
|
|
|
|
|
2019-05-07 18:21:44 +00:00
|
|
|
|
namespace JoinStuff
|
|
|
|
|
{
|
2019-05-08 10:53:45 +00:00
|
|
|
|
|
|
|
|
|
/// Base class with optional flag attached that's needed to implement RIGHT and FULL JOINs.
|
|
|
|
|
template <typename T, bool with_used>
|
2019-05-07 18:21:44 +00:00
|
|
|
|
struct WithFlags;
|
|
|
|
|
|
2019-05-08 10:53:45 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct WithFlags<T, true> : T
|
2019-05-07 18:21:44 +00:00
|
|
|
|
{
|
2019-05-08 10:53:45 +00:00
|
|
|
|
using Base = T;
|
|
|
|
|
using T::T;
|
|
|
|
|
|
2019-05-07 18:21:44 +00:00
|
|
|
|
mutable std::atomic<bool> used {};
|
|
|
|
|
void setUsed() const { used.store(true, std::memory_order_relaxed); } /// Could be set simultaneously from different threads.
|
|
|
|
|
bool getUsed() const { return used; }
|
2019-11-07 21:32:44 +00:00
|
|
|
|
|
|
|
|
|
bool setUsedOnce() const
|
|
|
|
|
{
|
|
|
|
|
/// fast check to prevent heavy CAS with seq_cst order
|
|
|
|
|
if (used.load(std::memory_order_relaxed))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool expected = false;
|
|
|
|
|
return used.compare_exchange_strong(expected, true);
|
|
|
|
|
}
|
2019-05-07 18:21:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-05-08 10:53:45 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct WithFlags<T, false> : T
|
2019-05-07 18:21:44 +00:00
|
|
|
|
{
|
2019-05-08 10:53:45 +00:00
|
|
|
|
using Base = T;
|
|
|
|
|
using T::T;
|
|
|
|
|
|
2019-05-07 18:21:44 +00:00
|
|
|
|
void setUsed() const {}
|
|
|
|
|
bool getUsed() const { return true; }
|
2019-11-07 21:32:44 +00:00
|
|
|
|
bool setUsedOnce() const { return true; }
|
2019-05-07 18:21:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-11-07 21:32:44 +00:00
|
|
|
|
using MappedOne = WithFlags<RowRef, false>;
|
|
|
|
|
using MappedAll = WithFlags<RowRefList, false>;
|
|
|
|
|
using MappedOneFlagged = WithFlags<RowRef, true>;
|
|
|
|
|
using MappedAllFlagged = WithFlags<RowRefList, true>;
|
|
|
|
|
using MappedAsof = WithFlags<AsofRowRefs, false>;
|
2019-05-07 18:21:44 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-29 11:12:47 +00:00
|
|
|
|
/** Data structure for implementation of JOIN.
|
|
|
|
|
* It is just a hash table: keys -> rows of joined ("right") table.
|
|
|
|
|
* Additionally, CROSS JOIN is supported: instead of hash table, it use just set of blocks without keys.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2019-12-02 18:07:27 +00:00
|
|
|
|
* JOIN-s could be of these types:
|
|
|
|
|
* - ALL × LEFT/INNER/RIGHT/FULL
|
|
|
|
|
* - ANY × LEFT/INNER/RIGHT
|
|
|
|
|
* - SEMI/ANTI x LEFT/RIGHT
|
|
|
|
|
* - ASOF x LEFT/INNER
|
|
|
|
|
* - CROSS
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2019-12-02 18:07:27 +00:00
|
|
|
|
* ALL means usual JOIN, when rows are multiplied by number of matching rows from the "right" table.
|
|
|
|
|
* ANY uses one line per unique key from right talbe. For LEFT JOIN it would be any row (with needed joined key) from the right table,
|
|
|
|
|
* for RIGHT JOIN it would be any row from the left table and for INNER one it would be any row from right and any row from left.
|
|
|
|
|
* SEMI JOIN filter left table by keys that are present in right table for LEFT JOIN, and filter right table by keys from left table
|
|
|
|
|
* for RIGHT JOIN. In other words SEMI JOIN returns only rows which joining keys present in another table.
|
|
|
|
|
* ANTI JOIN is the same as SEMI JOIN but returns rows with joining keys that are NOT present in another table.
|
|
|
|
|
* SEMI/ANTI JOINs allow to get values from both tables. For filter table it gets any row with joining same key. For ANTI JOIN it returns
|
|
|
|
|
* defaults other table columns.
|
|
|
|
|
* ASOF JOIN is not-equi join. For one key column it finds nearest value to join according to join inequality.
|
|
|
|
|
* It's expected that ANY|SEMI LEFT JOIN is more efficient that ALL one.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* If INNER is specified - leave only rows that have matching rows from "right" table.
|
|
|
|
|
* If LEFT is specified - in case when there is no matching row in "right" table, fill it with default values instead.
|
|
|
|
|
* If RIGHT is specified - first process as INNER, but track what rows from the right table was joined,
|
|
|
|
|
* and at the end, add rows from right table that was not joined and substitute default values for columns of left table.
|
|
|
|
|
* If FULL is specified - first process as LEFT, but track what rows from the right table was joined,
|
|
|
|
|
* and at the end, add rows from right table that was not joined and substitute default values for columns of left table.
|
2015-04-17 08:46:06 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* Thus, LEFT and RIGHT JOINs are not symmetric in terms of implementation.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* All JOINs (except CROSS) are done by equality condition on keys (equijoin).
|
|
|
|
|
* Non-equality and other conditions are not supported.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* Implementation:
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* 1. Build hash table in memory from "right" table.
|
|
|
|
|
* This hash table is in form of keys -> row in case of ANY or keys -> [rows...] in case of ALL.
|
|
|
|
|
* This is done in insertFromBlock method.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* 2. Process "left" table and join corresponding rows from "right" table by lookups in the map.
|
|
|
|
|
* This is done in joinBlock methods.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* In case of ANY LEFT JOIN - form new columns with found values or default values.
|
|
|
|
|
* This is the most simple. Number of rows in left table does not change.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* In case of ANY INNER JOIN - form new columns with found values,
|
|
|
|
|
* and also build a filter - in what rows nothing was found.
|
|
|
|
|
* Then filter columns of "left" table.
|
2014-06-19 19:00:58 +00:00
|
|
|
|
*
|
2017-03-29 11:12:47 +00:00
|
|
|
|
* In case of ALL ... JOIN - form new columns with all found rows,
|
|
|
|
|
* and also fill 'offsets' array, describing how many times we need to replicate values of "left" table.
|
|
|
|
|
* Then replicate columns of "left" table.
|
2017-03-30 14:09:24 +00:00
|
|
|
|
*
|
|
|
|
|
* How Nullable keys are processed:
|
|
|
|
|
*
|
|
|
|
|
* NULLs never join to anything, even to each other.
|
|
|
|
|
* During building of map, we just skip keys with NULL value of any component.
|
|
|
|
|
* During joining, we simply treat rows with any NULLs in key as non joined.
|
2017-04-04 06:52:39 +00:00
|
|
|
|
*
|
|
|
|
|
* Default values for outer joins (LEFT, RIGHT, FULL):
|
|
|
|
|
*
|
|
|
|
|
* Behaviour is controlled by 'join_use_nulls' settings.
|
|
|
|
|
* If it is false, we substitute (global) default value for the data type, for non-joined rows
|
|
|
|
|
* (zero, empty string, etc. and NULL for Nullable data types).
|
|
|
|
|
* If it is true, we always generate Nullable column and substitute NULLs for non-joined rows,
|
|
|
|
|
* as in standard SQL.
|
2014-06-12 02:31:30 +00:00
|
|
|
|
*/
|
2020-04-07 09:48:47 +00:00
|
|
|
|
class HashJoin : public IJoin
|
2014-06-12 02:31:30 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2020-04-07 09:48:47 +00:00
|
|
|
|
HashJoin(std::shared_ptr<TableJoin> table_join_, const Block & right_sample_block, bool any_take_last_row_ = false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
2020-04-08 18:59:52 +00:00
|
|
|
|
bool empty() const { return data->type == Type::EMPTY; }
|
|
|
|
|
bool overDictionary() const { return data->type == Type::DICT; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/** Add block of data from right hand of JOIN to the map.
|
|
|
|
|
* Returns false, if some limit was exceeded and you should not insert more data.
|
|
|
|
|
*/
|
2020-03-18 03:27:32 +00:00
|
|
|
|
bool addJoinedBlock(const Block & block, bool check_limits) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
|
/** Join data from the map (that was previously built by calls to addJoinedBlock) to the block with data from "left" table.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
* Could be called from different threads in parallel.
|
|
|
|
|
*/
|
2020-01-15 20:33:29 +00:00
|
|
|
|
void joinBlock(Block & block, ExtraBlockPtr & not_processed) override;
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
|
|
/// Infer the return type for joinGet function
|
2020-04-07 14:52:32 +00:00
|
|
|
|
DataTypePtr joinGetReturnType(const String & column_name, bool or_null) const;
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
|
|
/// Used by joinGet function that turns StorageJoin into a dictionary
|
2020-04-07 14:52:32 +00:00
|
|
|
|
void joinGet(Block & block, const String & column_name, bool or_null) const;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/** Keep "totals" (separate part of dataset, see WITH TOTALS) to use later.
|
|
|
|
|
*/
|
2019-09-09 19:43:37 +00:00
|
|
|
|
void setTotals(const Block & block) override { totals = block; }
|
|
|
|
|
bool hasTotals() const override { return totals; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
|
void joinTotals(Block & block) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/** For RIGHT and FULL JOINs.
|
|
|
|
|
* A stream that will contain default values from left table, joined with rows from right table, that was not joined before.
|
|
|
|
|
* Use only after all calls to joinBlock was done.
|
2017-04-04 06:52:39 +00:00
|
|
|
|
* left_sample_block is passed without account of 'use_nulls' setting (columns will be converted to Nullable inside).
|
2017-04-01 07:20:54 +00:00
|
|
|
|
*/
|
2019-11-05 20:22:20 +00:00
|
|
|
|
BlockInputStreamPtr createStreamWithNonJoinedRows(const Block & result_sample_block, UInt64 max_block_size) const override;
|
2019-12-22 02:44:04 +00:00
|
|
|
|
bool hasStreamWithNonJoinedRows() const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/// Number of keys in all built JOIN maps.
|
2019-10-11 17:27:54 +00:00
|
|
|
|
size_t getTotalRowCount() const final;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Sum size in bytes of all buffers, used for JOIN maps and for all memory pools.
|
2020-02-17 17:08:31 +00:00
|
|
|
|
size_t getTotalByteCount() const final;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
2020-04-13 12:32:45 +00:00
|
|
|
|
bool alwaysReturnsEmptySet() const final { return isInnerOrRight(getKind()) && data->empty && !overDictionary(); }
|
2019-10-11 17:27:54 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
ASTTableJoin::Kind getKind() const { return kind; }
|
2019-08-14 15:11:20 +00:00
|
|
|
|
ASTTableJoin::Strictness getStrictness() const { return strictness; }
|
2020-06-01 09:38:46 +00:00
|
|
|
|
TypeIndex getAsofType() const { return *asof_type; }
|
2019-10-11 17:56:26 +00:00
|
|
|
|
ASOF::Inequality getAsofInequality() const { return asof_inequality; }
|
2019-05-07 18:21:44 +00:00
|
|
|
|
bool anyTakeLastRow() const { return any_take_last_row; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/// Different types of keys for maps.
|
|
|
|
|
#define APPLY_FOR_JOIN_VARIANTS(M) \
|
2017-11-12 00:46:22 +00:00
|
|
|
|
M(key8) \
|
|
|
|
|
M(key16) \
|
|
|
|
|
M(key32) \
|
|
|
|
|
M(key64) \
|
|
|
|
|
M(key_string) \
|
|
|
|
|
M(key_fixed_string) \
|
|
|
|
|
M(keys128) \
|
|
|
|
|
M(keys256) \
|
2017-04-01 07:20:54 +00:00
|
|
|
|
M(hashed)
|
|
|
|
|
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
|
|
/// Used for reading from StorageJoin and applying joinGet function
|
|
|
|
|
#define APPLY_FOR_JOIN_VARIANTS_LIMITED(M) \
|
|
|
|
|
M(key8) \
|
|
|
|
|
M(key16) \
|
|
|
|
|
M(key32) \
|
|
|
|
|
M(key64) \
|
|
|
|
|
M(key_string) \
|
|
|
|
|
M(key_fixed_string)
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
enum class Type
|
|
|
|
|
{
|
|
|
|
|
EMPTY,
|
|
|
|
|
CROSS,
|
2020-04-08 18:59:52 +00:00
|
|
|
|
DICT,
|
2017-04-01 07:20:54 +00:00
|
|
|
|
#define M(NAME) NAME,
|
|
|
|
|
APPLY_FOR_JOIN_VARIANTS(M)
|
|
|
|
|
#undef M
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Different data structures, that are used to perform JOIN.
|
|
|
|
|
*/
|
|
|
|
|
template <typename Mapped>
|
|
|
|
|
struct MapsTemplate
|
|
|
|
|
{
|
2019-02-28 09:35:38 +00:00
|
|
|
|
std::unique_ptr<FixedHashMap<UInt8, Mapped>> key8;
|
|
|
|
|
std::unique_ptr<FixedHashMap<UInt16, Mapped>> key16;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::unique_ptr<HashMap<UInt32, Mapped, HashCRC32<UInt32>>> key32;
|
|
|
|
|
std::unique_ptr<HashMap<UInt64, Mapped, HashCRC32<UInt64>>> key64;
|
2017-11-12 00:46:22 +00:00
|
|
|
|
std::unique_ptr<HashMapWithSavedHash<StringRef, Mapped>> key_string;
|
|
|
|
|
std::unique_ptr<HashMapWithSavedHash<StringRef, Mapped>> key_fixed_string;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::unique_ptr<HashMap<UInt128, Mapped, UInt128HashCRC32>> keys128;
|
|
|
|
|
std::unique_ptr<HashMap<UInt256, Mapped, UInt256HashCRC32>> keys256;
|
2017-11-12 00:46:22 +00:00
|
|
|
|
std::unique_ptr<HashMap<UInt128, Mapped, UInt128TrivialHash>> hashed;
|
2019-03-19 16:53:36 +00:00
|
|
|
|
|
|
|
|
|
void create(Type which)
|
|
|
|
|
{
|
|
|
|
|
switch (which)
|
|
|
|
|
{
|
|
|
|
|
case Type::EMPTY: break;
|
|
|
|
|
case Type::CROSS: break;
|
2020-04-08 18:59:52 +00:00
|
|
|
|
case Type::DICT: break;
|
2019-03-19 16:53:36 +00:00
|
|
|
|
|
|
|
|
|
#define M(NAME) \
|
|
|
|
|
case Type::NAME: NAME = std::make_unique<typename decltype(NAME)::element_type>(); break;
|
|
|
|
|
APPLY_FOR_JOIN_VARIANTS(M)
|
|
|
|
|
#undef M
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t getTotalRowCount(Type which) const
|
|
|
|
|
{
|
|
|
|
|
switch (which)
|
|
|
|
|
{
|
|
|
|
|
case Type::EMPTY: return 0;
|
|
|
|
|
case Type::CROSS: return 0;
|
2020-04-08 18:59:52 +00:00
|
|
|
|
case Type::DICT: return 0;
|
2019-03-19 16:53:36 +00:00
|
|
|
|
|
|
|
|
|
#define M(NAME) \
|
|
|
|
|
case Type::NAME: return NAME ? NAME->size() : 0;
|
|
|
|
|
APPLY_FOR_JOIN_VARIANTS(M)
|
|
|
|
|
#undef M
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t getTotalByteCountImpl(Type which) const
|
|
|
|
|
{
|
|
|
|
|
switch (which)
|
|
|
|
|
{
|
|
|
|
|
case Type::EMPTY: return 0;
|
|
|
|
|
case Type::CROSS: return 0;
|
2020-04-08 18:59:52 +00:00
|
|
|
|
case Type::DICT: return 0;
|
2019-03-19 16:53:36 +00:00
|
|
|
|
|
|
|
|
|
#define M(NAME) \
|
|
|
|
|
case Type::NAME: return NAME ? NAME->getBufferSizeInBytes() : 0;
|
|
|
|
|
APPLY_FOR_JOIN_VARIANTS(M)
|
|
|
|
|
#undef M
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-11-07 21:32:44 +00:00
|
|
|
|
using MapsOne = MapsTemplate<JoinStuff::MappedOne>;
|
2019-05-07 18:21:44 +00:00
|
|
|
|
using MapsAll = MapsTemplate<JoinStuff::MappedAll>;
|
2019-11-07 21:32:44 +00:00
|
|
|
|
using MapsOneFlagged = MapsTemplate<JoinStuff::MappedOneFlagged>;
|
|
|
|
|
using MapsAllFlagged = MapsTemplate<JoinStuff::MappedAllFlagged>;
|
2019-05-07 18:21:44 +00:00
|
|
|
|
using MapsAsof = MapsTemplate<JoinStuff::MappedAsof>;
|
2018-12-30 15:54:45 +00:00
|
|
|
|
|
2019-11-07 21:32:44 +00:00
|
|
|
|
using MapsVariant = std::variant<MapsOne, MapsAll, MapsOneFlagged, MapsAllFlagged, MapsAsof>;
|
2019-12-19 15:50:28 +00:00
|
|
|
|
using BlockNullmapList = std::deque<std::pair<const Block *, ColumnPtr>>;
|
|
|
|
|
|
|
|
|
|
struct RightTableData
|
|
|
|
|
{
|
|
|
|
|
/// Protect state for concurrent use in insertFromBlock and joinBlock.
|
|
|
|
|
/// @note that these methods could be called simultaneously only while use of StorageJoin.
|
|
|
|
|
mutable std::shared_mutex rwlock;
|
|
|
|
|
|
|
|
|
|
Type type = Type::EMPTY;
|
|
|
|
|
bool empty = true;
|
|
|
|
|
|
|
|
|
|
MapsVariant maps;
|
|
|
|
|
Block sample_block; /// Block as it would appear in the BlockList
|
|
|
|
|
BlocksList blocks; /// Blocks of "right" table.
|
|
|
|
|
BlockNullmapList blocks_nullmaps; /// Nullmaps for blocks of "right" table (if needed)
|
|
|
|
|
|
|
|
|
|
/// Additional data - strings for string keys and continuation elements of single-linked lists of references to rows.
|
|
|
|
|
Arena pool;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-07 09:48:47 +00:00
|
|
|
|
void reuseJoinedData(const HashJoin & join)
|
2019-12-19 15:50:28 +00:00
|
|
|
|
{
|
|
|
|
|
data = join.data;
|
|
|
|
|
}
|
2014-06-18 20:08:31 +00:00
|
|
|
|
|
2020-02-17 17:08:31 +00:00
|
|
|
|
std::shared_ptr<RightTableData> getJoinedData() const
|
|
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-18 19:14:29 +00:00
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
friend class NonJoinedBlockInputStream;
|
2020-01-31 14:51:09 +00:00
|
|
|
|
friend class JoinSource;
|
2015-04-17 08:46:06 +00:00
|
|
|
|
|
2020-04-07 09:48:47 +00:00
|
|
|
|
std::shared_ptr<TableJoin> table_join;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
ASTTableJoin::Kind kind;
|
|
|
|
|
ASTTableJoin::Strictness strictness;
|
2014-06-18 19:14:29 +00:00
|
|
|
|
|
2019-09-11 15:57:09 +00:00
|
|
|
|
/// Names of key columns in right-side table (in the order they appear in ON/USING clause). @note It could contain duplicates.
|
2019-09-16 12:37:46 +00:00
|
|
|
|
const Names & key_names_right;
|
2014-06-18 19:14:29 +00:00
|
|
|
|
|
2019-12-19 15:50:28 +00:00
|
|
|
|
bool nullable_right_side; /// In case of LEFT and FULL joins, if use_nulls, convert right-side columns to Nullable.
|
|
|
|
|
bool nullable_left_side; /// In case of RIGHT and FULL joins, if use_nulls, convert left-side columns to Nullable.
|
|
|
|
|
bool any_take_last_row; /// Overwrite existing values when encountering the same key again
|
2020-06-01 09:38:46 +00:00
|
|
|
|
std::optional<TypeIndex> asof_type;
|
2019-10-11 17:56:26 +00:00
|
|
|
|
ASOF::Inequality asof_inequality;
|
2015-03-02 01:10:58 +00:00
|
|
|
|
|
2019-12-19 15:50:28 +00:00
|
|
|
|
/// Right table data. StorageJoin shares it between many Join objects.
|
|
|
|
|
std::shared_ptr<RightTableData> data;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Sizes key_sizes;
|
2014-06-12 02:31:30 +00:00
|
|
|
|
|
2017-04-27 17:48:30 +00:00
|
|
|
|
/// Block with columns from the right-side table except key columns.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Block sample_block_with_columns_to_add;
|
2019-09-11 15:57:09 +00:00
|
|
|
|
/// Block with key columns in the same order they appear in the right-side table (duplicates appear once).
|
|
|
|
|
Block right_table_keys;
|
2019-11-01 17:41:07 +00:00
|
|
|
|
/// Block with key columns right-side table keys that are needed in result (would be attached after joined columns).
|
|
|
|
|
Block required_right_keys;
|
|
|
|
|
/// Left table column names that are sources for required_right_keys columns
|
|
|
|
|
std::vector<String> required_right_keys_sources;
|
2015-05-26 00:37:48 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Poco::Logger * log;
|
2014-07-06 19:48:39 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Block totals;
|
2015-04-16 09:55:24 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
void init(Type type_);
|
2014-06-18 20:08:31 +00:00
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
|
/** Set information about structure of right hand of JOIN (joined data).
|
|
|
|
|
*/
|
|
|
|
|
void setSampleBlock(const Block & block);
|
|
|
|
|
|
2019-12-19 15:50:28 +00:00
|
|
|
|
const Block & savedBlockSample() const { return data->sample_block; }
|
|
|
|
|
|
2019-12-03 14:38:13 +00:00
|
|
|
|
/// Modify (structure) right block to save it in block list
|
2019-12-03 13:31:52 +00:00
|
|
|
|
Block structureRightBlock(const Block & stored_block) const;
|
2020-02-18 12:41:23 +00:00
|
|
|
|
void initRightBlockStructure(Block & saved_block_sample);
|
2019-11-01 17:41:07 +00:00
|
|
|
|
void initRequiredRightKeys();
|
2019-04-01 10:35:37 +00:00
|
|
|
|
|
2019-11-07 12:42:39 +00:00
|
|
|
|
template <ASTTableJoin::Kind KIND, ASTTableJoin::Strictness STRICTNESS, typename Maps>
|
|
|
|
|
void joinBlockImpl(
|
|
|
|
|
Block & block,
|
2018-11-30 14:49:35 +00:00
|
|
|
|
const Names & key_names_left,
|
2019-11-07 12:42:39 +00:00
|
|
|
|
const Block & block_with_columns_to_add,
|
|
|
|
|
const Maps & maps) const;
|
2014-06-18 18:31:35 +00:00
|
|
|
|
|
2020-04-06 13:39:57 +00:00
|
|
|
|
void joinBlockImplCross(Block & block, ExtraBlockPtr & not_processed) const;
|
2019-11-07 12:42:39 +00:00
|
|
|
|
|
|
|
|
|
template <typename Maps>
|
2020-04-07 14:52:32 +00:00
|
|
|
|
void joinGetImpl(Block & block, const Block & block_with_columns_to_add, const Maps & maps_) const;
|
2019-12-19 15:50:28 +00:00
|
|
|
|
|
|
|
|
|
static Type chooseMethod(const ColumnRawPtrs & key_columns, Sizes & key_sizes);
|
2014-06-12 02:31:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|