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