MySQL  8.0.27
Source Code Documentation
hash_join_chunk.h
Go to the documentation of this file.
1 #ifndef SQL_HASH_JOIN_CHUNK_H_
2 #define SQL_HASH_JOIN_CHUNK_H_
3 
4 /* Copyright (c) 2019, 2021, 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 
30 class 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, bool uses_match_flags);
87 
88  /// @returns the number of rows in this HashJoinChunk
89  ha_rows num_rows() const { return m_num_rows; }
90 
91  /// Write a row to the HashJoinChunk.
92  ///
93  /// Read the row that lies in the record buffer (record[0]) of the given
94  /// tables and write it out to the underlying file. If the QEP_TAB signals
95  /// that the row ID should be kept, it is also written out. Note that
96  /// TABLE::read_set is used to signal which columns that should be written to
97  /// the chunk.
98  ///
99  /// @param buffer a buffer that is used when copying data from the tables to
100  /// the chunk file. Note that any existing data in "buffer" is overwritten.
101  /// @param matched whether this row has seen a matching row from the other
102  /// input. The flag is only written if 'm_uses_match_flags' is set, and if
103  /// the row comes from the probe input.
104  ///
105  /// @retval true on error.
106  bool WriteRowToChunk(String *buffer, bool matched);
107 
108  /// Read a row from the HashJoinChunk and put it in the record buffer.
109  ///
110  /// The function will read a row from file on disk and put it in the record
111  /// buffers (table->record[0]) in the provided tables. The file on disk should
112  /// already be pointing to the start of a row.
113  ///
114  /// @param buffer a buffer that is used when copying data from the chunk file
115  /// to the tables. Note that any existing data in "buffer" is overwritten.
116  ///
117  /// @param[out] matched whether this row has seen a matching row from the
118  /// other input. The flag is only restored if 'm_uses_match_flags' is set,
119  /// and if the row comes from the probe input.
120  /// @retval true on error.
121  bool LoadRowFromChunk(String *buffer, bool *matched);
122 
123  /// Flush the file buffer, and prepare the file for reading.
124  ///
125  /// @retval true on error
126  bool Rewind();
127 
128  private:
129  // A collection of which tables the chunk file holds data from. Used to
130  // determine where to read data from, and where to put the data back.
132 
133  // The number of rows in this chunk file.
135 
136  // The underlying file that is used when reading data to and from disk.
138 
139  // Whether every row is prefixed with a match flag.
140  bool m_uses_match_flags{false};
141 };
142 
143 #endif // SQL_HASH_JOIN_CHUNK_H_
Definition: hash_join_chunk.h:66
bool LoadRowFromChunk(String *buffer, bool *matched)
Read a row from the HashJoinChunk and put it in the record buffer.
Definition: hash_join_chunk.cc:124
IO_CACHE m_file
Definition: hash_join_chunk.h:137
ha_rows m_num_rows
Definition: hash_join_chunk.h:134
HashJoinChunk()=default
ha_rows num_rows() const
Definition: hash_join_chunk.h:89
pack_rows::TableCollection m_tables
Definition: hash_join_chunk.h:131
bool WriteRowToChunk(String *buffer, bool matched)
Write a row to the HashJoinChunk.
Definition: hash_join_chunk.cc:91
bool m_uses_match_flags
Definition: hash_join_chunk.h:140
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:81
~HashJoinChunk()
Definition: hash_join_chunk.cc:69
HashJoinChunk(const HashJoinChunk &obj)=delete
bool Init(const pack_rows::TableCollection &tables, bool uses_match_flags)
Initialize this HashJoinChunk.
Definition: hash_join_chunk.cc:71
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
A structure that contains a list of tables for the hash join operation, and some pre-computed propert...
Definition: pack_rows.h:83
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1138
Common header for many mysys elements.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:391
Generic routines for packing rows (possibly from multiple tables at the same time) into strings,...
Definition: my_sys.h:340