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

Reset row counters when creating Query using templates #2704

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

shrima-cf
Copy link
Contributor

Reset the row counters when we create Query, so we calculate rowsRead and rowsWritten correctly.

@shrima-cf shrima-cf requested a review from kentonv September 12, 2024 22:16
@shrima-cf shrima-cf marked this pull request as ready for review September 12, 2024 22:16
@shrima-cf shrima-cf requested review from a team as code owners September 12, 2024 22:16
@@ -509,7 +509,9 @@ KJ_TEST("SQLite read row counters (basic)") {
constexpr int dbRowCount = 1000;
auto insertStmt = db.prepare("INSERT INTO things (id, unindexed_int, value) VALUES (?, ?, ?)");
for (int i = 0; i < dbRowCount; i++) {
insertStmt.run(i, i * 1000, kj::str("value", i));
auto query = insertStmt.run(i, i * 1000, kj::str("value", i));
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I can tell, this new test code only tests the Query(SqliteDatabase& db, const Regulator& regulator, Statement& statement, Params&&... bindings) (on line 404 in sqlite.h).

You've also changed Query(SqliteDatabase& db, const Regulator& regulator, kj::StringPtr sqlCode, Params&&... bindings) (from line 412). This version of the constructor is called by countRowsTouched. How come the test was passing before adding the code? I think it's because countRowsTouched makes a new implicit statement on every call, rather than reusing the statement.

Is there a test we can add that tickles the problem and is fixed by adding the resetRowCounters(); statement?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at this a bit more... why do we have the resetRowCounters() in Query(SqliteDatabase& db, const Regulator& regulator, kj::StringPtr sqlCode, Params&&... bindings) (from line 412)? We're creating a new statement from the SQL code.

Copy link
Member

Choose a reason for hiding this comment

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

The kj::StringPtr sqlCode version always prepares a new statement from scratch hence its counts always start from zero anyway, so there was no bug in that version of the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need the reset counters in the constructor which creates the statement, only for the one that takes in a statement. I updated the code accordingly

@justin-mp
Copy link
Contributor

Also, it's good practice to create an internal PR that updates workerd to make sure that no internal tests fail.

@@ -413,6 +414,7 @@ class SqliteDatabase::Query final: private ResetListener {
regulator(regulator),
ownStatement(db.prepareSql(regulator, sqlCode, 0, MULTI)),
maybeStatement(ownStatement) {
resetRowCounters();
Copy link
Member

Choose a reason for hiding this comment

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

This line isn't needed because in this constructor the statement is always newly-constructed so the counts will already be zero.

Only the other constructor, which takes a Statement& that may have been used before, needs to reset the counters.

@shrima-cf
Copy link
Contributor Author

Also, it's good practice to create an internal PR that updates workerd to make sure that no internal tests fail.

Yep! I had some git permissions issues so couldn't post that PR, but will send out the edgeworker PR with the deps update

@shrima-cf shrima-cf force-pushed the shrima/fix-resetCounters-sqlite branch from 6fe5471 to 3756650 Compare September 16, 2024 22:50
@shrima-cf shrima-cf merged commit e32364c into main Sep 16, 2024
12 of 13 checks passed
@shrima-cf shrima-cf deleted the shrima/fix-resetCounters-sqlite branch September 16, 2024 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants