MySQL 8.0.39
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, bool uses_match_flags);
88
89 /// @returns the number of rows in this HashJoinChunk
90 ha_rows num_rows() const { return m_num_rows; }
91
92 /// Write a row to the HashJoinChunk.
93 ///
94 /// Read the row that lies in the record buffer (record[0]) of the given
95 /// tables and write it out to the underlying file. If the QEP_TAB signals
96 /// that the row ID should be kept, it is also written out. Note that
97 /// TABLE::read_set is used to signal which columns that should be written to
98 /// the chunk.
99 ///
100 /// @param buffer a buffer that is used when copying data from the tables to
101 /// the chunk file. Note that any existing data in "buffer" is overwritten.
102 /// @param matched whether this row has seen a matching row from the other
103 /// input. The flag is only written if 'm_uses_match_flags' is set, and if
104 /// the row comes from the probe input.
105 ///
106 /// @retval true on error.
107 bool WriteRowToChunk(String *buffer, bool matched);
108
109 /// Read a row from the HashJoinChunk and put it in the record buffer.
110 ///
111 /// The function will read a row from file on disk and put it in the record
112 /// buffers (table->record[0]) in the provided tables. The file on disk should
113 /// already be pointing to the start of a row.
114 ///
115 /// @param buffer a buffer that is used when copying data from the chunk file
116 /// to the tables. Note that any existing data in "buffer" is overwritten.
117 ///
118 /// @param[out] matched whether this row has seen a matching row from the
119 /// other input. The flag is only restored if 'm_uses_match_flags' is set,
120 /// and if the row comes from the probe input.
121 /// @retval true on error.
122 bool LoadRowFromChunk(String *buffer, bool *matched);
123
124 /// Flush the file buffer, and prepare the file for reading.
125 ///
126 /// @retval true on error
127 bool Rewind();
128
129 private:
130 // A collection of which tables the chunk file holds data from. Used to
131 // determine where to read data from, and where to put the data back.
133
134 // The number of rows in this chunk file.
136
137 // The underlying file that is used when reading data to and from disk.
139
140 // Whether every row is prefixed with a match flag.
142};
143
144#endif // SQL_ITERATORS_HASH_JOIN_CHUNK_H_
Definition: hash_join_chunk.h:67
bool LoadRowFromChunk(String *buffer, bool *matched)
Read a row from the HashJoinChunk and put it in the record buffer.
Definition: hash_join_chunk.cc:125
IO_CACHE m_file
Definition: hash_join_chunk.h:138
ha_rows m_num_rows
Definition: hash_join_chunk.h:135
HashJoinChunk()=default
ha_rows num_rows() const
Definition: hash_join_chunk.h:90
pack_rows::TableCollection m_tables
Definition: hash_join_chunk.h:132
bool WriteRowToChunk(String *buffer, bool matched)
Write a row to the HashJoinChunk.
Definition: hash_join_chunk.cc:92
bool m_uses_match_flags
Definition: hash_join_chunk.h:141
HashJoinChunk & operator=(HashJoinChunk &&other)
Definition: hash_join_chunk.cc:53
bool Rewind()
Flush the file buffer, and prepare the file for reading.
Definition: hash_join_chunk.cc:82
~HashJoinChunk()
Definition: hash_join_chunk.cc:70
HashJoinChunk(const HashJoinChunk &obj)=delete
bool Init(const pack_rows::TableCollection &tables, bool uses_match_flags)
Initialize this HashJoinChunk.
Definition: hash_join_chunk.cc:72
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
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:1140
Common header for many mysys elements.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
Generic routines for packing rows (possibly from multiple tables at the same time) into strings,...
Definition: my_sys.h:341