304 lines
4.8 KiB
SQL
304 lines
4.8 KiB
SQL
# name: test/sql/function/string/test_substring.test
|
|
# description: Substring test
|
|
# group: [string]
|
|
|
|
statement ok
|
|
PRAGMA enable_verification
|
|
|
|
statement ok
|
|
CREATE TABLE strings(s VARCHAR, off INTEGER, length INTEGER);
|
|
|
|
statement ok
|
|
INSERT INTO strings VALUES ('hello', 1, 2), ('world', 2, 3), ('b', 1, 1), (NULL, 2, 2)
|
|
|
|
foreach FUN substring substring_grapheme
|
|
|
|
# test zero length
|
|
query TT
|
|
SELECT ${FUN}('🦆ab', 1, 0), ${FUN}('abc', 1, 0)
|
|
----
|
|
(empty) (empty)
|
|
|
|
# constant offset/length
|
|
# normal substring
|
|
query T
|
|
SELECT substring(s from 1 for 2) FROM strings
|
|
----
|
|
he
|
|
wo
|
|
b
|
|
NULL
|
|
|
|
# substring out of range
|
|
query T
|
|
SELECT substring(s from 2 for 2) FROM strings
|
|
----
|
|
el
|
|
or
|
|
(empty)
|
|
NULL
|
|
|
|
# variable length offset/length
|
|
query T
|
|
SELECT substring(s from off for length) FROM strings
|
|
----
|
|
he
|
|
orl
|
|
b
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring(s from off for 2) FROM strings
|
|
----
|
|
he
|
|
or
|
|
b
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring(s from 1 for length) FROM strings
|
|
----
|
|
he
|
|
wor
|
|
b
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring('hello' from off for length) FROM strings
|
|
----
|
|
he
|
|
ell
|
|
h
|
|
el
|
|
|
|
# test substrings with constant nulls in different places
|
|
query T
|
|
SELECT substring(NULL from off for length) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring('hello' from NULL for length) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring('hello' from off for NULL) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring(NULL from NULL for length) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring('hello' from NULL for NULL) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring(NULL from off for NULL) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
query T
|
|
SELECT substring(NULL from NULL for NULL) FROM strings
|
|
----
|
|
NULL
|
|
NULL
|
|
NULL
|
|
NULL
|
|
|
|
# fixed slice
|
|
query T
|
|
SELECT substring(s from -2 for 2) FROM strings
|
|
----
|
|
lo
|
|
ld
|
|
b
|
|
NULL
|
|
|
|
# zero offset (this is accepted by SQLite)
|
|
query T
|
|
SELECT substring(s from 0 for length) FROM strings
|
|
----
|
|
h
|
|
wo
|
|
(empty)
|
|
NULL
|
|
|
|
# negative length
|
|
query T
|
|
SELECT ${FUN}(s, 2, -2) FROM strings
|
|
----
|
|
h
|
|
w
|
|
b
|
|
NULL
|
|
|
|
# negative offset and negative length
|
|
query T
|
|
SELECT ${FUN}(s, -2, -2) FROM strings
|
|
----
|
|
el
|
|
or
|
|
(empty)
|
|
NULL
|
|
|
|
# length 0
|
|
query T
|
|
SELECT ${FUN}(s, 2, 0) FROM strings
|
|
----
|
|
(empty)
|
|
(empty)
|
|
(empty)
|
|
NULL
|
|
|
|
# no length
|
|
query T
|
|
SELECT ${FUN}(s, 2) FROM strings
|
|
----
|
|
ello
|
|
orld
|
|
(empty)
|
|
NULL
|
|
|
|
query T
|
|
SELECT ${FUN}(${FUN}(s, 2), 2) FROM strings
|
|
----
|
|
llo
|
|
rld
|
|
(empty)
|
|
NULL
|
|
|
|
# very large offset and length
|
|
query T
|
|
SELECT ${FUN}(s, 2147483647, 2147483647) FROM strings
|
|
----
|
|
(empty)
|
|
(empty)
|
|
(empty)
|
|
NULL
|
|
|
|
query T
|
|
SELECT ${FUN}(s, 2147483647, -2147483648) FROM strings
|
|
----
|
|
hello
|
|
world
|
|
b
|
|
NULL
|
|
|
|
query T
|
|
SELECT ${FUN}(s, -2147483647, 2147483647) FROM strings
|
|
----
|
|
hello
|
|
world
|
|
b
|
|
NULL
|
|
|
|
query T
|
|
SELECT ${FUN}(s, -2147483648, -2147483648) FROM strings
|
|
----
|
|
(empty)
|
|
(empty)
|
|
(empty)
|
|
NULL
|
|
|
|
# Issue #2553 - accept BIGINT arguments
|
|
query I
|
|
SELECT ${FUN}('abc', INSTR('abc', 'b'));
|
|
----
|
|
bc
|
|
|
|
# Issue #4978 - substring integer overflow
|
|
query I
|
|
SELECT ${FUN}('a', -1)
|
|
----
|
|
a
|
|
|
|
query I
|
|
SELECT ${FUN}('abcd', -1)
|
|
----
|
|
d
|
|
|
|
query I
|
|
SELECT ${FUN}('abcd', -7)
|
|
----
|
|
abcd
|
|
|
|
# Even tough we accept bigints, we don't allow offsets and lengths larger than
|
|
# a 32-bit integer, since we need to be able to do the internal resulting string
|
|
# length calculations within a 64-bit integer to avoid overflows.
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 9223372036854775807, -9223372036854775808) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (> 4294967295)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, -9223372036854775808, -9223372036854775808) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (< -4294967296)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 9223372036854775807, 9223372036854775807) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (> 4294967295)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, -9223372036854775808, 9223372036854775807) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (< -4294967296)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 0, 9223372036854775807) FROM strings
|
|
----
|
|
Out of Range Error: Substring length outside of supported range (> 4294967295)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 0, -9223372036854775808) FROM strings
|
|
----
|
|
Out of Range Error: Substring length outside of supported range (< -4294967296)
|
|
|
|
# int32_t limits
|
|
statement error
|
|
SELECT ${FUN}(s, 4294967296, 2147483647) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (> 4294967295)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, -4294967297, 2147483647) FROM strings
|
|
----
|
|
Out of Range Error: Substring offset outside of supported range (< -4294967296)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 0, 4294967296) FROM strings
|
|
----
|
|
Out of Range Error: Substring length outside of supported range (> 4294967295)
|
|
|
|
statement error
|
|
SELECT ${FUN}(s, 0, -4294967297) FROM strings
|
|
----
|
|
Out of Range Error: Substring length outside of supported range (< -4294967296)
|
|
|
|
endloop
|