2019-09-18 12:46:57 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2020-07-10 18:10:06 +00:00
|
|
|
#include <Core/Block.h>
|
2019-09-18 12:46:57 +00:00
|
|
|
#include <Interpreters/IJoin.h>
|
2021-01-31 19:25:47 +00:00
|
|
|
#include <Interpreters/ActionsDAG.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2019-09-18 12:46:57 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ColumnWithTypeAndName;
|
2020-07-10 18:10:06 +00:00
|
|
|
class TableJoin;
|
2019-09-18 12:46:57 +00:00
|
|
|
class IColumn;
|
2021-09-06 10:59:18 +00:00
|
|
|
|
2019-09-18 12:46:57 +00:00
|
|
|
using ColumnRawPtrs = std::vector<const IColumn *>;
|
2022-12-27 17:49:17 +00:00
|
|
|
using ColumnPtrMap = std::unordered_map<String, ColumnPtr>;
|
2021-09-06 10:59:18 +00:00
|
|
|
using ColumnRawPtrMap = std::unordered_map<String, const IColumn *>;
|
2021-07-21 17:03:33 +00:00
|
|
|
using UInt8ColumnDataPtr = const ColumnUInt8::Container *;
|
2019-09-18 12:46:57 +00:00
|
|
|
|
|
|
|
namespace JoinCommon
|
|
|
|
{
|
2021-09-09 11:04:37 +00:00
|
|
|
|
2023-01-20 16:30:34 +00:00
|
|
|
/// Helper interface to work with mask from JOIN ON section
|
2021-11-08 12:36:34 +00:00
|
|
|
class JoinMask
|
|
|
|
{
|
|
|
|
public:
|
2022-12-28 13:20:58 +00:00
|
|
|
explicit JoinMask()
|
2021-11-08 12:36:34 +00:00
|
|
|
: column(nullptr)
|
2022-12-28 13:20:58 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
explicit JoinMask(bool value, size_t size)
|
|
|
|
: column(ColumnUInt8::create(size, value))
|
2021-11-08 12:36:34 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
explicit JoinMask(ColumnPtr col)
|
|
|
|
: column(col)
|
|
|
|
{}
|
|
|
|
|
2022-12-28 13:20:58 +00:00
|
|
|
bool hasData()
|
|
|
|
{
|
|
|
|
return column != nullptr;
|
|
|
|
}
|
2021-11-08 12:36:34 +00:00
|
|
|
|
|
|
|
UInt8ColumnDataPtr getData()
|
|
|
|
{
|
|
|
|
if (column)
|
|
|
|
return &assert_cast<const ColumnUInt8 &>(*column).getData();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isRowFiltered(size_t row) const
|
|
|
|
{
|
2022-12-28 13:20:58 +00:00
|
|
|
return !assert_cast<const ColumnUInt8 &>(*column).getData()[row];
|
2021-11-08 12:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ColumnPtr column;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-04-17 19:03:32 +00:00
|
|
|
bool canBecomeNullable(const DataTypePtr & type);
|
|
|
|
DataTypePtr convertTypeToNullable(const DataTypePtr & type);
|
2021-08-06 11:54:04 +00:00
|
|
|
void convertColumnToNullable(ColumnWithTypeAndName & column);
|
2019-09-18 12:46:57 +00:00
|
|
|
void convertColumnsToNullable(Block & block, size_t starting_pos = 0);
|
2021-06-25 12:03:10 +00:00
|
|
|
void convertColumnsToNullable(MutableColumns & mutable_columns, size_t starting_pos = 0);
|
2019-10-29 19:39:42 +00:00
|
|
|
void removeColumnNullability(ColumnWithTypeAndName & column);
|
2020-07-10 18:10:06 +00:00
|
|
|
void changeColumnRepresentation(const ColumnPtr & src_column, ColumnPtr & dst_column);
|
|
|
|
ColumnPtr emptyNotNullableClone(const ColumnPtr & column);
|
2021-07-21 17:03:33 +00:00
|
|
|
ColumnPtr materializeColumn(const Block & block, const String & name);
|
2019-12-17 15:02:42 +00:00
|
|
|
Columns materializeColumns(const Block & block, const Names & names);
|
|
|
|
ColumnRawPtrs materializeColumnsInplace(Block & block, const Names & names);
|
|
|
|
ColumnRawPtrs getRawPointers(const Columns & columns);
|
2021-08-06 14:15:11 +00:00
|
|
|
void restoreLowCardinalityInplace(Block & block, const Names & lowcard_keys);
|
2019-09-18 12:46:57 +00:00
|
|
|
|
2020-03-13 08:15:43 +00:00
|
|
|
ColumnRawPtrs extractKeysForJoin(const Block & block_keys, const Names & key_names_right);
|
2019-09-18 12:46:57 +00:00
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
/// Throw an exception if join condition column is not UIint8
|
|
|
|
void checkTypesOfMasks(const Block & block_left, const String & condition_name_left,
|
|
|
|
const Block & block_right, const String & condition_name_right);
|
|
|
|
|
|
|
|
/// Throw an exception if blocks have different types of key columns . Compare up to Nullability.
|
|
|
|
void checkTypesOfKeys(const Block & block_left, const Names & key_names_left,
|
|
|
|
const Block & block_right, const Names & key_names_right);
|
|
|
|
|
|
|
|
/// Check both keys and conditions
|
|
|
|
void checkTypesOfKeys(const Block & block_left, const Names & key_names_left, const String & condition_name_left,
|
|
|
|
const Block & block_right, const Names & key_names_right, const String & condition_name_right);
|
2019-09-18 12:46:57 +00:00
|
|
|
|
|
|
|
void createMissedColumns(Block & block);
|
2021-07-14 10:02:23 +00:00
|
|
|
void joinTotals(Block left_totals, Block right_totals, const TableJoin & table_join, Block & out_block);
|
2019-09-18 12:46:57 +00:00
|
|
|
|
2021-01-21 14:18:57 +00:00
|
|
|
void addDefaultValues(IColumn & column, const DataTypePtr & type, size_t count);
|
2021-01-21 13:46:46 +00:00
|
|
|
|
2021-02-09 13:17:42 +00:00
|
|
|
bool typesEqualUpToNullability(DataTypePtr left_type, DataTypePtr right_type);
|
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
/// Return mask array of type ColumnUInt8 for specified column. Source should have type UInt8 or Nullable(UInt8).
|
2021-11-08 12:36:34 +00:00
|
|
|
JoinMask getColumnAsMask(const Block & block, const String & column_name);
|
2021-07-21 17:03:33 +00:00
|
|
|
|
|
|
|
/// Split key and other columns by keys name list
|
|
|
|
void splitAdditionalColumns(const Names & key_names, const Block & sample_block, Block & block_keys, Block & block_others);
|
|
|
|
|
2021-05-15 11:39:13 +00:00
|
|
|
void changeLowCardinalityInplace(ColumnWithTypeAndName & column);
|
|
|
|
|
2022-06-15 15:11:24 +00:00
|
|
|
Blocks scatterBlockByHash(const Strings & key_columns_names, const Block & block, size_t num_shards);
|
2022-10-05 12:40:32 +00:00
|
|
|
Blocks scatterBlockByHash(const Strings & key_columns_names, const Blocks & blocks, size_t num_shards);
|
|
|
|
Blocks scatterBlockByHash(const Strings & key_columns_names, const BlocksList & blocks, size_t num_shards);
|
2022-06-15 15:11:24 +00:00
|
|
|
|
|
|
|
bool hasNonJoinedBlocks(const TableJoin & table_join);
|
2022-06-11 11:03:44 +00:00
|
|
|
|
2022-07-07 12:26:34 +00:00
|
|
|
/// Insert default values for rows marked in filter
|
|
|
|
ColumnPtr filterWithBlanks(ColumnPtr src_column, const IColumn::Filter & filter, bool inverse_filter = false);
|
|
|
|
|
2019-09-18 12:46:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 18:10:06 +00:00
|
|
|
/// Creates result from right table data in RIGHT and FULL JOIN when keys are not present in left table.
|
2022-10-04 08:20:13 +00:00
|
|
|
class NotJoinedBlocks final : public IBlocksStream
|
2020-07-10 18:10:06 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-08-06 14:15:11 +00:00
|
|
|
using LeftToRightKeyRemap = std::unordered_map<String, String>;
|
2020-07-10 18:10:06 +00:00
|
|
|
|
2021-08-09 14:30:37 +00:00
|
|
|
/// Returns non joined columns from right part of join
|
|
|
|
class RightColumnsFiller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Create empty block for right part
|
|
|
|
virtual Block getEmptyBlock() = 0;
|
|
|
|
/// Fill columns from right part of join with not joined rows
|
|
|
|
virtual size_t fillColumns(MutableColumns & columns_right) = 0;
|
|
|
|
|
|
|
|
virtual ~RightColumnsFiller() = default;
|
|
|
|
};
|
|
|
|
|
2021-08-17 13:30:01 +00:00
|
|
|
NotJoinedBlocks(std::unique_ptr<RightColumnsFiller> filler_,
|
2021-08-06 14:15:11 +00:00
|
|
|
const Block & result_sample_block_,
|
|
|
|
size_t left_columns_count,
|
2022-12-05 17:54:12 +00:00
|
|
|
const TableJoin & table_join);
|
2020-07-10 18:10:06 +00:00
|
|
|
|
2022-10-05 12:40:32 +00:00
|
|
|
Block nextImpl() override;
|
2021-08-06 14:15:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void extractColumnChanges(size_t right_pos, size_t result_pos);
|
2021-08-06 11:54:04 +00:00
|
|
|
void correctLowcardAndNullability(Block & block);
|
2020-07-10 18:10:06 +00:00
|
|
|
void addLeftColumns(Block & block, size_t rows_added) const;
|
|
|
|
void addRightColumns(Block & block, MutableColumns & columns_right) const;
|
|
|
|
void copySameKeys(Block & block) const;
|
|
|
|
|
2021-08-09 14:30:37 +00:00
|
|
|
std::unique_ptr<RightColumnsFiller> filler;
|
2020-07-10 18:10:06 +00:00
|
|
|
|
2021-08-06 14:15:11 +00:00
|
|
|
/// Right block saved in Join
|
2020-07-10 18:10:06 +00:00
|
|
|
Block saved_block_sample;
|
2021-07-21 17:03:33 +00:00
|
|
|
|
2021-08-06 14:15:11 +00:00
|
|
|
/// Output of join
|
|
|
|
Block result_sample_block;
|
2020-07-10 18:10:06 +00:00
|
|
|
|
|
|
|
/// Indices of columns in result_sample_block that should be generated
|
|
|
|
std::vector<size_t> column_indices_left;
|
|
|
|
/// Indices of columns that come from the right-side table: right_pos -> result_pos
|
|
|
|
std::unordered_map<size_t, size_t> column_indices_right;
|
2021-08-06 14:15:11 +00:00
|
|
|
|
2020-07-10 18:10:06 +00:00
|
|
|
std::unordered_map<size_t, size_t> same_result_keys;
|
2021-08-06 14:15:11 +00:00
|
|
|
|
|
|
|
/// Which right columns (saved in parent) need Nullability/LowCardinality change
|
|
|
|
/// before placing them in result block
|
2021-08-06 11:54:04 +00:00
|
|
|
std::vector<std::pair<size_t, bool>> right_nullability_changes;
|
|
|
|
std::vector<std::pair<size_t, bool>> right_lowcard_changes;
|
2020-07-10 18:10:06 +00:00
|
|
|
|
|
|
|
void setRightIndex(size_t right_pos, size_t result_position);
|
|
|
|
};
|
|
|
|
|
2019-09-18 12:46:57 +00:00
|
|
|
}
|