MySQL 9.1.0
Source Code Documentation
hypergraph.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_JOIN_OPTIMIZER_HYPERGRAPH_H_
25#define SQL_JOIN_OPTIMIZER_HYPERGRAPH_H_ 1
26
27/**
28 @file
29 Definition of an undirected (join) hypergraph. A hypergraph in this context
30 is an undirected graph consisting of nodes and hyperedges, where hyperedges
31 are edges that can have more than one node in each side of the edge.
32 For instance, in a graph with nodes {A, B, C, D}, a regular undirected edge
33 could be e.g. (A,B), while in a hypergraph, an edge such as ({A,C},B) would
34 also be allowed. Note that this definition of hypergraphs differs from that
35 on Wikipedia.
36
37 The main user of Hypergraph is subgraph_enumeration.h.
38 */
39
40#include <stddef.h>
41#include <algorithm>
42#include <bit>
43#include <vector>
44
46#include "sql/mem_root_array.h"
47
48struct MEM_ROOT;
49
50namespace hypergraph {
51
52struct Node {
53 // List of edges (indexes into the hypergraph's “edges” array) that touch this
54 // node. We split these into simple edges (only one node on each side) and
55 // complex edges (all others), because we can often quickly discard all simple
56 // edges by testing the set of interesting nodes against the
57 // “simple_neighborhood” bitmap.
58 //
59 // For optimization purposes, the edges are stored as if they were directed,
60 // even though the hypergraph is fundamentally undirected. That is, a (u,v)
61 // edge will be duplicated internally to (v,u), and the version that is posted
62 // in a node's edge list is the one where the node itself is on the left side.
63 // This saves a lot of duplicate code, and also reduces the amount of branch
64 // mispredictions significantly (it helps something like 30% on the overall
65 // speed).
66 std::vector<unsigned> complex_edges, simple_edges;
67
68 // All nodes on the “right” side of an edge in simple_edges.
70
71 private:
72 // Speeds up BM_HyperStar17_ManyHyperedges by 5–10%.
73 // (MSVC with debug STL will get a dummy byte here, since the struct is
74 // already more than 64 bytes.)
75 static constexpr int Size =
76 sizeof(std::vector<unsigned>) * 2 + sizeof(NodeMap);
77 char padding[std::max<int>(1, 64 - Size)];
78};
79static_assert(sizeof(Node) >= 64);
80
81struct Hyperedge {
82 // The endpoints (hypernodes) of this hyperedge. See the comment about
83 // duplicated edges in Node.
84 //
85 // left and right may not overlap, and both must have at least one bit set.
88};
89
90struct Hypergraph {
91 public:
93 Mem_root_array<Node> nodes; // Maximum 8*sizeof(NodeMap) elements.
95
96 void AddNode();
97 void AddEdge(NodeMap left, NodeMap right);
98
99 // NOTE: Since every edge is stored twice (see AddEdge), also updates the
100 // corresponding opposite-direction edge automatically. Also note that this
101 // will shift internal edge lists around, so even after no-op changes,
102 // you are not guaranteed to get back subgraph pairs in the same order
103 // as before.
104 void ModifyEdge(unsigned edge_idx, NodeMap new_left, NodeMap new_right);
105
106 private:
107 void AttachEdgeToNodes(size_t left_first_idx, size_t right_first_idx,
108 NodeMap left, NodeMap right);
109};
110
111/// Is an edge between "left" and "right" a simple edge?
112inline bool IsSimpleEdge(NodeMap left, NodeMap right) {
113 return std::has_single_bit(left) && std::has_single_bit(right);
114}
115
116} // namespace hypergraph
117
118#endif // SQL_JOIN_OPTIMIZER_HYPERGRAPH_H_
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
Definition: hypergraph.cc:30
bool IsSimpleEdge(NodeMap left, NodeMap right)
Is an edge between "left" and "right" a simple edge?
Definition: hypergraph.h:112
uint64_t NodeMap
Since our graphs can never have more than 61 tables, node sets and edge lists are implemented using 6...
Definition: node_map.h:40
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: hypergraph.h:81
NodeMap left
Definition: hypergraph.h:86
NodeMap right
Definition: hypergraph.h:87
Definition: hypergraph.h:90
Mem_root_array< Node > nodes
Definition: hypergraph.h:93
void AddNode()
Definition: hypergraph.cc:32
Hypergraph(MEM_ROOT *mem_root)
Definition: hypergraph.h:92
void AddEdge(NodeMap left, NodeMap right)
Definition: hypergraph.cc:34
void AttachEdgeToNodes(size_t left_first_idx, size_t right_first_idx, NodeMap left, NodeMap right)
Definition: hypergraph.cc:117
Mem_root_array< Hyperedge > edges
Definition: hypergraph.h:94
void ModifyEdge(unsigned edge_idx, NodeMap new_left, NodeMap new_right)
Definition: hypergraph.cc:58
Definition: hypergraph.h:52
char padding[std::max< int >(1, 64 - Size)]
Definition: hypergraph.h:77
std::vector< unsigned > complex_edges
Definition: hypergraph.h:66
static constexpr int Size
Definition: hypergraph.h:75
NodeMap simple_neighborhood
Definition: hypergraph.h:69
std::vector< unsigned > simple_edges
Definition: hypergraph.h:66