summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-03-03 16:39:57 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2021-03-03 16:39:57 -0500
commit0a687c8f103d217ff1ca8c34a644b380d89bb0ad (patch)
tree6406e23e84feca1351f879b7ddaaa9edafb4cf6a /src/test
parent3769e11a31831fc2f3bd4c4a24b4f45c352fb8fb (diff)
Add trim_array() function.
This has been in the SQL spec since 2008. It's a pretty thin wrapper around the array slice functionality, but the spec says we should have it, so here it is. Vik Fearing, reviewed by Dian Fay Discussion: https://postgr.es/m/fc92ce17-9655-8ff1-c62a-4dc4c8ccd815@postgresfriends.org
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/arrays.out21
-rw-r--r--src/test/regress/sql/arrays.sql13
2 files changed, 34 insertions, 0 deletions
diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.out
index 8bc7721e7d5..3e3a1beaab3 100644
--- a/src/test/regress/expected/arrays.out
+++ b/src/test/regress/expected/arrays.out
@@ -2399,3 +2399,24 @@ SELECT width_bucket(5, ARRAY[3, 4, NULL]);
ERROR: thresholds array must not contain NULLs
SELECT width_bucket(5, ARRAY[ARRAY[1, 2], ARRAY[3, 4]]);
ERROR: thresholds must be one-dimensional array
+-- trim_array
+SELECT arr, trim_array(arr, 2)
+FROM
+(VALUES ('{1,2,3,4,5,6}'::bigint[]),
+ ('{1,2}'),
+ ('[10:16]={1,2,3,4,5,6,7}'),
+ ('[-15:-10]={1,2,3,4,5,6}'),
+ ('{{1,10},{2,20},{3,30},{4,40}}')) v(arr);
+ arr | trim_array
+-------------------------------+-----------------
+ {1,2,3,4,5,6} | {1,2,3,4}
+ {1,2} | {}
+ [10:16]={1,2,3,4,5,6,7} | {1,2,3,4,5}
+ [-15:-10]={1,2,3,4,5,6} | {1,2,3,4}
+ {{1,10},{2,20},{3,30},{4,40}} | {{1,10},{2,20}}
+(5 rows)
+
+SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
+ERROR: number of elements to trim must be between 0 and 3
+SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail
+ERROR: number of elements to trim must be between 0 and 3
diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sql
index c40619a8d5d..912233ef968 100644
--- a/src/test/regress/sql/arrays.sql
+++ b/src/test/regress/sql/arrays.sql
@@ -722,3 +722,16 @@ SELECT width_bucket(5, '{}');
SELECT width_bucket('5'::text, ARRAY[3, 4]::integer[]);
SELECT width_bucket(5, ARRAY[3, 4, NULL]);
SELECT width_bucket(5, ARRAY[ARRAY[1, 2], ARRAY[3, 4]]);
+
+-- trim_array
+
+SELECT arr, trim_array(arr, 2)
+FROM
+(VALUES ('{1,2,3,4,5,6}'::bigint[]),
+ ('{1,2}'),
+ ('[10:16]={1,2,3,4,5,6,7}'),
+ ('[-15:-10]={1,2,3,4,5,6}'),
+ ('{{1,10},{2,20},{3,30},{4,40}}')) v(arr);
+
+SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
+SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail