I noticed that the JSON path lexer does not support the decimal literal syntax forms

.1
1.

(that is, there are no digits before or after the decimal point). This is allowed by the relevant ECMAScript standard (https://262.ecma-international.org/5.1/#sec-7.8.3) and of course SQL allows it as well.

Is there a reason for this? I didn't find any code comments or documentation about this.

Attached are patches that would enable this. As you can see, a bunch of test cases are affected.
From 9fc73f1fa4d83da85dc1626cf8b218ec8a11104c Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Fri, 18 Feb 2022 10:52:56 +0100
Subject: [PATCH v1 1/2] Test cases for JSON path decimal literals

---
 src/test/regress/expected/jsonpath.out | 18 ++++++++++++++++++
 src/test/regress/sql/jsonpath.sql      |  4 ++++
 2 files changed, 22 insertions(+)

diff --git a/src/test/regress/expected/jsonpath.out 
b/src/test/regress/expected/jsonpath.out
index e399fa9631..54eb548ad1 100644
--- a/src/test/regress/expected/jsonpath.out
+++ b/src/test/regress/expected/jsonpath.out
@@ -878,6 +878,24 @@ select '0.0010e+2'::jsonpath;
  0.10
 (1 row)
 
+select '.001'::jsonpath;
+ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
+LINE 1: select '.001'::jsonpath;
+               ^
+select '.001e1'::jsonpath;
+ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
+LINE 1: select '.001e1'::jsonpath;
+               ^
+select '1.'::jsonpath;
+ERROR:  syntax error, unexpected end of file at end of jsonpath input
+LINE 1: select '1.'::jsonpath;
+               ^
+select '1.e1'::jsonpath;
+ jsonpath 
+----------
+ 1."e1"
+(1 row)
+
 select '1e'::jsonpath;
 ERROR:  invalid floating point number at or near "1e" of jsonpath input
 LINE 1: select '1e'::jsonpath;
diff --git a/src/test/regress/sql/jsonpath.sql 
b/src/test/regress/sql/jsonpath.sql
index 17ab775783..bf71b99fc5 100644
--- a/src/test/regress/sql/jsonpath.sql
+++ b/src/test/regress/sql/jsonpath.sql
@@ -163,6 +163,10 @@
 select '0.0010e-1'::jsonpath;
 select '0.0010e+1'::jsonpath;
 select '0.0010e+2'::jsonpath;
+select '.001'::jsonpath;
+select '.001e1'::jsonpath;
+select '1.'::jsonpath;
+select '1.e1'::jsonpath;
 select '1e'::jsonpath;
 select '1.e'::jsonpath;
 select '1.2e'::jsonpath;
-- 
2.35.1

From 1881760218f15749122460eb3a71a0b648b5c86b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Fri, 18 Feb 2022 11:11:18 +0100
Subject: [PATCH v1 2/2] Fix JSON path decimal literal syntax

Per ECMAScript standard (referenced by SQL standard), the syntax forms

.1
1.

should be allowed for decimal numeric literals, but the existing
implementation rejected them.
---
 src/backend/utils/adt/jsonpath_scan.l  |  12 +-
 src/test/regress/expected/jsonpath.out | 184 +++++++++++++++----------
 2 files changed, 110 insertions(+), 86 deletions(-)

diff --git a/src/backend/utils/adt/jsonpath_scan.l 
b/src/backend/utils/adt/jsonpath_scan.l
index 827a9e44cb..bf1cea8c03 100644
--- a/src/backend/utils/adt/jsonpath_scan.l
+++ b/src/backend/utils/adt/jsonpath_scan.l
@@ -82,8 +82,7 @@ other         
[^\?\%\$\.\[\]\{\}\(\)\|\&\!\=\<\>\@\#\,\*:\-\+\/\\\" \t\n\r\f]
 
 digit          [0-9]
 integer                (0|[1-9]{digit}*)
-decimal                {integer}\.{digit}+
-decimalfail    {integer}\.
+decimal                ({integer}\.{digit}*|\.{digit}+)
 real           ({integer}|{decimal})[Ee][-+]?{digit}+
 realfail1      ({integer}|{decimal})[Ee]
 realfail2      ({integer}|{decimal})[Ee][-+]
@@ -242,15 +241,6 @@ hex_fail   \\x{hex_dig}{0,1}
                                                                        return 
INT_P;
                                                                }
 
-{decimalfail}                                  {
-                                                                       /* 
throw back the ., and treat as integer */
-                                                                       
yyless(yyleng - 1);
-                                                                       
addstring(true, yytext, yyleng);
-                                                                       
addchar(false, '\0');
-                                                                       
yylval->str = scanstring;
-                                                                       return 
INT_P;
-                                                               }
-
 ({realfail1}|{realfail2})              { yyerror(NULL, "invalid floating point 
number"); }
 
 \"                                                             {
diff --git a/src/test/regress/expected/jsonpath.out 
b/src/test/regress/expected/jsonpath.out
index 54eb548ad1..058010172f 100644
--- a/src/test/regress/expected/jsonpath.out
+++ b/src/test/regress/expected/jsonpath.out
@@ -354,11 +354,9 @@ select 'null.type()'::jsonpath;
 (1 row)
 
 select '1.type()'::jsonpath;
- jsonpath 
-----------
- 1.type()
-(1 row)
-
+ERROR:  syntax error, unexpected TYPE_P, expecting end of file at end of 
jsonpath input
+LINE 1: select '1.type()'::jsonpath;
+               ^
 select '(1).type()'::jsonpath;
  jsonpath 
 ----------
@@ -569,17 +567,23 @@ select '$ ? (@.a < +1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < .1)'::jsonpath;
-               ^
+    jsonpath     
+-----------------
+ $?(@."a" < 0.1)
+(1 row)
+
 select '$ ? (@.a < -.1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < -.1)'::jsonpath;
-               ^
+     jsonpath     
+------------------
+ $?(@."a" < -0.1)
+(1 row)
+
 select '$ ? (@.a < +.1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < +.1)'::jsonpath;
-               ^
+    jsonpath     
+-----------------
+ $?(@."a" < 0.1)
+(1 row)
+
 select '$ ? (@.a < 0.1)'::jsonpath;
     jsonpath     
 -----------------
@@ -635,17 +639,23 @@ select '$ ? (@.a < +1e1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < .1e1)'::jsonpath;
-               ^
+   jsonpath    
+---------------
+ $?(@."a" < 1)
+(1 row)
+
 select '$ ? (@.a < -.1e1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < -.1e1)'::jsonpath;
-               ^
+    jsonpath    
+----------------
+ $?(@."a" < -1)
+(1 row)
+
 select '$ ? (@.a < +.1e1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < +.1e1)'::jsonpath;
-               ^
+   jsonpath    
+---------------
+ $?(@."a" < 1)
+(1 row)
+
 select '$ ? (@.a < 0.1e1)'::jsonpath;
    jsonpath    
 ---------------
@@ -701,17 +711,23 @@ select '$ ? (@.a < +1e-1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e-1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < .1e-1)'::jsonpath;
-               ^
+     jsonpath     
+------------------
+ $?(@."a" < 0.01)
+(1 row)
+
 select '$ ? (@.a < -.1e-1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < -.1e-1)'::jsonpath;
-               ^
+     jsonpath      
+-------------------
+ $?(@."a" < -0.01)
+(1 row)
+
 select '$ ? (@.a < +.1e-1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < +.1e-1)'::jsonpath;
-               ^
+     jsonpath     
+------------------
+ $?(@."a" < 0.01)
+(1 row)
+
 select '$ ? (@.a < 0.1e-1)'::jsonpath;
      jsonpath     
 ------------------
@@ -767,17 +783,23 @@ select '$ ? (@.a < +1e+1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e+1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < .1e+1)'::jsonpath;
-               ^
+   jsonpath    
+---------------
+ $?(@."a" < 1)
+(1 row)
+
 select '$ ? (@.a < -.1e+1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < -.1e+1)'::jsonpath;
-               ^
+    jsonpath    
+----------------
+ $?(@."a" < -1)
+(1 row)
+
 select '$ ? (@.a < +.1e+1)'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '$ ? (@.a < +.1e+1)'::jsonpath;
-               ^
+   jsonpath    
+---------------
+ $?(@."a" < 1)
+(1 row)
+
 select '$ ? (@.a < 0.1e+1)'::jsonpath;
    jsonpath    
 ---------------
@@ -879,21 +901,27 @@ select '0.0010e+2'::jsonpath;
 (1 row)
 
 select '.001'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '.001'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 0.001
+(1 row)
+
 select '.001e1'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '.001e1'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 0.01
+(1 row)
+
 select '1.'::jsonpath;
-ERROR:  syntax error, unexpected end of file at end of jsonpath input
-LINE 1: select '1.'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 1
+(1 row)
+
 select '1.e1'::jsonpath;
  jsonpath 
 ----------
- 1."e1"
+ 10
 (1 row)
 
 select '1e'::jsonpath;
@@ -901,11 +929,9 @@ ERROR:  invalid floating point number at or near "1e" of 
jsonpath input
 LINE 1: select '1e'::jsonpath;
                ^
 select '1.e'::jsonpath;
- jsonpath 
-----------
- 1."e"
-(1 row)
-
+ERROR:  invalid floating point number at or near "1.e" of jsonpath input
+LINE 1: select '1.e'::jsonpath;
+               ^
 select '1.2e'::jsonpath;
 ERROR:  invalid floating point number at or near "1.2e" of jsonpath input
 LINE 1: select '1.2e'::jsonpath;
@@ -931,19 +957,19 @@ select '1e3'::jsonpath;
 select '1.e3'::jsonpath;
  jsonpath 
 ----------
- 1."e3"
+ 1000
 (1 row)
 
 select '1.e3.e'::jsonpath;
-  jsonpath  
-------------
- 1."e3"."e"
+ jsonpath 
+----------
+ 1000."e"
 (1 row)
 
 select '1.e3.e4'::jsonpath;
-  jsonpath   
--------------
- 1."e3"."e4"
+ jsonpath  
+-----------
+ 1000."e4"
 (1 row)
 
 select '1.2e3'::jsonpath;
@@ -965,18 +991,26 @@ select '(1.2).e3'::jsonpath;
 (1 row)
 
 select '1..e'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '1..e'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 1."e"
+(1 row)
+
 select '1..e3'::jsonpath;
-ERROR:  syntax error, unexpected '.' at or near "." of jsonpath input
-LINE 1: select '1..e3'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 1."e3"
+(1 row)
+
 select '(1.).e'::jsonpath;
-ERROR:  syntax error, unexpected ')' at or near ")" of jsonpath input
-LINE 1: select '(1.).e'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 1."e"
+(1 row)
+
 select '(1.).e3'::jsonpath;
-ERROR:  syntax error, unexpected ')' at or near ")" of jsonpath input
-LINE 1: select '(1.).e3'::jsonpath;
-               ^
+ jsonpath 
+----------
+ 1."e3"
+(1 row)
+
-- 
2.35.1

Reply via email to