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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
package engine
import (
"fmt"
"math"
"strings"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)
// ===============================
// STRING FUNCTIONS
// ===============================
// Length returns the length of a string
func (e *SQLEngine) Length(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("LENGTH function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("LENGTH function conversion error: %v", err)
}
length := int64(len(str))
return &schema_pb.Value{
Kind: &schema_pb.Value_Int64Value{Int64Value: length},
}, nil
}
// Upper converts a string to uppercase
func (e *SQLEngine) Upper(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("UPPER function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("UPPER function conversion error: %v", err)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: strings.ToUpper(str)},
}, nil
}
// Lower converts a string to lowercase
func (e *SQLEngine) Lower(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("LOWER function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("LOWER function conversion error: %v", err)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: strings.ToLower(str)},
}, nil
}
// Trim removes leading and trailing whitespace from a string
func (e *SQLEngine) Trim(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("TRIM function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("TRIM function conversion error: %v", err)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimSpace(str)},
}, nil
}
// LTrim removes leading whitespace from a string
func (e *SQLEngine) LTrim(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("LTRIM function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("LTRIM function conversion error: %v", err)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimLeft(str, " \t\n\r")},
}, nil
}
// RTrim removes trailing whitespace from a string
func (e *SQLEngine) RTrim(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("RTRIM function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("RTRIM function conversion error: %v", err)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: strings.TrimRight(str, " \t\n\r")},
}, nil
}
// Substring extracts a substring from a string
func (e *SQLEngine) Substring(value *schema_pb.Value, start *schema_pb.Value, length ...*schema_pb.Value) (*schema_pb.Value, error) {
if value == nil || start == nil {
return nil, fmt.Errorf("SUBSTRING function requires non-null value and start position")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("SUBSTRING function value conversion error: %v", err)
}
startPos, err := e.valueToInt64(start)
if err != nil {
return nil, fmt.Errorf("SUBSTRING function start position conversion error: %v", err)
}
// Convert to 0-based indexing (SQL uses 1-based)
if startPos < 1 {
startPos = 1
}
startIdx := int(startPos - 1)
if startIdx >= len(str) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: ""},
}, nil
}
var result string
if len(length) > 0 && length[0] != nil {
lengthVal, err := e.valueToInt64(length[0])
if err != nil {
return nil, fmt.Errorf("SUBSTRING function length conversion error: %v", err)
}
if lengthVal <= 0 {
result = ""
} else {
if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
// If length is out-of-bounds for int, take substring from startIdx to end
result = str[startIdx:]
} else {
// Safe conversion after bounds check
endIdx := startIdx + int(lengthVal)
if endIdx > len(str) {
endIdx = len(str)
}
result = str[startIdx:endIdx]
}
}
} else {
result = str[startIdx:]
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: result},
}, nil
}
// Concat concatenates multiple strings
func (e *SQLEngine) Concat(values ...*schema_pb.Value) (*schema_pb.Value, error) {
if len(values) == 0 {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: ""},
}, nil
}
var result strings.Builder
for i, value := range values {
if value == nil {
continue // Skip null values
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("CONCAT function value %d conversion error: %v", i, err)
}
result.WriteString(str)
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: result.String()},
}, nil
}
// Replace replaces all occurrences of a substring with another substring
func (e *SQLEngine) Replace(value, oldStr, newStr *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil || oldStr == nil || newStr == nil {
return nil, fmt.Errorf("REPLACE function requires non-null values")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("REPLACE function value conversion error: %v", err)
}
old, err := e.valueToString(oldStr)
if err != nil {
return nil, fmt.Errorf("REPLACE function old string conversion error: %v", err)
}
new, err := e.valueToString(newStr)
if err != nil {
return nil, fmt.Errorf("REPLACE function new string conversion error: %v", err)
}
result := strings.ReplaceAll(str, old, new)
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: result},
}, nil
}
// Position returns the position of a substring in a string (1-based, 0 if not found)
func (e *SQLEngine) Position(substring, value *schema_pb.Value) (*schema_pb.Value, error) {
if substring == nil || value == nil {
return nil, fmt.Errorf("POSITION function requires non-null values")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("POSITION function string conversion error: %v", err)
}
substr, err := e.valueToString(substring)
if err != nil {
return nil, fmt.Errorf("POSITION function substring conversion error: %v", err)
}
pos := strings.Index(str, substr)
if pos == -1 {
pos = 0 // SQL returns 0 for not found
} else {
pos = pos + 1 // Convert to 1-based indexing
}
return &schema_pb.Value{
Kind: &schema_pb.Value_Int64Value{Int64Value: int64(pos)},
}, nil
}
// Left returns the leftmost characters of a string
func (e *SQLEngine) Left(value *schema_pb.Value, length *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil || length == nil {
return nil, fmt.Errorf("LEFT function requires non-null values")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("LEFT function string conversion error: %v", err)
}
lengthVal, err := e.valueToInt64(length)
if err != nil {
return nil, fmt.Errorf("LEFT function length conversion error: %v", err)
}
if lengthVal <= 0 {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: ""},
}, nil
}
if lengthVal > int64(len(str)) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
// Safe conversion after bounds check
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str[:int(lengthVal)]},
}, nil
}
// Right returns the rightmost characters of a string
func (e *SQLEngine) Right(value *schema_pb.Value, length *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil || length == nil {
return nil, fmt.Errorf("RIGHT function requires non-null values")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("RIGHT function string conversion error: %v", err)
}
lengthVal, err := e.valueToInt64(length)
if err != nil {
return nil, fmt.Errorf("RIGHT function length conversion error: %v", err)
}
if lengthVal <= 0 {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: ""},
}, nil
}
if lengthVal > int64(len(str)) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
if lengthVal > int64(math.MaxInt) || lengthVal < int64(math.MinInt) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
// Safe conversion after bounds check
startPos := len(str) - int(lengthVal)
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str[startPos:]},
}, nil
}
// Reverse reverses a string
func (e *SQLEngine) Reverse(value *schema_pb.Value) (*schema_pb.Value, error) {
if value == nil {
return nil, fmt.Errorf("REVERSE function requires non-null value")
}
str, err := e.valueToString(value)
if err != nil {
return nil, fmt.Errorf("REVERSE function conversion error: %v", err)
}
// Reverse the string rune by rune to handle Unicode correctly
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: string(runes)},
}, nil
}
|