Skip to content

Commit 4f14442

Browse files
authored
Merge pull request #147550 from spilchen/blathers/backport-release-25.2.1-rc-147546
release-25.2.1-rc: sql: handle math.MinInt64 count in substring_index builtin
2 parents 4afa288 + a812386 commit 4f14442

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pkg/sql/logictest/testdata/logic_test/builtin_function

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4669,4 +4669,20 @@ SELECT substring_index('a..b..c..d', '..', -2);
46694669
----
46704670
c..d
46714671

4672+
# Verify boundary cases of count input. Found in issue #147516.
4673+
query T
4674+
SELECT substring_index('a..b..c..d', '..', -9223372036854775808);
4675+
----
4676+
a..b..c..d
4677+
4678+
query T
4679+
SELECT substring_index('a..b..c..d', '..', -9223372036854775807);
4680+
----
4681+
a..b..c..d
4682+
4683+
query T
4684+
SELECT substring_index('a..b..c..d', '..', 9223372036854775807);
4685+
----
4686+
a..b..c..d
4687+
46724688
subtest end

pkg/sql/sem/builtins/builtins.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ var regularBuiltins = map[string]builtinDefinition{
422422
}
423423

424424
// If count is negative, return the last 'abs(count)' parts joined by delim
425-
count = -count
426-
if count >= length {
425+
if -count >= length || count == math.MinInt {
427426
return tree.NewDString(input), nil // If count exceeds occurrences, return the full string
428427
}
429-
return tree.NewDString(strings.Join(parts[length-count:], delim)), nil
428+
start := length + count // count is negative
429+
return tree.NewDString(strings.Join(parts[start:], delim)), nil
430430
},
431431
Info: "Returns a substring of `input` before `count` occurrences of `delim`.\n" +
432432
"If `count` is positive, the leftmost part is returned. If `count` is negative, the rightmost part is returned.",

0 commit comments

Comments
 (0)