Optimize JSON patch implementation with native SQLite function#42
Optimize JSON patch implementation with native SQLite function#42
Conversation
- Added `checkJsonPatchSupport` to `WasmDatabaseEngine` to detect native `json_patch` availability. - Optimized `updateCell` to use `json_patch` SQL function when available. - Optimized `updateCellBatch` to use `json_patch` SQL function for batch updates when available. - Ensured JS fallback is used when native function is unavailable. - Handled object inputs for patches by stringifying them before binding. - Added comprehensive unit tests in `tests/unit/json_patch_optimization.test.ts`. Co-authored-by: zknpr <96851588+zknpr@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Summary of ChangesHello @zknpr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant performance enhancement to the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant performance optimization by leveraging SQLite's native json_patch function where available, with a sensible fallback to the existing JavaScript implementation. The changes are well-structured and cover both single and batch cell updates. The addition of unit tests is also a great step towards ensuring the new logic is correct. I've identified a couple of areas where type safety could be improved and the new tests could be made more robust. Overall, this is a solid improvement.
| } | ||
| if (this.checkJsonPatchSupport()) { | ||
| // Use native json_patch | ||
| const patchStr = typeof patch === 'string' ? patch : JSON.stringify(patch); |
There was a problem hiding this comment.
The patch parameter is typed as string | undefined in the method signature, but this line handles it as if it could also be an object. This type discrepancy is also reflected in the new regression test, which uses as any to pass an object.
This can lead to type safety issues. To make the code more robust and self-documenting, the method signature for updateCell and the DatabaseOperations interface should be updated to patch?: string | object. Since the interface is in a file not modified in this PR, I'd recommend at least adding a code comment here to explain the lenient type handling.
| const stmt = this.instance.prepare(sql); | ||
| try { | ||
| for (const update of columnUpdates) { | ||
| const patchStr = typeof update.value === 'string' ? update.value : JSON.stringify(update.value); |
There was a problem hiding this comment.
Similar to updateCell, there's a type discrepancy here. This line handles update.value as if it could be an object for json_patch operations, but its type is CellValue from the CellUpdate interface, which doesn't include objects. The new regression test for batch updates also uses as any to pass an object.
This can compromise type safety. The CellUpdate interface in types.ts should ideally be updated to allow value to be an object when operation is json_patch. Since types.ts is not in this PR, please consider adding a comment to clarify this behavior or addressing the type definition in a follow-up PR.
| const originalPrepare = engine.instance.prepare.bind(engine.instance); | ||
| engine.instance.prepare = (sql: string, params: any[]) => { | ||
| return originalPrepare(sql, params); | ||
| }; |
There was a problem hiding this comment.
| it('should use json_patch when updating batch cells with object value (regression test)', async () => { | ||
| // Reset data | ||
| await engine.executeQuery("UPDATE data SET content = ? WHERE id = 2", [JSON.stringify({ a: 10, b: 20 })]); | ||
|
|
||
| const updates = [ | ||
| { rowId: 2, column: 'content', value: { b: 3000, c: 4000 } as any, operation: 'json_patch' } | ||
| ]; | ||
|
|
||
| await engine.updateCellBatch('data', updates); | ||
|
|
||
| // Verify result | ||
| const result = await engine.executeQuery("SELECT content FROM data WHERE id = 2"); | ||
| const content = JSON.parse(result[0].rows[0][0]); | ||
| assert.deepStrictEqual(content, { a: 10, b: 3000, c: 4000 }); | ||
| }); |
There was a problem hiding this comment.
This regression test correctly verifies that the update is applied successfully when an object is passed as a patch value. However, it doesn't check if the optimized json_patch native SQL function was actually used.
To make this test more robust, I suggest adding a spy on engine.instance.prepare to assert that the generated SQL contains json_patch, similar to the should use json_patch when updating batch cells test.
it('should use json_patch when updating batch cells with object value (regression test)', async () => {
// Reset data
await engine.executeQuery("UPDATE data SET content = ? WHERE id = 2", [JSON.stringify({ a: 10, b: 20 })]);
const originalPrepare = engine.instance.prepare.bind(engine.instance);
const preparedStatements: string[] = [];
engine.instance.prepare = (sql: string, params: any[]) => {
preparedStatements.push(sql);
return originalPrepare(sql, params);
};
const updates = [
{ rowId: 2, column: 'content', value: { b: 3000, c: 4000 } as any, operation: 'json_patch' }
];
await engine.updateCellBatch('data', updates);
// Verify result
const result = await engine.executeQuery("SELECT content FROM data WHERE id = 2");
const content = JSON.parse(result[0].rows[0][0]);
assert.deepStrictEqual(content, { a: 10, b: 3000, c: 4000 });
// Check if json_patch was used
const usesJsonPatch = preparedStatements.some(q => q.toLowerCase().includes('json_patch'));
assert.ok(usesJsonPatch, 'Should use json_patch SQL function for batch update with object value');
engine.instance.prepare = originalPrepare;
});|
Merged as part of v1.3.0 release in PR #65 |
Understood. Acknowledging that this is merged. |
This PR optimizes the JSON patch implementation in
WasmDatabaseEngineby leveraging the nativejson_patchfunction provided by SQLite (if available). This significantly improves performance by avoiding the read-modify-write cycle in JavaScript. It also maintains a fallback to the existing JavaScript implementation for environments withoutjson_patchsupport. The changes cover both single cell updates (updateCell) and batch updates (updateCellBatch), and include robust handling for object inputs.PR created automatically by Jules for task 12088301536056168967 started by @zknpr