]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
The json_patch() code for JSONB compiles and works sometimes, but there are
authordrh <>
Tue, 28 Nov 2023 12:28:28 +0000 (12:28 +0000)
committerdrh <>
Tue, 28 Nov 2023 12:28:28 +0000 (12:28 +0000)
still issues.  Incremental check-in.

FossilOrigin-Name: e0099464a0045a04f4ccf29bc2b8325fc8c7f39ccf4847e74818f928c9153588

manifest
manifest.uuid
src/json.c

index eb129dd02f8ef8e3f24c4760b457f2216ce419ce..3e1841dc0fd7022898a8d79132cf0af41af8716e 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C More\saggressive\suse\sof\sjsonBlobEdit().\s\sImprovements\sto\sthe\sMergePatch\nimplementation\ssketch.
-D 2023-11-28T00:27:58.169
+C The\sjson_patch()\scode\sfor\sJSONB\scompiles\sand\sworks\ssometimes,\sbut\sthere\sare\nstill\sissues.\s\sIncremental\scheck-in.
+D 2023-11-28T12:28:28.510
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -688,7 +688,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
-F src/json.c a91647a3c56298ff94bb2d5a989e8e329c4435e3a0ae9760b04b46c7b2083541
+F src/json.c 2dfbca7572f686feea28665d524a34cdde23cf3dcb15cfa79cabe77f8032524f
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2145,8 +2145,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 4d353387fc10e1038cfdd86e66007bf728c231a928e588897bbee0fbfe76f225
-R 42f4ec3bb9c19f358b256294966169e5
+P fbca9570fd2e1465739e4d3a8d9bb40fad594fd78ab49b2cb34efa27ebdd8361
+R 74a90e7c3058a227c8ae29f97ecb7181
+T *branch * jsonb-patch
+T *sym-jsonb-patch *
+T -sym-jsonb *
 U drh
-Z 551901455bde8067c499befcab52664b
+Z 73c44f2da8d785efd9b8e3ab2061af48
 # Remove this line to create a well-formed Fossil manifest.
index 6460236f38ef71b92d7b9a7a3921ab58db5f060a..477eefc52236bd3789f5b229ddb2127350b7a3c0 100644 (file)
@@ -1 +1 @@
-fbca9570fd2e1465739e4d3a8d9bb40fad594fd78ab49b2cb34efa27ebdd8361
\ No newline at end of file
+e0099464a0045a04f4ccf29bc2b8325fc8c7f39ccf4847e74818f928c9153588
\ No newline at end of file
index 0138db5183464b65d6b5baeae982473100e865c9..6f5021aafe76c6449d87a91bc95006dbe53fd699 100644 (file)
@@ -380,7 +380,7 @@ static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
 static int jsonXlateBlobToNode(JsonParse *pParse, u32 i);
 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
 static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
-static u32 jsonXlateBlobToText(JsonParse*,u32,JsonString*);
+static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*);
 
 /**************************************************************************
 ** Utility routines for dealing with JsonString objects
@@ -2558,13 +2558,12 @@ static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
   u32 nSize;
   if( pParse->nBlobAlloc>0 ) return 1;
   aOld = pParse->aBlob;
-  nSize = pParse->nBlob + pParse->nIns + nExtra;
-  if( nSize>100 ) nSize -= 100;
+  nSize = pParse->nBlob + nExtra;
   pParse->aBlob = 0;
   if( jsonBlobExpand(pParse, nSize) ){
     return 0;
   }
-  assert( pParse->nBlobAlloc >= pParse->nBlob + pParse->nIns );
+  assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra );
   memcpy(pParse->aBlob, aOld, pParse->nBlob);
   return 1;
 }
@@ -3248,7 +3247,7 @@ static void jsonReturnStringAsBlob(JsonString *pStr){
 ** payload size in to *pSz.  It returns the offset from i to the
 ** beginning of the payload.  Return 0 on error.
 */
-static u32 jsonbPayloadSize(JsonParse *pParse, u32 i, u32 *pSz){
+static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
   u8 x;
   u32 sz;
   u32 n;
@@ -3306,7 +3305,7 @@ static u32 jsonbPayloadSize(JsonParse *pParse, u32 i, u32 *pSz){
 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
 */
 static u32 jsonXlateBlobToText(
-  JsonParse *pParse,             /* the complete parse of the JSON */
+  const JsonParse *pParse,       /* the complete parse of the JSON */
   u32 i,                         /* Start rendering at this index */
   JsonString *pOut               /* Write JSON here */
 ){
@@ -3861,7 +3860,7 @@ static u32 jsonLookupBlobStep(
   u8 x;
 
   if( zPath[0]==0 ){
-    if( pParse->eEdit && jsonBlobMakeEditable(pParse, 0) ){
+    if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
       n = jsonbPayloadSize(pParse, iRoot, &sz);
       sz += n;
       if( pParse->eEdit==JEDIT_DEL ){
@@ -3939,7 +3938,7 @@ static u32 jsonLookupBlobStep(
       ix.aBlob = aLabel;
       ix.nBlobAlloc = sizeof(aLabel);
       jsonBlobAppendNodeType(&ix,JSONB_TEXTRAW, nKey);
-      if( jsonBlobMakeEditable(pParse, ix.nBlob+nKey) ){
+      if( jsonBlobMakeEditable(pParse, ix.nBlob+nKey+pParse->nIns) ){
         nIns = ix.nBlob + nKey + pParse->nIns;
         jsonBlobEdit(pParse, j, 0, 0, nIns);
         assert( pParse->nBlob + pParse->nIns <= pParse->nBlobAlloc );
@@ -3998,7 +3997,9 @@ static u32 jsonLookupBlobStep(
     }
     if( j>iEnd ) return JSON_BLOB_ERROR;
     if( k>1 ) return JSON_BLOB_NOTFOUND;
-    if( pParse->eEdit>=JEDIT_INS && jsonBlobMakeEditable(pParse, 0) ){
+    if( pParse->eEdit>=JEDIT_INS
+     && jsonBlobMakeEditable(pParse, pParse->nIns)
+    ){
       testcase( pParse->eEdit==JEDIT_INS );
       testcase( pParse->eEdit==JEDIT_SET );
       jsonBlobEdit(pParse, j, 0, pParse->aIns, pParse->nIns);
@@ -4994,7 +4995,6 @@ static JsonNode *jsonMergePatch(
 }
 
 
-#if 0
 /*
 ** Return codes for jsonMergePatchBlob()
 */
@@ -5052,7 +5052,7 @@ static JsonNode *jsonMergePatch(
 static int jsonMergePatchBlob(
   JsonParse *pTarget,      /* The JSON parser that contains the TARGET */
   u32 iTarget,             /* Index of TARGET in pTarget->aBlob[] */
-  const JsonParse *pPatch  /* The PATCH */
+  const JsonParse *pPatch, /* The PATCH */
   u32 iPatch               /* Index of PATCH in pPatch->aBlob[] */
 ){
   u8 x;             /* Type of a single node */
@@ -5061,6 +5061,7 @@ static int jsonMergePatchBlob(
   u32 iTStart;      /* First label in the target object */
   u32 iTEndBE;      /* Original first byte past end of target, before edit */
   u32 iTEnd;        /* Current first byte past end of target */
+  u8 eTLabel;       /* Node type of the target label */
   u32 iTLabel;      /* Index of the label */
   u32 nTLabel;      /* Header size in bytes for the target label */
   u32 szTLabel;     /* Size of the target label payload */
@@ -5070,6 +5071,7 @@ static int jsonMergePatchBlob(
 
   u32 iPCursor;     /* Cursor position while scanning the patch */
   u32 iPEnd;        /* First byte past the end of the patch */
+  u8 ePLabel;       /* Node type of the patch label */
   u32 iPLabel;      /* Start of patch label */
   u32 nPLabel;      /* Size of header on the patch label */
   u32 szPLabel;     /* Payload size of the patch label */
@@ -5093,9 +5095,9 @@ static int jsonMergePatchBlob(
   }
   x = pTarget->aBlob[iTarget] & 0x0f;
   if( x!=JSONB_OBJECT ){  /* Algorithm line 05 */
-    static const u8 emptyObject = { JSONB_OBJECT };
+    static const u8 emptyObject[] = { JSONB_OBJECT };
     n = jsonbPayloadSize(pTarget, iTarget, &sz);
-    jsonBlobEdit(pTarget, iTarget, szTarget, emptyObject, 1); /* Line 06 */
+    jsonBlobEdit(pTarget, iTarget, sz, emptyObject, 1); /* Line 06 */
   }
   n = jsonbPayloadSize(pPatch, iPatch, &sz);
   if( n==0 ) return JSON_MERGE_BADPATCH;
@@ -5108,8 +5110,10 @@ static int jsonMergePatchBlob(
 
   while( iPCursor<iPEnd ){  /* Algorithm line 07 */
     iPLabel = iPCursor;
-    x = pPatch->aBlob[iPCursor] & 0x0f;
-    if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_MERGE_BADPATCH;
+    ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
+    if( ePLabel<JSONB_TEXT || ePLabel>JSONB_TEXTRAW ){
+      return JSON_MERGE_BADPATCH;
+    }
     nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
     if( nPLabel==0 ) return JSON_MERGE_BADPATCH;
     iPValue = iPCursor + nPLabel + szPLabel;
@@ -5123,8 +5127,10 @@ static int jsonMergePatchBlob(
     iTEnd = iTEndBE + pTarget->delta;
     while( iTCursor<iTEnd ){
       iTLabel = iTCursor;
-      x = pTarget->aBlob[iTCursor] & 0x0f;
-      if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_MERGE_BADTARGET;
+      eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
+      if( eTLabel<JSONB_TEXT || eTLabel>JSONB_TEXTRAW ){
+        return JSON_MERGE_BADTARGET;
+      }
       nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
       if( nTLabel==0 ) return JSON_MERGE_BADTARGET;
       iTValue = iTLabel + nTLabel + szTLabel;
@@ -5133,6 +5139,7 @@ static int jsonMergePatchBlob(
       if( nTValue==0 ) return JSON_MERGE_BADTARGET;
       if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET;
       if( eTLabel==ePLabel ){
+        /* Common case */
         if( szTLabel==szPLabel
          && memcmp(&pTarget->aBlob[iTLabel+nTLabel],
                    &pPatch->aBlob[iPLabel+nPLabel], szTLabel)==0
@@ -5140,7 +5147,23 @@ static int jsonMergePatchBlob(
           break;  /* Labels match. */
         }
       }else{
-        if( jsonLabelEqual(pTarget, iTLabel, pPatch, iPLabel) ) break;
+        /* Should rarely happen */
+        JsonString s1, s2;
+        int isEqual;
+        jsonStringInit(&s1, 0);
+        jsonXlateBlobToText(pTarget, iTLabel, &s1);
+        if( s1.eErr ) return JSON_MERGE_OOM;
+        jsonStringInit(&s2, 0);
+        jsonXlateBlobToText(pPatch, iPLabel, &s2);
+        if( s2.eErr ) return JSON_MERGE_OOM;
+        if( s1.nUsed==s2.nUsed && memcmp(s1.zBuf, s2.zBuf, s1.nUsed)==0 ){
+          isEqual = 1;
+        }else{
+          isEqual = 0;
+        }
+        jsonStringReset(&s1);
+        jsonStringReset(&s2);
+        if( isEqual ) break;
       }
       iTCursor = iTValue + nTValue + szTValue;
     }
@@ -5154,22 +5177,23 @@ static int jsonMergePatchBlob(
         if( pTarget->oom ) return JSON_MERGE_OOM;
       }else{
         /* Algorithm line 12 */
-        int rc = jsonMergePatchBlob(pTarget, iTValue, pPatch, pPValue);
+        int rc = jsonMergePatchBlob(pTarget, iTValue, pPatch, iPValue);
         if( rc ) return rc;
       }        
     }else if( x>0 ){  /* Algorithm line 13 */
       /* No match and patch value is not NULL */
-      if( pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){
+      if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){  /* Line 14 */
         jsonBlobEdit(pTarget, iTEnd, 0,
-                     pPatch->aBlob+iPValue, szPValue+nPValue, 0, 0);
+                     pPatch->aBlob+iPValue, szPValue+nPValue);
         if( pTarget->oom ) return JSON_MERGE_OOM;
       }else{
+        int rc;
         u32 szNew = szPLabel+nPLabel;
         jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
         if( pTarget->oom ) return JSON_MERGE_OOM;
         memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
         pTarget->aBlob[iTEnd+szNew] = 0x00;
-        rc = jsonMergePatch(pTarget, iTEnd+szNew, pPatch, iPValue);
+        rc = jsonMergePatchBlob(pTarget, iTEnd+szNew,pPatch,iPValue);
         if( rc ) return rc;
       }
     }
@@ -5177,7 +5201,6 @@ static int jsonMergePatchBlob(
   jsonAfterEditSizeAdjust(pTarget, iTarget);
   return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK;
 }
-#endif
 
 
 /*
@@ -5195,6 +5218,43 @@ static void jsonPatchFunc(
   JsonNode *pResult;   /* The result of the merge */
 
   UNUSED_PARAMETER(argc);
+  if( jsonFuncArgMightBeBinary(argv[0])
+   && jsonFuncArgMightBeBinary(argv[1])
+  ){
+    JsonParse target, patch;
+    sqlite3 *db;
+    int rc;
+    memset(&target, 0, sizeof(target));
+    memset(&patch, 0, sizeof(patch));
+    target.aBlob = (u8*)sqlite3_value_blob(argv[0]);
+    target.nBlob = sqlite3_value_bytes(argv[0]);
+    patch.aBlob = (u8*)sqlite3_value_blob(argv[1]);
+    patch.nBlob = sqlite3_value_bytes(argv[1]);
+    db = sqlite3_context_db_handle(ctx);
+    if( db->mallocFailed ) return;
+    if( !jsonBlobMakeEditable(&target, patch.nBlob) ) return;
+    rc = jsonMergePatchBlob(&target, 0, &patch, 0);
+    if( rc ){
+      if( rc==JSON_MERGE_OOM ){
+        sqlite3_result_error_nomem(ctx);
+      }else{
+        sqlite3_result_error(ctx, "malformed JSON", -1);
+      }
+      jsonParseReset(&target);
+    }else{
+      int flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
+      if( flgs & JSON_BLOB ){
+        sqlite3_result_blob(ctx, target.aBlob, target.nBlob, SQLITE_DYNAMIC);
+      }else{
+        JsonString s;
+        jsonStringInit(&s, ctx);
+        jsonXlateBlobToText(&target, 0, &s);
+        jsonReturnString(&s);
+        jsonParseReset(&target);
+      }
+    }
+    return;
+  }
   pX = jsonParseCached(ctx, argv[0], ctx, 1);
   if( pX==0 ) return;
   assert( pX->hasMod==0 );