MySQL 8.3.0
Source Code Documentation
hash_join_chunk.h
Go to the documentation of this file.
1#ifndef SQL_ITERATORS_HASH_JOIN_CHUNK_H_
2#define SQL_ITERATORS_HASH_JOIN_CHUNK_H_
3
4/* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include "my_base.h"
27#include "my_sys.h"
28#include "sql/pack_rows.h"
29
30class String;
31
32// A HashJoinChunk is a file located on disk that can be used to store rows.
33// It is used in on-disk hash join when a table is to be partitioned out to
34// several smaller files, a.k.a. HashJoinChunks.
35//
36// When writing a column to a HashJoinChunk, we use StoreFromTableBuffers for
37// converting the necessary columns into a format suitable for storage on disk.
38// Conveniently, StoreFromTableBuffers creates a contiguous range of bytes and a
39// corresponding length that easily and efficiently can be written out to the
40// file. When reading rows back from a file, LoadIntoTableBuffers() is used to
41// put the row back into the table record buffers.
42//
43// The basic usage goes like this:
44//
45// HashJoinChunk chunk;
46// // Initialize a chunk to hold data from the given tables without any match
47// // flags.
48// chunk.Init(tables, /*uses_match_flags=*/false);
49// String buffer; // A buffer that is used when copying data between tables
50// // and the chunk file, and vica versa.
51// while (iterator->Read() == 0) {
52// // Write the row that lies in the record buffers of "tables" to this
53// // chunk, using the provided buffer. If the chunk file was initialized to
54// // use match flags, we would prefix the row with a match flag saying that
55// // this row did not have any matching row.
56// chunk.WriteRowToChunk(&buffer, /*matched=*/false);
57// };
58//
59// chunk.Rewind(); // Prepare to read the first row in this chunk.
60//
61// bool match_flag,
62// // Put the row from the chunk to the record buffers of "tables", using the
63// // provided buffer. If the chunk file was initialized to use match flags,
64// // the match flag for the row read would be stored in 'match_flag'.
65// chunk.LoadRowFromChunk(&buffer, &match_flag);
67 public:
68 HashJoinChunk() = default; // Constructible.
69
70 HashJoinChunk(HashJoinChunk &&other); // Movable.
71
72 HashJoinChunk(const HashJoinChunk &obj) = delete;
73
75
77
78 /// Initialize this HashJoinChunk.
79 ///
80 /// @param tables The tables to store row data from. Which column we store in
81 /// the chunk file is determined by each tables read set.
82 /// @param uses_match_flags Whether each row should be prefixed with a match
83 /// flag, saying whether the row had a matching row.
84 ///
85 /// @returns true if the initialization failed.
86 bool Init(const pack_rows::TableCollection &tables,
87 bool uses_match_flags = false);
88
89 /// @returns the number of rows in this chunk
90 ha_rows NumRows() const { return m_num_rows; }
91
92 /// Set the number of rows we currently care about in this chunk.
93 /// Used to keep track of number of rows written from a certain
94 /// point in time (the counter is incremented by writing).
95 /// @param no the number to set the counter to
96 void SetNumRows(ha_rows no) { m_num_rows = no; }
97
98 /// Write a row to the HashJoinChunk.
99 ///
100 /// Read the row that lies in the record buffer (record[0]) of the given
101 /// tables and write it out to the underlying file. If the QEP_TAB signals
102 /// that the row ID should be kept, it is also written out. Note that
103 /// TABLE::read_set is used to signal which columns that should be written to
104 /// the chunk.
105 ///
106 /// @param buffer a buffer that is used when copying data from the tables to
107 /// the chunk file. Note that any existing data in "buffer" is overwritten.
108 /// @param matched whether this row has seen a matching row from the other
109 /// input. The flag is only written if 'm_uses_match_flags' is set, and if
110 /// the row comes from the probe input.
111 /// @param file_set_no Used by set operations only: the zero based chunk file
112 /// set number. If equal to std::numeric_limits<size_t>::max(), ignore (
113 /// default value).
114 ///
115 /// @retval true on error.
116 bool WriteRowToChunk(String *buffer, bool matched,
117 size_t file_set_no = std::numeric_limits<size_t>::max());
118
119 /// Read a row from the HashJoinChunk and put it in the record buffer.
120 ///
121 /// The function will read a row from file on disk and put it in the record
122 /// buffers (table->record[0]) in the provided tables. The file on disk should
123 /// already be pointing to the start of a row.
124 ///
125 /// @param buffer a buffer that is used when copying data from the chunk file
126 /// to the tables. Note that any existing data in "buffer" is overwritten.
127 ///
128 /// @param[out] matched whether this row has seen a matching row from the
129 /// other input. The flag is only restored if 'm_uses_match_flags' is set,
130 /// and if the row comes from the probe input.
131 /// @param file_set_no Used by set operations only: the zero based chunk file
132 /// set number. If not nullptr, set this to current set file number. Note:
133 /// If WriteRowToChunk used a non ignorable file_set_no, it is expected
134 /// that a non nullptr value be provided here for reading of rows to proceed
135 /// correctly.
136 /// @retval true on error.
137 bool LoadRowFromChunk(String *buffer, bool *matched,
138 size_t *file_set_no = nullptr);
139
140 /// Flush the file buffer, and prepare the file for reading.
141 ///
142 /// @retval true on error
143 bool Rewind();
144
145 /// Switch from reading to writing, saving current read position
146 /// m_last_read_pos. Continue writing from m_last_write_pos.
147 /// @retval true on error
148 bool SetAppend();
149
150 /// Switch from writing to reading, saving current write position in
151 /// m_last_write_pos. Continue reading from m_last_read_pos.
152 /// @retval true on error
153 bool ContinueRead();
154
155 private:
156 // A collection of which tables the chunk file holds data from. Used to
157 // determine where to read data from, and where to put the data back.
159
160 // The number of rows in this chunk file.
162
163 // The underlying file that is used when reading data to and from disk.
165
166 // Whether every row is prefixed with a match flag.
168
169 // Used to resume writing and reading from previous position
172};
173
174#endif // SQL_ITERATORS_HASH_JOIN_CHUNK_H_
Definition: hash_join_chunk.h:66
bool ContinueRead()
Switch from writing to reading, saving current write position in m_last_write_pos.
Definition: hash_join_chunk.cc:113
IO_CACHE m_file
Definition: hash_join_chunk.h:164
ha_rows NumRows() const
Definition: hash_join_chunk.h:90
bool SetAppend()
Switch from reading to writing, saving current read position m_last_read_pos.
Definition: hash_join_chunk.cc:100
bool Init(const pack_rows::TableCollection &tables, bool uses_match_flags=false)
Initialize this HashJoinChunk.
Definition: hash_join_chunk.cc:70
ha_rows m_num_rows
Definition: hash_join_chunk.h:161
HashJoinChunk()=default
size_t m_last_read_pos
Definition: hash_join_chunk.h:171
pack_rows::TableCollection m_tables
Definition: hash_join_chunk.h:158
bool m_uses_match_flags
Definition: hash_join_chunk.h:167
HashJoinChunk & operator=(HashJoinChunk &&other)
Definition: hash_join_chunk.cc:51
bool Rewind()
Flush the file buffer, and prepare the file for reading.
Definition: hash_join_chunk.cc:82
bool LoadRowFromChunk(String *buffer, bool *matched, size_t *file_set_no=nullptr)
Read a row from the HashJoinChunk and put it in the record buffer.
Definition: hash_join_chunk.cc:168
~HashJoinChunk()
Definition: hash_join_chunk.cc:68
bool WriteRowToChunk(String *buffer, bool matched, size_t file_set_no=std::numeric_limits< size_t >::max())
Write a row to the HashJoinChunk.
Definition: hash_join_chunk.cc:127
size_t m_last_write_pos
Definition: hash_join_chunk.h:170
HashJoinChunk(const HashJoinChunk &obj)=delete
void SetNumRows(ha_rows no)
Set the number of rows we currently care about in this chunk.
Definition: hash_join_chunk.h:96
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
A structure that contains a list of input tables for a hash join operation, BKA join operation or a s...
Definition: pack_rows.h:92
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1140
Common header for many mysys elements.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
Generic routines for packing rows (possibly from multiple tables at the same time) into strings,...
Definition: my_sys.h:345