Skip to content

Commit 2606a27

Browse files
committed
Bug#33806018: Remove usage of MD5 in explain_access_path.cc
In debug builds only, the hypergraph optimizer has functionality for forcing certain subplans to be preferred, regardless of their cost. The identifiers for these subplans are created from MD5 checksums. Since MD5 is being phased out, we change it to create identifiers from SHA-256 checksums instead, so that it will continue to work also when MD5 is not available. Change-Id: I243959734debeef78d295523b046c2314fcfc931
1 parent 642e456 commit 2606a27

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed

mysql-test/r/subplan_tokens.result

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,41 @@ test.t1 analyze status OK
66
SET DEBUG='+d,subplan_tokens';
77
EXPLAIN FORMAT=tree SELECT * FROM t1;
88
EXPLAIN
9-
-> [0x383f9723be72015d] Table scan on t1 (cost=0.08..0.25 rows=3)
9+
-> [0xeed2c0bd3e39ba93] Table scan on t1 (cost=0.08..0.25 rows=3)
1010

1111
To force this plan, use:
12-
SET DEBUG='+d,subplan_tokens,force_subplan_0x383f9723be72015d';
12+
SET DEBUG='+d,subplan_tokens,force_subplan_0xeed2c0bd3e39ba93';
1313

1414
EXPLAIN FORMAT=tree SELECT * FROM t1 ORDER BY a;
1515
EXPLAIN
16-
-> [0x4f56594d4fd521c9] Index scan on t1 using a (cost=0.08..0.25 rows=3)
16+
-> [0x2215d3ce44f01b0d] Index scan on t1 using a (cost=0.08..0.25 rows=3)
1717

1818
To force this plan, use:
19-
SET DEBUG='+d,subplan_tokens,force_subplan_0x4f56594d4fd521c9';
19+
SET DEBUG='+d,subplan_tokens,force_subplan_0x2215d3ce44f01b0d';
2020

21-
SET DEBUG='+d,subplan_tokens,force_subplan_0x383f9723be72015d';
21+
SET DEBUG='+d,subplan_tokens,force_subplan_0xeed2c0bd3e39ba93';
2222
EXPLAIN FORMAT=tree SELECT * FROM t1 ORDER BY a;
2323
EXPLAIN
24-
-> [0x685afbd3f088dc87] Sort: t1.a (cost=1.03..1.03 rows=3)
25-
-> [0x383f9723be72015d] Table scan on t1 (cost=0.08..0.25 rows=3)
24+
-> [0xd3135ef8a92cb9b8] Sort: t1.a (cost=1.03..1.03 rows=3)
25+
-> [0xeed2c0bd3e39ba93] Table scan on t1 (cost=0.08..0.25 rows=3)
2626

2727
To force this plan, use:
28-
SET DEBUG='+d,subplan_tokens,force_subplan_0x685afbd3f088dc87,force_subplan_0x383f9723be72015d';
28+
SET DEBUG='+d,subplan_tokens,force_subplan_0xd3135ef8a92cb9b8,force_subplan_0xeed2c0bd3e39ba93';
2929

3030
SET DEBUG='+d,subplan_tokens,'
31-
'force_subplan_0x90b6f02248424220,'
32-
'force_subplan_0x383f9723be72015d,'
33-
'force_subplan_0xedd60db81c76aaf5';
31+
'force_subplan_0x38d170e70c04e92c,'
32+
'force_subplan_0xeed2c0bd3e39ba93,'
33+
'force_subplan_0x079e429c703ec298';
3434
EXPLAIN FORMAT=tree SELECT * FROM t1 JOIN t1 AS t2 ON t1.a=t2.a ORDER BY t1.a;
3535
EXPLAIN
36-
-> [0x9fe1924c5f48742c] Sort: t1.a (cost=2.09..2.09 rows=3)
37-
-> [0x90b6f02248424220] Inner hash join (t1.a = t2.a) (cost=0.80..1.31 rows=3)
38-
-> [0x383f9723be72015d] Table scan on t1 (cost=0.08..0.25 rows=3)
36+
-> [0x1836646581797f8f] Sort: t1.a (cost=2.09..2.09 rows=3)
37+
-> [0x38d170e70c04e92c] Inner hash join (t1.a = t2.a) (cost=0.80..1.31 rows=3)
38+
-> [0xeed2c0bd3e39ba93] Table scan on t1 (cost=0.08..0.25 rows=3)
3939
-> Hash
40-
-> [0xedd60db81c76aaf5] Table scan on t2 (cost=0.08..0.25 rows=3)
40+
-> [0x079e429c703ec298] Table scan on t2 (cost=0.08..0.25 rows=3)
4141

4242
To force this plan, use:
43-
SET DEBUG='+d,subplan_tokens,force_subplan_0x9fe1924c5f48742c,force_subplan_0x90b6f02248424220,force_subplan_0x383f9723be72015d,force_subplan_0xedd60db81c76aaf5';
43+
SET DEBUG='+d,subplan_tokens,force_subplan_0x1836646581797f8f,force_subplan_0x38d170e70c04e92c,force_subplan_0xeed2c0bd3e39ba93,force_subplan_0x079e429c703ec298';
4444

4545
SET DEBUG='-d,subplan_tokens';
4646
EXPLAIN FORMAT=tree SELECT * FROM t1 JOIN t1 AS t2 ON t1.a=t2.a ORDER BY t1.a;

mysql-test/t/subplan_tokens.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ EXPLAIN FORMAT=tree SELECT * FROM t1 ORDER BY a;
1414

1515
# However, if we force the table scan variant from earlier,
1616
# we should get a sort instead.
17-
SET DEBUG='+d,subplan_tokens,force_subplan_0x383f9723be72015d';
17+
SET DEBUG='+d,subplan_tokens,force_subplan_0xeed2c0bd3e39ba93';
1818
EXPLAIN FORMAT=tree SELECT * FROM t1 ORDER BY a;
1919

20-
# Force a hash join; this is the truncated MD5 checksum of:
21-
# Inner hash join (t1.a = t2.a),0x383f9723be72015d,Hash:0xedd60db81c76aaf5
20+
# Force a hash join; this is the truncated SHA-256 checksum of:
21+
# Inner hash join (t1.a = t2.a),0xeed2c0bd3e39ba93,Hash:0x079e429c703ec298
2222
# We also force the hash join subplans to ensure they are not optimized away.
2323
SET DEBUG='+d,subplan_tokens,'
24-
'force_subplan_0x90b6f02248424220,'
25-
'force_subplan_0x383f9723be72015d,'
26-
'force_subplan_0xedd60db81c76aaf5';
24+
'force_subplan_0x38d170e70c04e92c,'
25+
'force_subplan_0xeed2c0bd3e39ba93,'
26+
'force_subplan_0x079e429c703ec298';
2727
EXPLAIN FORMAT=tree SELECT * FROM t1 JOIN t1 AS t2 ON t1.a=t2.a ORDER BY t1.a;
2828

2929
# When not forcing anything, we should prefer the nested loop join.

sql/join_optimizer/explain_access_path.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222

2323
#include "sql/join_optimizer/explain_access_path.h"
2424

25-
#include "my_md5.h"
26-
#include "my_md5_size.h"
25+
#include <functional>
26+
#include <string>
27+
#include <vector>
28+
29+
#include <openssl/sha.h>
30+
31+
#include "sha2.h"
2732
#include "sql/filesort.h"
2833
#include "sql/item_sum.h"
2934
#include "sql/iterators/basic_row_iterators.h"
@@ -42,10 +47,7 @@
4247
#include "sql/range_optimizer/range_optimizer.h"
4348
#include "sql/sql_optimizer.h"
4449
#include "sql/table.h"
45-
46-
#include <functional>
47-
#include <string>
48-
#include <vector>
50+
#include "template_utils.h"
4951

5052
using std::string;
5153
using std::vector;
@@ -1172,7 +1174,7 @@ string PrintQueryPlan(int level, AccessPath *path, JOIN *join,
11721174
}
11731175

11741176
// 0x
1175-
// truncated_md5(desc1,desc2,...,[child1_desc:]0xchild1,[child2_desc:]0xchild2,...)
1177+
// truncated_sha256(desc1,desc2,...,[child1_desc:]0xchild1,[child2_desc:]0xchild2,...)
11761178
string GetForceSubplanToken(AccessPath *path, JOIN *join) {
11771179
if (path == nullptr) {
11781180
return "";
@@ -1194,13 +1196,14 @@ string GetForceSubplanToken(AccessPath *path, JOIN *join) {
11941196
digest += GetForceSubplanToken(child.path, join);
11951197
}
11961198

1197-
unsigned char md5sum[MD5_HASH_SIZE];
1198-
compute_md5_hash(pointer_cast<char *>(md5sum), digest.data(), digest.size());
1199+
unsigned char sha256sum[SHA256_DIGEST_LENGTH];
1200+
(void)SHA_EVP256(pointer_cast<const unsigned char *>(digest.data()),
1201+
digest.size(), sha256sum);
11991202

12001203
char ret[8 * 2 + 2 + 1];
1201-
snprintf(ret, sizeof(ret), "0x%02x%02x%02x%02x%02x%02x%02x%02x", md5sum[0],
1202-
md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], md5sum[6],
1203-
md5sum[7]);
1204+
snprintf(ret, sizeof(ret), "0x%02x%02x%02x%02x%02x%02x%02x%02x", sha256sum[0],
1205+
sha256sum[1], sha256sum[2], sha256sum[3], sha256sum[4], sha256sum[5],
1206+
sha256sum[6], sha256sum[7]);
12041207

12051208
return ret;
12061209
}

sql/join_optimizer/explain_access_path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -51,7 +51,7 @@ std::string PrintQueryPlan(
5151
preferred over all others.
5252
5353
The token returned is “0x<digest>”, where <digest> is the first 64 bits
54-
of the MD5 sum of this string:
54+
of the SHA-256 sum of this string:
5555
5656
desc1,desc2,...,[child1_desc:]0xchild1,[child2_desc:]0xchild2,<more
5757
children>
@@ -60,7 +60,7 @@ std::string PrintQueryPlan(
6060
and 0xchild1 is the token for children. The normal way to generate such
6161
tokens is to use SET DEBUG='+d,subplan_tokens' and look at the EXPLAIN
6262
FORMAT=tree, but in a pinch, you can also write them by hand and use
63-
md5sum or a similar tool.
63+
sha256sum or a similar tool.
6464
6565
Only the hypergraph join optimizer honors token preferences, but EXPLAIN
6666
FORMAT=tree shows computed tokens for both optimizers.

0 commit comments

Comments
 (0)