diff --git a/manifest b/manifest index 2019f703d9..990e4b9844 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C New\sproposal\sfor\s->\sand\s->>\soperators. -D 2022-01-10T13:55:08.463 +C Implement\sthe\snew\sPG-compliant\sversions\sof\sthe\s->\sand\s->>\soperators. +D 2022-01-10T15:43:13.387 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -512,7 +512,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 F src/hwtime.h cb1d7e3e1ed94b7aa6fde95ae2c2daccc3df826be26fc9ed7fd90d1750ae6144 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c e528416ff5d86fc5d656ea6a26f03fde39836b6175f93048c32a03cb2ee16743 -F src/json.c 726f1901ecc66bc359d785c268e70c2f10fcec22b212613b00d7da5aa561d462 +F src/json.c d3134d392d88cb21ab352dd877b901ee5d46e6404fc1321f77cf67bb4d4dc9d3 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 95db1fe62c5973f1c5d9c53f6083e21a73ece14cdd47eeca0639691332e85c4d F src/main.c 2b6b0dbfeb14d4bb57e368604b0736b2aa42b51b00339d399b01d6b1fc9b4960 @@ -1148,7 +1148,7 @@ F test/jrnlmode.test 9b5bc01dac22223cb60ec2d5f97acf568d73820794386de5634dcadbea9 F test/jrnlmode2.test 8759a1d4657c064637f8b079592651530db738419e1d649c6df7048cd724363d F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa F test/json101.test bb71538005f2d9e18620bdd3b76839a93ca0be61903eb8d751a64e78cf99b8fb -F test/json102.test a44a20b7dfa446e67f47d0c02927fe93f80d6ebf3080a5d827757e8d4921c081 +F test/json102.test 86edc7d283085addff8593b178997e75875530d1385f5926717543d3475e6b01 F test/json103.test aff6b7a4c17d5a20b487a7bc1a274bfdc63b829413bdfb83bedac42ec7f67e3b F test/json104.test 2cb7ff2cca2c8214d3e5260eeb9ce45faec0926f68b3e40c1aaa6ca247284144 F test/json105.test 45f7d6a9a54c85f8a9589b68d3e7a1f42d02f2359911a8cdbad1f9988f571173 @@ -1938,8 +1938,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P c3b01d496479b3250a8895c245f79ab43ac469148d163593fea00894db195a37 -R 992262dfca636fa297052ec77a843cce +P 1108e12a2244edc6247050a0e9ad25b912bba6c57c71c51c2bc55167a6955175 +R 7d182b8fa0a61a014b08b226b8a1030b U drh -Z 83712db3104322990e05c7028e4a9b88 +Z b4595bb8abe65d415fc97060e1191b94 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d1f9996545..2dee764db7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -1108e12a2244edc6247050a0e9ad25b912bba6c57c71c51c2bc55167a6955175 \ No newline at end of file +39eff3b9bf1f318d3606d8e5361a2adb2ba8bc1ca2a436ce4f9a204aa4cf20dd \ No newline at end of file diff --git a/src/json.c b/src/json.c index aad4507767..e2dbc7e307 100644 --- a/src/json.c +++ b/src/json.c @@ -1506,40 +1506,30 @@ static void jsonArrayLengthFunc( ** Bit values for the flags passed into jsonExtractFunc() or ** jsonSetFunc() via the user-data value. */ -#define JSON_NULLERR 0x01 /* Return NULL if input is not JSON */ -#define JSON_ABPATH 0x02 /* Allow abbreviated JSON path specs */ +#define JSON_JSON 0x01 /* Result is always JSON */ +#define JSON_SQL 0x02 /* Result is always SQL */ +#define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */ #define JSON_ISSET 0x04 /* json_set(), not json_insert() */ /* ** json_extract(JSON, PATH, ...) -** json_nextract(JSON, PATH, ...) ** "->"(JSON,PATH) ** "->>"(JSON,PATH) ** ** Return the element described by PATH. Return NULL if that PATH element -** is not found. For leaf nodes of the JSON, the value returned is a pure -** SQL value. In other words, quotes have been removed from strings. +** is not found. ** -** If there are multiple PATHs, then the value returned is a JSON array -** with one entry in the array for each PATH term. +** If JSON_JSON is set or if more that one PATH argument is supplied then +** always return a JSON representation of the result. If JSON_SQL is set, +** then always return an SQL representation of the result. If neither flag +** is present and argc==2, then return JSON for objects and arrays and SQL +** for all other values. ** -** Throw an error if any PATH is malformed. +** When multiple PATH arguments are supplied, the result is a JSON array +** containing the result of each PATH. ** -** If JSON is not well-formed JSON then: -** -** (1) raise an error if the JSON_NULLERR flag is not set. -** -** (2) Otherwise (if the JSON_NULLERR flags is set and) if there -** is a single PATH argument with the value '$', simply quote -** the JSON input as if by json_quote(). In other words, treat -** the JSON input as a string and convert it into a valid JSON -** string. -** -** (3) Otherwise (if JSON_NULLERR is set and the PATH is not '$') -** return NULL -** -** If the JSON_ABPATH flag is set and there is only a single PATH, then -** allow abbreviated PATH specs that omit the leading "$". +** Abbreviated JSON path expressions are allows if JSON_ABPATH, for +** compatibility with PG. */ static void jsonExtractFunc( sqlite3_context *ctx, @@ -1553,23 +1543,13 @@ static void jsonExtractFunc( JsonString jx; if( argc<2 ) return; - p = jsonParseCached(ctx, argv, (flags & JSON_NULLERR)!=0 ? 0 : ctx); - if( p==0 ){ - /* If the form is "json_nextract(IN,'$')" and IN is not well-formed JSON, - ** then return IN as a quoted JSON string. */ - if( (flags & JSON_NULLERR)!=0 - && argc==2 - && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0 - && zPath[0]=='$' && zPath[1]==0 - ){ - jsonQuoteFunc(ctx, argc, argv); - } - return; - } + p = jsonParseCached(ctx, argv, ctx); + if( p==0 ) return; if( argc==2 ){ - /* With a single PATH argument, the return is the unquoted SQL value */ + /* With a single PATH argument */ zPath = (const char*)sqlite3_value_text(argv[1]); - if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & JSON_ABPATH)!=0 ){ + if( zPath==0 ) return; + if( zPath[0]!='$' && (flags & JSON_ABPATH)!=0 ){ /* The -> and ->> operators accept abbreviated PATH arguments. This ** is mostly for compatibility with PostgreSQL, but also for convenience. ** @@ -1587,13 +1567,22 @@ static void jsonExtractFunc( jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); jsonAppendChar(&jx, 0); } - pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx); + if( jx.bErr==0 ){ + pNode = jsonLookup(p, jx.zBuf, 0, ctx); + if( pNode==0 ){ + /* No-op. jsonLookup will have left an error for us */ + }else if( flags & JSON_JSON ){ + jsonReturnJson(pNode, ctx, 0); + }else{ + jsonReturn(pNode, ctx, 0); + sqlite3_result_subtype(ctx, 0); + } + } jsonReset(&jx); }else{ pNode = jsonLookup(p, zPath, 0, ctx); + if( p->nErr==0 && pNode ) jsonReturn(pNode, ctx, 0); } - if( p->nErr ) return; - if( pNode ) jsonReturn(pNode, ctx, 0); }else{ /* Two or more PATH arguments results in a JSON array with each ** element of the array being the value selected by one of the PATHs */ @@ -2637,11 +2626,9 @@ void sqlite3RegisterJsonFunctions(void){ JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc), JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc), JFUNCTION(json_extract, -1, 0, jsonExtractFunc), - JFUNCTION(json_nextract, -1, JSON_NULLERR, jsonExtractFunc), - JFUNCTION(->, 2, JSON_NULLERR|JSON_ABPATH, jsonExtractFunc), - JFUNCTION(->>, 2, JSON_ABPATH, jsonExtractFunc), + JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc), + JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc), JFUNCTION(json_insert, -1, 0, jsonSetFunc), - JFUNCTION(json_ntype, 1, JSON_NULLERR, jsonTypeFunc), JFUNCTION(json_object, -1, 0, jsonObjectFunc), JFUNCTION(json_patch, 2, 0, jsonPatchFunc), JFUNCTION(json_quote, 1, 0, jsonQuoteFunc), diff --git a/test/json102.test b/test/json102.test index 7ae99353bf..9f8f482b85 100644 --- a/test/json102.test +++ b/test/json102.test @@ -71,70 +71,21 @@ do_execsql_test json102-240 { do_execsql_test json102-250 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$'); } {{{"a":2,"c":[4,5,{"f":7}]}}} -do_execsql_test json102-251 { - SELECT json_nextract('{"a":2,"c":[4,5,{"f":7}]}', '$'); -} {{{"a":2,"c":[4,5,{"f":7}]}}} -do_execsql_test json102-252 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$'; -} {{{"a":2,"c":[4,5,{"f":7}]}}} do_execsql_test json102-260 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c'); } {{[4,5,{"f":7}]}} -do_execsql_test json102-261 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c'; -} {{[4,5,{"f":7}]}} -do_execsql_test json102-262 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c'; -} {{[4,5,{"f":7}]}} -do_catchsql_test json102-265 { - SELECT json_extract('[1,2,3', '$[2]'); -} {1 {malformed JSON}} -do_catchsql_test json102-266 { - SELECT json_nextract('[1,2,3', '$[2]'); -} {0 {{}}} -do_catchsql_test json102-267 { - SELECT json_extract('[1,2,3', '$'); -} {1 {malformed JSON}} -do_catchsql_test json102-268 { - SELECT json_nextract('[1,2,3', '$'); -} {0 {{"[1,2,3"}}} -do_catchsql_test json102-269a { - SELECT '[1,2,3' ->> '$'; -} {1 {malformed JSON}} -do_catchsql_test json102-269b { - SELECT '[1,2,3' -> '$'; -} {0 {{"[1,2,3"}}} - do_execsql_test json102-270 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]'); } {{{"f":7}}} -do_execsql_test json102-271 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]'; -} {{{"f":7}}} -do_execsql_test json102-272 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2; -} {{{"f":7}}} do_execsql_test json102-280 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f'); } {{7}} -do_execsql_test json102-281 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 -> 'f'; -} {{7}} -do_execsql_test json102-282 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> '[2]' -> 'f'; -} {{7}} do_execsql_test json102-290 { SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a'); } {{[[4,5],2]}} do_execsql_test json102-300 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x'); } {{}} -do_execsql_test json102-301 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'x'; -} {{}} -do_execsql_test json102-302 { - SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> NULL; -} {{}} do_execsql_test json102-310 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a'); } {{[null,2]}} @@ -198,9 +149,6 @@ do_execsql_test json102-500 { do_execsql_test json102-510 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}'); } {{object}} -do_execsql_test json102-511 { - SELECT json_ntype('{"a":[2,3.5,true,false,null,"x"]}'); -} {{object}} do_execsql_test json102-520 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$'); } {{object}} @@ -234,12 +182,6 @@ do_execsql_test json102-610 { do_execsql_test json102-620 { SELECT json_valid(char(123)||'"x":35'); } {{0}} -do_catchsql_test json102-630 { - SELECT json_type('["a",'); -} {1 {malformed JSON}} -do_catchsql_test json102-631 { - SELECT json_ntype('["a",'); -} {0 {{}}} ifcapable vtab { do_execsql_test json102-1000 { @@ -395,4 +337,60 @@ do_execsql_test json102-1501 { SELECT sum(json_valid(json_quote('a'||char(x)||'z'))) FROM c ORDER BY x; } {31} +# 2022-01-10 tests for -> and ->> operators +# +reset_db +do_execsql_test json102-1600 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, x JSON); + INSERT INTO t1(id,x) VALUES + (1, '{"a":null}'), + (2, '{"a":123}'), + (3, '{"a":4.5}'), + (4, '{"a":"six"}'), + (5, '{"a":[7,8]}'), + (6, '{"a":{"b":9}}'), + (7, '{"b":999}'); + SELECT + id, + x->'a' AS '->', + CASE WHEN subtype(x->'a') THEN 'json' ELSE typeof(x->'a') END AS 'type', + x->>'a' AS '->>', + CASE WHEN subtype(x->>'a') THEN 'json' ELSE typeof(x->>'a') END AS 'type', + json_extract(x,'$.a') AS 'json_extract', + CASE WHEN subtype(json_extract(x,'$.a')) + THEN 'json' ELSE typeof(json_extract(x,'$.a')) END AS 'type' + FROM t1 ORDER BY id; +} [list \ + 1 null json {} null {} null \ + 2 123 json 123 integer 123 integer \ + 3 4.5 json 4.5 real 4.5 real \ + 4 {"six"} json six text six text \ + 5 {[7,8]} json {[7,8]} text {[7,8]} json \ + 6 {{"b":9}} json {{"b":9}} text {{"b":9}} json \ + 7 {} null {} null {} null +] +do_execsql_test json102-1610 { + DELETE FROM t1; + INSERT INTO t1(x) VALUES('[null,123,4.5,"six",[7,8],{"b":9}]'); + WITH c(y) AS (VALUES(0),(1),(2),(3),(4),(5),(6)) + SELECT + y, + x->y AS '->', + CASE WHEN subtype(x->y) THEN 'json' ELSE typeof(x->y) END AS 'type', + x->>y AS '->>', + CASE WHEN subtype(x->>y) THEN 'json' ELSE typeof(x->>y) END AS 'type', + json_extract(x,format('$[%d]',y)) AS 'json_extract', + CASE WHEN subtype(json_extract(x,format('$[%d]',y))) + THEN 'json' ELSE typeof(json_extract(x,format('$[%d]',y))) END AS 'type' + FROM c, t1 ORDER BY y; +} [list \ + 0 null json {} null {} null \ + 1 123 json 123 integer 123 integer \ + 2 4.5 json 4.5 real 4.5 real \ + 3 {"six"} json six text six text \ + 4 {[7,8]} json {[7,8]} text {[7,8]} json \ + 5 {{"b":9}} json {{"b":9}} text {{"b":9}} json \ + 6 {} null {} null {} null +] + finish_test