MySQL 9.1.0
Source Code Documentation
cmp_varlen_keys.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 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 CMP_VARLEN_KEYS_INCLUDED
25#define CMP_VARLEN_KEYS_INCLUDED
26
27#include <assert.h>
28#include <stdio.h>
29#include <functional>
30
31#include "sql/sort_param.h"
32#include "sql/sql_array.h"
33
34/**
35 A compare function for variable-length keys used by filesort().
36 For record format documentation, @see Sort_param.
37
38 @param sort_field_array array of field descriptors for sorting
39 @param use_hash compare hash values (for grouping of JSON data)
40 @param s1 pointer to record 1
41 @param s2 pointer to record 2
42 @return true/false according to sorting order
43 true : s1 < s2
44 false : s1 >= s2
45 */
46inline bool cmp_varlen_keys(
47 Bounds_checked_array<st_sort_field> sort_field_array, bool use_hash,
48 const uchar *s1, const uchar *s2) {
51 int res;
52 for (const st_sort_field &sort_field : sort_field_array) {
53 uint kp1_len, kp2_len, kp_len;
54 if (sort_field.maybe_null) {
55 const int k1_nullbyte = *kp1++;
56 const int k2_nullbyte = *kp2++;
57 if (k1_nullbyte != k2_nullbyte) return k1_nullbyte < k2_nullbyte;
58 if (k1_nullbyte == 0 || k1_nullbyte == 0xff) {
59 if (!sort_field.is_varlen) {
60 kp1 += sort_field.length;
61 kp2 += sort_field.length;
62 }
63 continue; // Both key parts are null, nothing to compare
64 }
65 }
66 if (sort_field.is_varlen) {
67 assert(uint4korr(kp1) >= 4);
68 assert(uint4korr(kp2) >= 4);
69
70 kp1_len = uint4korr(kp1) - 4;
71 kp1 += 4;
72
73 kp2_len = uint4korr(kp2) - 4;
74 kp2 += 4;
75
76 kp_len = std::min(kp1_len, kp2_len);
77 } else {
78 kp_len = kp1_len = kp2_len = sort_field.length;
79 }
80
81 res = memcmp(kp1, kp2, kp_len);
82
83 if (res) return res < 0;
84 if (kp1_len != kp2_len) {
85 if (sort_field.reverse)
86 return kp2_len < kp1_len;
87 else
88 return kp1_len < kp2_len;
89 }
90
91 kp1 += kp1_len;
92 kp2 += kp2_len;
93 }
94
95 if (use_hash) {
96 // Compare hashes at the end of sort keys
97 return memcmp(kp1, kp2, 8) < 0;
98 } else {
99 return false;
100 }
101}
102
103/**
104 This struct is used for merging chunks for filesort()
105 For filesort() with fixed-size keys we use memcmp to compare rows.
106 For variable length keys, we use cmp_varlen_keys to compare rows.
107 */
109 size_t m_len;
111
112 // CTOR for filesort() with fixed-size keys
113 explicit Merge_chunk_greater(size_t len) : m_len(len), m_param(nullptr) {}
114
115 // CTOR for filesort() with varlen keys
116 explicit Merge_chunk_greater(Sort_param *param) : m_len(0), m_param(param) {}
117
118 bool operator()(Merge_chunk *a, Merge_chunk *b) const {
120 }
121
122 bool key_is_greater_than(uchar *key1, uchar *key2) const {
123 // Fixed len keys
124 if (m_len) return memcmp(key1, key2, m_len) > 0;
125
126 if (m_param)
128 key1);
129
130 // We can actually have zero-length sort key for filesort().
131 return false;
132 }
133};
134
135#endif // CMP_VARLEN_KEYS_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
A wrapper class which provides array bounds checking.
Definition: sql_array.h:47
There are several record formats for sorting:
Definition: sort_param.h:302
Bounds_checked_array< st_sort_field > local_sortorder
ORDER BY list with some precalculated info for filesort.
Definition: sort_param.h:323
static const uint size_of_varlength_field
Definition: sort_param.h:467
bool use_hash
Definition: sort_param.h:311
bool cmp_varlen_keys(Bounds_checked_array< st_sort_field > sort_field_array, bool use_hash, const uchar *s1, const uchar *s2)
A compare function for variable-length keys used by filesort().
Definition: cmp_varlen_keys.h:46
static uint16 key1[1001]
Definition: hp_test2.cc:50
uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:152
unsigned char uchar
Definition: my_inttypes.h:52
This struct is used for merging chunks for filesort() For filesort() with fixed-size keys we use memc...
Definition: cmp_varlen_keys.h:108
Merge_chunk_greater(Sort_param *param)
Definition: cmp_varlen_keys.h:116
bool key_is_greater_than(uchar *key1, uchar *key2) const
Definition: cmp_varlen_keys.h:122
Sort_param * m_param
Definition: cmp_varlen_keys.h:110
bool operator()(Merge_chunk *a, Merge_chunk *b) const
Definition: cmp_varlen_keys.h:118
size_t m_len
Definition: cmp_varlen_keys.h:109
Merge_chunk_greater(size_t len)
Definition: cmp_varlen_keys.h:113
Descriptor for a merge chunk to be sort-merged.
Definition: sql_sort.h:57
uchar * current_key() const
Definition: sql_sort.h:82
Struct that holds information about a sort field.
Definition: sort_param.h:86