-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathvector_conversion.cc
173 lines (151 loc) · 5.29 KB
/
vector_conversion.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright (c) 2024, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <sys/types.h>
#include <cerrno>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "sql_string.h"
bool from_string_to_vector(const CHARSET_INFO *cs, const char *input,
uint32_t input_len, char *output,
uint32_t *max_output_dims) {
if (input_len == 0 || input == nullptr) {
*max_output_dims = 0;
return true;
}
/* Convert the input to UTF-8 */
const CHARSET_INFO *process_cs = cs;
String temp_input;
if (!my_charset_is_ascii_based(cs)) {
process_cs = &my_charset_utf8mb4_bin;
temp_input.alloc(input_len);
unsigned err = 0;
input_len = my_convert(temp_input.ptr(), input_len, process_cs, input,
input_len, cs, &err);
if (err != 0) {
*max_output_dims = 0;
return true;
}
input = temp_input.ptr();
}
// Skip the leading whitespaces
auto len = process_cs->cset->scan(process_cs, input, input + input_len,
MY_SEQ_SPACES);
if (len >= input_len) {
*max_output_dims = 0;
return true;
}
input_len -= len;
input += len;
// Check that we start with '['
if (input_len == 0 || input[0] != '[') {
*max_output_dims = 0;
return true;
}
// Check for memory region overlap
size_t const output_len = sizeof(float) * (*max_output_dims);
String temp_output(output, output_len, nullptr);
if (output + output_len >= input && input + input_len >= output) {
temp_output = String(output_len);
}
const char *const input_end = input + input_len - 1;
input = input + 1;
uint32_t dim = 0;
char *end = nullptr;
bool with_success = false;
errno = 0;
for (float fnum = strtof(input, &end); input != end;
fnum = strtof(input, &end)) {
input = end;
if (errno == ERANGE || dim >= *max_output_dims || std::isnan(fnum) ||
std::isinf(fnum)) {
errno = 0;
break;
}
memcpy(temp_output.ptr() + dim * sizeof(float), &fnum, sizeof(float));
input +=
process_cs->cset->scan(process_cs, input, input_end, MY_SEQ_SPACES);
if (*input == ',') {
// Check that we have delimiter with ','
input = input + 1;
dim++;
} else if (*input == ']') {
// Check that we end with ']'
input += 1;
with_success = true;
dim++;
break;
} else {
break;
}
}
if (with_success &&
!check_if_only_end_space(process_cs, input, input_end + 1)) {
*max_output_dims = 0;
return true;
}
if (temp_output.ptr() != output) {
memcpy(output, temp_output.ptr(), dim * sizeof(float));
}
*max_output_dims = dim;
return !with_success;
}
bool from_vector_to_string(const char *input, uint32_t input_dims, char *output,
uint32_t *max_output_len) {
const uint32_t end_cushion = 12;
if (input == nullptr || *max_output_len < end_cushion) {
return true;
}
// Check for memory region overlap
size_t const input_len = input_dims * sizeof(float);
String temp_output(output, *max_output_len, nullptr);
if (output + *max_output_len >= input && input + input_len >= output) {
temp_output = String(*max_output_len);
}
char *write_ptr = temp_output.ptr();
uint32_t total_length = 1;
write_ptr[0] = '[';
write_ptr += 1;
for (uint32_t i = 0; i < input_dims; i++) {
int const remaining_bytes = *max_output_len - total_length;
int nchars = 0;
if (*max_output_len <= total_length + end_cushion) {
nchars = snprintf(write_ptr, remaining_bytes, "...");
i = input_dims;
} else {
char const delimiter = (i == input_dims - 1) ? ']' : ',';
float input_value = 0;
memcpy(&input_value, input + i * sizeof(float), sizeof(float));
nchars = snprintf(write_ptr, remaining_bytes, "%.5e%c", input_value,
delimiter);
if (nchars < 0 || nchars >= remaining_bytes) {
return true; /* LCOV_EXCL_LINE */
}
}
write_ptr += nchars;
total_length += nchars;
}
if (temp_output.ptr() != output) {
memcpy(output, temp_output.ptr(), total_length);
}
*max_output_len = total_length;
return false;
}