Skip to content

Commit 17ca067

Browse files
committed
Clean up parsing of ltree and lquery some more.
Fix lquery parsing to handle repeated flag characters correctly, and to enforce the max label length correctly in some cases where it did not before, and to detect empty labels in some cases where it did not before. In a more cosmetic vein, use a switch rather than if-then chains to handle the different states, and avoid unnecessary checks on charlen when looking for ASCII characters, and factor out multiple copies of the label length checking code. Tom Lane and Dmitry Belyavsky Discussion: https://postgr.es/m/CADqLbzLVkBuPX0812o+z=c3i6honszsZZ6VQOSKR3VPbB56P3w@mail.gmail.com
1 parent 949a9f0 commit 17ca067

File tree

3 files changed

+362
-278
lines changed

3 files changed

+362
-278
lines changed

contrib/ltree/expected/ltree.out

+98
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ SELECT '1.2._3'::ltree;
3131
1.2._3
3232
(1 row)
3333

34+
-- empty labels not allowed
35+
SELECT '.2.3'::ltree;
36+
ERROR: ltree syntax error at character 1
37+
LINE 1: SELECT '.2.3'::ltree;
38+
^
39+
SELECT '1..3'::ltree;
40+
ERROR: ltree syntax error at character 3
41+
LINE 1: SELECT '1..3'::ltree;
42+
^
43+
SELECT '1.2.'::ltree;
44+
ERROR: ltree syntax error
45+
LINE 1: SELECT '1.2.'::ltree;
46+
^
47+
DETAIL: Unexpected end of input.
48+
SELECT repeat('x', 255)::ltree;
49+
repeat
50+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
51+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
52+
(1 row)
53+
54+
SELECT repeat('x', 256)::ltree;
55+
ERROR: label string is too long
56+
DETAIL: Label length is 256, must be at most 255, at character 257.
3457
SELECT ltree2text('1.2.3.34.sdf');
3558
ltree2text
3659
--------------
@@ -451,12 +474,81 @@ SELECT 'foo.bar{,}.!a*|b{1,}.c{,44}.d{3,4}'::lquery;
451474
foo.bar{,}.!a*|b{1,}.c{,44}.d{3,4}
452475
(1 row)
453476

477+
SELECT 'foo*@@*'::lquery;
478+
lquery
479+
--------
480+
foo@*
481+
(1 row)
482+
454483
SELECT 'qwerty%@*.tu'::lquery;
455484
lquery
456485
--------------
457486
qwerty%@*.tu
458487
(1 row)
459488

489+
-- empty labels not allowed
490+
SELECT '.2.3'::lquery;
491+
ERROR: lquery syntax error at character 1
492+
LINE 1: SELECT '.2.3'::lquery;
493+
^
494+
SELECT '1..3'::lquery;
495+
ERROR: lquery syntax error at character 3
496+
LINE 1: SELECT '1..3'::lquery;
497+
^
498+
SELECT '1.2.'::lquery;
499+
ERROR: lquery syntax error
500+
LINE 1: SELECT '1.2.'::lquery;
501+
^
502+
DETAIL: Unexpected end of input.
503+
SELECT '@.2.3'::lquery;
504+
ERROR: lquery syntax error at character 1
505+
LINE 1: SELECT '@.2.3'::lquery;
506+
^
507+
SELECT '[email protected]'::lquery;
508+
ERROR: lquery syntax error at character 3
509+
LINE 1: SELECT '[email protected]'::lquery;
510+
^
511+
SELECT '1.2.@'::lquery;
512+
ERROR: lquery syntax error at character 5
513+
LINE 1: SELECT '1.2.@'::lquery;
514+
^
515+
SELECT '!.2.3'::lquery;
516+
ERROR: lquery syntax error at character 2
517+
LINE 1: SELECT '!.2.3'::lquery;
518+
^
519+
DETAIL: Empty labels are not allowed.
520+
SELECT '1.!.3'::lquery;
521+
ERROR: lquery syntax error at character 4
522+
LINE 1: SELECT '1.!.3'::lquery;
523+
^
524+
DETAIL: Empty labels are not allowed.
525+
SELECT '1.2.!'::lquery;
526+
ERROR: lquery syntax error at character 6
527+
LINE 1: SELECT '1.2.!'::lquery;
528+
^
529+
DETAIL: Empty labels are not allowed.
530+
SELECT '1.2.3|@.4'::lquery;
531+
ERROR: lquery syntax error at character 7
532+
LINE 1: SELECT '1.2.3|@.4'::lquery;
533+
^
534+
SELECT (repeat('x', 255) || '*@@*')::lquery;
535+
lquery
536+
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
537+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@*
538+
(1 row)
539+
540+
SELECT (repeat('x', 256) || '*@@*')::lquery;
541+
ERROR: label string is too long
542+
DETAIL: Label length is 256, must be at most 255, at character 257.
543+
SELECT ('!' || repeat('x', 255))::lquery;
544+
lquery
545+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
546+
!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
547+
(1 row)
548+
549+
SELECT ('!' || repeat('x', 256))::lquery;
550+
ERROR: label string is too long
551+
DETAIL: Label length is 256, must be at most 255, at character 258.
460552
SELECT nlevel('1.2.3.4');
461553
nlevel
462554
--------
@@ -1072,6 +1164,12 @@ SELECT 'QWER_TY'::ltree ~ 'q%@*';
10721164
t
10731165
(1 row)
10741166

1167+
SELECT 'QWER_TY'::ltree ~ 'q%@*%@*';
1168+
?column?
1169+
----------
1170+
t
1171+
(1 row)
1172+
10751173
SELECT 'QWER_TY'::ltree ~ 'Q_t%@*';
10761174
?column?
10771175
----------

0 commit comments

Comments
 (0)