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
//! v7.39 (round 610) — taking a slice of a string built a vector of the
//! whole string first.
//!
//! Round 608 took the operand COPIES out of the string functions. What was
//! left in these two is the shape underneath: to hand back five characters
//! they collected the entire input into a `Vec<char>` — four bytes for every
//! character of it — indexed that, and collected the answer back out.
//! Counted over 200k rows against a TEXT column:
//!
//! count(s) 0 allocations a row 3.3 ms
//! count(substr(s,2,5)) 3.5 28.9
//! count(left(s,5)) 5.5 37.6
//!
//! Both walk byte offsets now and slice once:
//!
//! left / right 5.5 -> 2 allocations a row 37.6 -> 19.5 ms
//! substr / substring 3.5 -> 1 28.9 -> 15.6
//!
//! and over pgwire on 500k rows against PG18:
//!
//! left(s,5) 102.30 -> 57.08 PG 9.81 10.40x -> 5.82x
//! right(s,5) 100.32 -> 62.59 PG 9.48 10.38x -> 6.60x
//! substring(s from 2 for 5) 70.31 -> 36.23 PG 9.79 6.90x -> 3.70x
//! substr(s,2,5) 75.07 -> 42.47 PG 9.94 7.70x -> 4.27x
//!
//! Characters, not bytes, is the whole contract here, and it is the part a
//! byte walk can get wrong — so the pins carry multi-byte text through every
//! shape: a positive and a negative count on both ends, a count past the
//! length, a start before the string, a zero length, and the empty string.
//! `left` with a positive count is the one case that never needs the total,
//! and it does not compute one.
//!
//! All 16 shapes were run against the previous binary and against this one:
//! SPG's answers are byte-identical. Against live PG18 fifteen match; the
//! sixteenth is `substr(s FROM 2 FOR 2)`, which PG rejects as a syntax error
//! (only `substring` has that spelling there) and SPG answers. That is the
//! parser's leniency and predates this round — the change is confined to two
//! evaluator arms.
use spg_engine::{Engine, QueryResult};
fn vals(e: &mut Engine, sql: &str) -> Vec<String> {
match e.execute(sql).unwrap_or_else(|err| panic!("{sql}: {err}")) {
QueryResult::Rows { rows, .. } => rows
.iter()
.map(|r| {
r.values
.iter()
.map(spg_engine::eval::value_to_text)
.collect::<Vec<_>>()
.join("|")
})
.collect(),
other => panic!("{sql}: {other:?}"),
}
}
fn seed() -> Engine {
let mut e = Engine::new();
e.execute("CREATE TABLE lt (id INT, s TEXT)").unwrap();
e.execute(
"INSERT INTO lt VALUES (1,'abcdef'),(2,''),(3,NULL),(4,'日本語テキスト'),(5,'ábç'),(6,'x')",
)
.unwrap();
e
}
/// `left` / `right`, including the negative counts that mean "drop from the
/// other end" and the counts that run past the string.
#[test]
fn round610_left_and_right() {
let mut e = seed();
assert_eq!(
vals(
&mut e,
"SELECT id, left(s,3), left(s,0), left(s,99), left(s,-2) FROM lt ORDER BY id"
),
vec![
"1|abc||abcdef|abcd",
"2||||",
"3|NULL|NULL|NULL|NULL",
"4|日本語||日本語テキスト|日本語テキ",
"5|ábç||ábç|á",
"6|x||x|",
],
"left(s,-2) drops the last two CHARACTERS"
);
assert_eq!(
vals(
&mut e,
"SELECT id, right(s,3), right(s,0), right(s,99), right(s,-2) FROM lt ORDER BY id"
),
vec![
"1|def||abcdef|cdef",
"2||||",
"3|NULL|NULL|NULL|NULL",
"4|キスト||日本語テキスト|語テキスト",
"5|ábç||ábç|ç",
"6|x||x|",
],
"right(s,-2) drops the first two"
);
assert_eq!(
vals(
&mut e,
"SELECT id, left(s,1), right(s,1), left(s,-99), right(s,-99) FROM lt ORDER BY id"
),
vec![
"1|a|f||",
"2||||",
"3|NULL|NULL|NULL|NULL",
"4|日|ト||",
"5|á|ç||",
"6|x|x||",
],
"a drop bigger than the string leaves nothing"
);
assert_eq!(
vals(
&mut e,
"SELECT id, length(left(s,3)), length(right(s,3)) FROM lt ORDER BY id"
),
vec!["1|3|3", "2|0|0", "3|NULL|NULL", "4|3|3", "5|3|3", "6|1|1"],
"three characters is three whatever their byte width"
);
assert_eq!(
vals(
&mut e,
"SELECT left('日本語テキスト',2), right('日本語テキスト',2), left('ábç',2), right('ábç',2)"
),
vec!["日本|スト|áb|bç"]
);
assert_eq!(
vals(
&mut e,
"SELECT left('',1), right('',1), left(123::TEXT,2), right(456::TEXT,2)"
),
vec!["||12|56"]
);
}
/// `substring` / `substr`, whose start is 1-based and may be zero or
/// negative.
#[test]
fn round610_substring() {
let mut e = seed();
assert_eq!(
vals(
&mut e,
"SELECT id, substring(s from 2 for 3), substring(s from 1 for 1), substring(s from 2) \
FROM lt ORDER BY id"
),
vec![
"1|bcd|a|bcdef",
"2|||",
"3|NULL|NULL|NULL",
"4|本語テ|日|本語テキスト",
"5|bç|á|bç",
"6||x|",
],
"no FOR takes the rest"
);
assert_eq!(
vals(
&mut e,
"SELECT id, substr(s,2,3), substr(s,1,0), substr(s,99,3), substr(s,0,3) FROM lt ORDER BY id"
),
vec![
"1|bcd|||ab",
"2||||",
"3|NULL|NULL|NULL|NULL",
"4|本語テ|||日本",
"5|bç|||áb",
"6||||x",
],
"start 0 spends one of the three on the position before the string"
);
assert_eq!(
vals(
&mut e,
"SELECT id, substr(s,-1,3), substr(s,-5,3), substring(s from 0 for 3) FROM lt ORDER BY id"
),
vec![
"1|a||ab",
"2|||",
"3|NULL|NULL|NULL",
"4|日||日本",
"5|á||áb",
"6|x||x",
],
"a negative start counts toward the string and the length is spent getting there"
);
assert_eq!(
vals(
&mut e,
"SELECT id, substring(s from 2 for 99), substr(s,2,0), substr(s,3) FROM lt ORDER BY id"
),
vec![
"1|bcdef||cdef",
"2|||",
"3|NULL|NULL|NULL",
"4|本語テキスト||語テキスト",
"5|bç||ç",
"6|||",
]
);
assert_eq!(
vals(
&mut e,
"SELECT id, length(substr(s,2,3)), length(substring(s from 2)) FROM lt ORDER BY id"
),
vec!["1|3|5", "2|0|0", "3|NULL|NULL", "4|3|6", "5|2|2", "6|0|0"]
);
assert_eq!(
vals(
&mut e,
"SELECT substr('日本語テキスト',2,3), substr('ábç',2,1), substring('ábç' from 3)"
),
vec!["本語テ|b|ç"]
);
assert_eq!(
vals(
&mut e,
"SELECT substr('',1,1), substring('' from 1 for 1), substr(789::TEXT,2,1)"
),
vec!["||8"]
);
}
/// The slices feeding the rest of a query.
#[test]
fn round610_slices_in_use() {
let mut e = seed();
assert_eq!(
vals(
&mut e,
"SELECT id, left(s,3)||'|'||right(s,3) FROM lt ORDER BY id"
),
vec![
"1|abc|def",
"2||",
"3|NULL",
"4|日本語|キスト",
"5|ábç|ábç",
"6|x|x"
]
);
assert_eq!(
vals(
&mut e,
"SELECT id FROM lt WHERE left(s,1) = 'a' OR right(s,1) = 'x' ORDER BY id"
),
vec!["1", "6"]
);
}
/// At the size where building the vector was the cost.
#[test]
fn round610_scale() {
let mut e = Engine::new();
e.execute("CREATE TABLE big (id INT, s TEXT)").unwrap();
e.execute("INSERT INTO big SELECT gg, 'row' || gg FROM generate_series(1, 20000) gg")
.unwrap();
assert_eq!(
vals(&mut e, "SELECT count(*) FROM big WHERE left(s,3) = 'row'"),
vec!["20000"]
);
assert_eq!(
vals(
&mut e,
"SELECT count(*) FROM big WHERE substr(s,4) = id::TEXT"
),
vec!["20000"],
"the tail after 'row' is the id on every row"
);
assert_eq!(
vals(
&mut e,
"SELECT count(*) FROM big WHERE right(s, length(id::TEXT)) = id::TEXT"
),
vec!["20000"]
);
assert_eq!(
vals(&mut e, "SELECT count(DISTINCT left(s,4)) FROM big"),
vals(
&mut e,
"SELECT count(DISTINCT substring(s from 1 for 4)) FROM big"
),
"the two spellings agree"
);
assert_eq!(
vals(
&mut e,
"SELECT count(*) FROM big WHERE left(s,-3) || right(s,3) = s"
),
vec!["20000"],
"the negative-count halves put the string back together"
);
}