MySQL 8.3.0
Source Code Documentation
cmp_varlen_keys.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef CMP_VARLEN_KEYS_INCLUDED
24#define CMP_VARLEN_KEYS_INCLUDED
25
26#include <assert.h>
27#include <stdio.h>
28#include <functional>
29
30#include "sql/sort_param.h"
31#include "sql/sql_array.h"
32
33/**
34 A compare function for variable-length keys used by filesort().
35 For record format documentation, @see Sort_param.
36
37 @param sort_field_array array of field descriptors for sorting
38 @param use_hash compare hash values (for grouping of JSON data)
39 @param s1 pointer to record 1
40 @param s2 pointer to record 2
41 @return true/false according to sorting order
42 true : s1 < s2
43 false : s1 >= s2
44 */
45inline bool cmp_varlen_keys(
46 Bounds_checked_array<st_sort_field> sort_field_array, bool use_hash,
47 const uchar *s1, const uchar *s2) {
50 int res;
51 for (const st_sort_field &sort_field : sort_field_array) {
52 uint kp1_len, kp2_len, kp_len;
53 if (sort_field.maybe_null) {
54 const int k1_nullbyte = *kp1++;
55 const int k2_nullbyte = *kp2++;
56 if (k1_nullbyte != k2_nullbyte) return k1_nullbyte < k2_nullbyte;
57 if (k1_nullbyte == 0 || k1_nullbyte == 0xff) {
58 if (!sort_field.is_varlen) {
59 kp1 += sort_field.length;
60 kp2 += sort_field.length;
61 }
62 continue; // Both key parts are null, nothing to compare
63 }
64 }
65 if (sort_field.is_varlen) {
66 assert(uint4korr(kp1) >= 4);
67 assert(uint4korr(kp2) >= 4);
68
69 kp1_len = uint4korr(kp1) - 4;
70 kp1 += 4;
71
72 kp2_len = uint4korr(kp2) - 4;
73 kp2 += 4;
74
75 kp_len = std::min(kp1_len, kp2_len);
76 } else {
77 kp_len = kp1_len = kp2_len = sort_field.length;
78 }
79
80 res = memcmp(kp1, kp2, kp_len);
81
82 if (res) return res < 0;
83 if (kp1_len != kp2_len) {
84 if (sort_field.reverse)
85 return kp2_len < kp1_len;
86 else
87 return kp1_len < kp2_len;
88 }
89
90 kp1 += kp1_len;
91 kp2 += kp2_len;
92 }
93
94 if (use_hash) {
95 // Compare hashes at the end of sort keys
96 return memcmp(kp1, kp2, 8) < 0;
97 } else {
98 return false;
99 }
100}
101
102/**
103 This struct is used for merging chunks for filesort()
104 For filesort() with fixed-size keys we use memcmp to compare rows.
105 For variable length keys, we use cmp_varlen_keys to compare rows.
106 */
108 size_t m_len;
110
111 // CTOR for filesort() with fixed-size keys
112 explicit Merge_chunk_greater(size_t len) : m_len(len), m_param(nullptr) {}
113
114 // CTOR for filesort() with varlen keys
115 explicit Merge_chunk_greater(Sort_param *param) : m_len(0), m_param(param) {}
116
117 bool operator()(Merge_chunk *a, Merge_chunk *b) const {
119 }
120
121 bool key_is_greater_than(uchar *key1, uchar *key2) const {
122 // Fixed len keys
123 if (m_len) return memcmp(key1, key2, m_len) > 0;
124
125 if (m_param)
127 key1);
128
129 // We can actually have zero-length sort key for filesort().
130 return false;
131 }
132};
133
134#endif // CMP_VARLEN_KEYS_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
A wrapper class which provides array bounds checking.
Definition: sql_array.h:46
There are several record formats for sorting:
Definition: sort_param.h:301
Bounds_checked_array< st_sort_field > local_sortorder
ORDER BY list with some precalculated info for filesort.
Definition: sort_param.h:322
static const uint size_of_varlength_field
Definition: sort_param.h:466
bool use_hash
Definition: sort_param.h:310
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:45
static uint16 key1[1001]
Definition: hp_test2.cc:49
uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:151
unsigned char uchar
Definition: my_inttypes.h:51
This struct is used for merging chunks for filesort() For filesort() with fixed-size keys we use memc...
Definition: cmp_varlen_keys.h:107
Merge_chunk_greater(Sort_param *param)
Definition: cmp_varlen_keys.h:115
bool key_is_greater_than(uchar *key1, uchar *key2) const
Definition: cmp_varlen_keys.h:121
Sort_param * m_param
Definition: cmp_varlen_keys.h:109
bool operator()(Merge_chunk *a, Merge_chunk *b) const
Definition: cmp_varlen_keys.h:117
size_t m_len
Definition: cmp_varlen_keys.h:108
Merge_chunk_greater(size_t len)
Definition: cmp_varlen_keys.h:112
Descriptor for a merge chunk to be sort-merged.
Definition: sql_sort.h:56
uchar * current_key() const
Definition: sql_sort.h:81
Struct that holds information about a sort field.
Definition: sort_param.h:85