| 1 | /* Copyright (c) 2021, 2026, 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 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include "sql/join_optimizer/compare_access_paths.h" |
| 27 | #include "unittest/gunit/test_utils.h" |
| 28 | |
| 29 | TEST(CompareAccessPathsTest, FuzzyComparison) { |
| 30 | double fuzz_factor = 1.01; |
| 31 | EXPECT_EQ(FuzzyComparison(1.0, 1.0, fuzz_factor), |
| 32 | FuzzyComparisonResult::IDENTICAL); |
| 33 | EXPECT_EQ(FuzzyComparison(1.0, 1.02, fuzz_factor), |
| 34 | FuzzyComparisonResult::FIRST_BETTER); |
| 35 | EXPECT_EQ(FuzzyComparison(1.02, 1.0, fuzz_factor), |
| 36 | FuzzyComparisonResult::SECOND_BETTER); |
| 37 | EXPECT_EQ(FuzzyComparison(1.0, 1.005, fuzz_factor), |
| 38 | FuzzyComparisonResult::FIRST_SLIGHTLY_BETTER); |
| 39 | EXPECT_EQ(FuzzyComparison(1.005, 1.0, fuzz_factor), |
| 40 | FuzzyComparisonResult::SECOND_SLIGHTLY_BETTER); |
| 41 | |
| 42 | // x is significantly less (better) than y if fuzz_factor * x < y. |
| 43 | // Verify that for x = 1.0 we switch from better to slightly better |
| 44 | // around y = 1.01. |
| 45 | EXPECT_EQ(FuzzyComparison(1.0, 1.0101, fuzz_factor), |
| 46 | FuzzyComparisonResult::FIRST_BETTER); |
| 47 | EXPECT_EQ(FuzzyComparison(1.0, 1.0099, fuzz_factor), |
| 48 | FuzzyComparisonResult::FIRST_SLIGHTLY_BETTER); |
| 49 | |
| 50 | // Exchanging x and y. |
| 51 | EXPECT_EQ(FuzzyComparison(1.0101, 1.0, fuzz_factor), |
| 52 | FuzzyComparisonResult::SECOND_BETTER); |
| 53 | EXPECT_EQ(FuzzyComparison(1.0099, 1.0, fuzz_factor), |
| 54 | FuzzyComparisonResult::SECOND_SLIGHTLY_BETTER); |
| 55 | } |
| 56 | |
| 57 | TEST(CompareAccessPathsTest, CompareAccessPaths) { |
| 58 | // We need the test server since the LogicalOrderings constructor takes |
| 59 | // a pointer to THD. |
| 60 | my_testing::Server_initializer m_initializer; |
| 61 | m_initializer.SetUp(); |
| 62 | THD *thd = m_initializer.thd(); |
| 63 | LogicalOrderings orderings(thd); |
| 64 | std::string trace; |
| 65 | orderings.Build(thd, &trace); |
| 66 | OrderingSet obsolete_orderings; |
| 67 | |
| 68 | AccessPath a; |
| 69 | // Discrete/categorical cost dimensions (non-fuzzy comparison). |
| 70 | a.parameter_tables = 0b111; |
| 71 | a.ordering_state = 0; |
| 72 | a.safe_for_rowid = AccessPath::Safety::UNSAFE; |
| 73 | // Numerical cost dimensions (fuzzy comparison). |
| 74 | a.set_num_output_rows(100.0); |
| 75 | a.cost = 100.0; |
| 76 | a.init_cost = 100.0; |
| 77 | a.init_once_cost = 0.0; |
| 78 | static_assert(std::is_trivially_copy_constructible<AccessPath>::value); |
| 79 | AccessPath b = a; |
| 80 | |
| 81 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 82 | PathComparisonResult::IDENTICAL); |
| 83 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 84 | PathComparisonResult::IDENTICAL); |
| 85 | |
| 86 | // Domination from a single discrete dimension (the parameter tables of one |
| 87 | // path is a subset of the parameter tables of the other path). |
| 88 | b.parameter_tables = 0b001; |
| 89 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 90 | PathComparisonResult::SECOND_DOMINATES); |
| 91 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 92 | PathComparisonResult::FIRST_DOMINATES); |
| 93 | |
| 94 | // Different strengths caused by categorical dimensions. |
| 95 | a.safe_for_rowid = AccessPath::Safety::SAFE; |
| 96 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 97 | PathComparisonResult::DIFFERENT_STRENGTHS); |
| 98 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 99 | PathComparisonResult::DIFFERENT_STRENGTHS); |
| 100 | |
| 101 | // Fuzzily identical, neither path dominates. |
| 102 | a = b; |
| 103 | a.cost = 100.5; |
| 104 | b.init_cost = 100.5; |
| 105 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 106 | PathComparisonResult::IDENTICAL); |
| 107 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 108 | PathComparisonResult::IDENTICAL); |
| 109 | |
| 110 | // Fuzzily identical, but one path dominates (slightly). |
| 111 | b.cost = 100.0; |
| 112 | b.init_cost = 100.0; |
| 113 | a.cost = 99.5; |
| 114 | a.init_cost = 99.5; |
| 115 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 116 | PathComparisonResult::FIRST_DOMINATES); |
| 117 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 118 | PathComparisonResult::SECOND_DOMINATES); |
| 119 | |
| 120 | // Different strenghts in numerical dimensions. |
| 121 | a.cost = 100.0; |
| 122 | b.cost = 95.0; |
| 123 | a.init_cost = 95.0; |
| 124 | b.init_cost = 100.0; |
| 125 | EXPECT_EQ(CompareAccessPaths(orderings, a, b, obsolete_orderings), |
| 126 | PathComparisonResult::DIFFERENT_STRENGTHS); |
| 127 | EXPECT_EQ(CompareAccessPaths(orderings, b, a, obsolete_orderings), |
| 128 | PathComparisonResult::DIFFERENT_STRENGTHS); |
| 129 | } |