Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add static parser #3365

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

thomasgauvin
Copy link

@thomasgauvin thomasgauvin commented Jan 29, 2025

I'm hoping to help push for getting the interpreting parser available (this makes it possible to use mysql2 as an alternative to mysql on Cloudflare Workers).

I opened this as a new PR because I have some bandwidth to address further comments in the PR if they come up. This code merely applies the recommended fixes as noted by @shiyuhang0 to the original PR by @sidorares #2099 (the original commit history is maintained and the branch was rebased onto master).

This could also be created as a PR to deoptimise-large-rows branch, however the maintainers recommend moving forward.

Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 15.82734% with 117 lines in your changes missing coverage. Please review.

Project coverage is 87.68%. Comparing base (f2cc820) to head (fdca5d7).

Files with missing lines Patch % Lines
lib/parsers/static_text_parser.js 11.45% 116 Missing ⚠️
lib/commands/query.js 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3365      +/-   ##
==========================================
- Coverage   88.45%   87.68%   -0.77%     
==========================================
  Files          83       84       +1     
  Lines       13067    13205     +138     
  Branches     1393     1396       +3     
==========================================
+ Hits        11558    11579      +21     
- Misses       1509     1626     +117     
Flag Coverage Δ
compression-0 87.68% <15.82%> (-0.77%) ⬇️
compression-1 87.68% <15.82%> (-0.77%) ⬇️
tls-0 87.08% <15.82%> (-0.76%) ⬇️
tls-1 87.45% <15.82%> (-0.77%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wellwelwel
Copy link
Collaborator

wellwelwel commented Jan 29, 2025

Thanks, @thomasgauvin! And sorry, january was a hectic month and I didn't really pay attention to your comment in #2289 (comment).

lib/parsers/static_text_parser.js 11.26% 126 Missing ⚠️

I'll add the label needs tests, but I don't intend to block this PR progress because of this. Probably tomorrow I'll go through the tests that currently use evaluation and adapt them for the new parser.

Perhaps a simple approach with parameterized tests can make things easier to avoid duplicate tests.

Copy link
Collaborator

@wellwelwel wellwelwel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasgauvin, I added some security points that were resolved after PR #2099 was created.

If it's not a problem, can I commit directly to your branch (including the typeCast / nestTables question and to add the tests I mentioned yesterday)?

lib/connection_config.js Outdated Show resolved Hide resolved
lib/parsers/static_text_parser.js Outdated Show resolved Hide resolved
}
if (options.rowsAsArray) {
result.push(value);
} else if (options.nestTables) {
Copy link
Collaborator

@wellwelwel wellwelwel Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the dynamic object properties, I believe it's beneficial to bring #2702 changes here.

There is a PoC from Snyk (Prototype Pollution): https://security.snyk.io/vuln/SNYK-JS-MYSQL2-6861580.

@wellwelwel
Copy link
Collaborator

Also, in #2289 it was decided to use disableEval instead of useStaticParser (I'm happy with both, to be honest):

@wellwelwel wellwelwel changed the title Add "interpreting" parser v2 feat: add static parser Jan 30, 2025
@wellwelwel wellwelwel changed the title feat: add static parser feat(query): add text static parser Jan 30, 2025
@wellwelwel wellwelwel changed the title feat(query): add text static parser feat(query): add static parser Jan 30, 2025
@thomasgauvin
Copy link
Author

@thomasgauvin, I added some security points that were resolved after PR #2099 was created.

If it's not a problem, can I commit directly to your branch (including the typeCast / nestTables question and to add the tests I mentioned yesterday)?

Yes, please feel free to commit anything to the branch directly. I've added you as collaborator on the fork so you should have commit access.

@thomasgauvin
Copy link
Author

Also, in #2289 it was decided to use disableEval instead of useStaticParser (I'm happy with both, to be honest):

Missed that, will adjust

thomasgauvin and others added 2 commits January 30, 2025 15:44
@wellwelwel
Copy link
Collaborator

wellwelwel commented Jan 31, 2025

🔬 Testing Updates

Note

I'll be updating this comment as I progress. Feel free to help with the debugging process.


assert(Buffer.isBuffer(res[0].foo));

  • Expect for a Buffer, but is returning a literal string "foobar".

assert.equal(rows2[0].nested_test_id, 1);

  • Expect for 1, but is returning undefined.

assert.equal(rows2[0].nested_test_title, 'test');

  • Expect for "test", but is returning undefined.

  • Expect for [ { nested_test: { id: 1, title: 'test' } } ], but is returning [ { nested_test_id: 1, nested_test_title: 'test' } ].

const [result] = await connection.query(
`SELECT CAST('{"test": true}' AS JSON) AS json_result`,
);

  • The connection is never closed when executing this query. To avoid blocking the CI, it's being skipped for now.

@wellwelwel
Copy link
Collaborator

wellwelwel commented Jan 31, 2025

@sidorares, as the query (and eventually execute) method is one of the most critical in the entire project, I chose not to create an exclusive test for the static parser, but rather to test the entire test suite against this option, including all Node.js, Bun, and Deno versions.

@thomasgauvin, I won't be committing until Saturday. Feel free to continue or wait 🙋🏻‍♂️

  • For the reasons in my comment above, naturally all CI using this option will fail at the moment.
  • I'll wait to add this variation to the coverage CI when all the tests have passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants