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: use bind params in bulkInsertQuery #17131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Americas
Copy link
Contributor

@Americas Americas commented Feb 28, 2024

Description of Changes

When using bulk create, the generated SQL does not use bindings. This creates an issue, since the data is logged along with the running query.

With this PR, the default generated SQL for a bulk insert will use bindings by default, unless bindParams is false.

This is a followup on #17128, I still hope we can get that one on the v6 branch, since it will still be a while for v7 to be usable. @ephys

List of Breaking Changes

  • internal breaking change, abstract QueryGenerator.bulkInsertQuery() now returns an object with 2 properties: query (always returned) and bind (only if bind params are used). This follows the same pattern that is used in other methods from this class, like InsertQuery.

@Americas Americas marked this pull request as ready for review February 28, 2024 11:14
@Americas Americas requested a review from a team as a code owner February 28, 2024 11:14
@Americas Americas force-pushed the main branch 7 times, most recently from 3771ada to 3c5194c Compare March 5, 2024 08:25
@Americas Americas force-pushed the main branch 5 times, most recently from a2dc36d to 5c09897 Compare March 12, 2024 14:31
Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

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

Small change request but otherwise this looks good :)

Thank you for implementing this in v7

@@ -40,6 +40,7 @@ type InsertOptions = ParameterOptions &

type BulkInsertOptions = ParameterOptions & {
hasTrigger?: boolean;
bindParam?: false | ((value: unknown) => string);
Copy link
Member

Choose a reason for hiding this comment

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

bindParam is a legacy option. Could you add support for parameterStyle instead?

enum ParameterStyle {
  bind = 'bind',
  replacement = 'replacement',
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Judging from the comment on my previous PR, I hope I did it right, but please explain if I did not get it right.

@@ -93,13 +94,13 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
valueHash: object,
columnDefinitions?: { [columnName: string]: NormalizedAttributeOptions },
options?: InsertOptions,
): { query: string; bind?: unknown[] };
): { query: string; bind?: Record<string, unknown> };
Copy link
Member

Choose a reason for hiding this comment

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

could you move { query, bind } to its own reusable type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@Americas Americas force-pushed the main branch 6 times, most recently from 7693d58 to c951e6e Compare March 19, 2024 09:56
@ephys ephys self-requested a review March 19, 2024 10:43
@Americas Americas force-pushed the main branch 5 times, most recently from b580d53 to 1b7781d Compare March 25, 2024 11:54
@lohart13
Copy link
Contributor

I have a question on this, all the dialects have varying numbers of allowed parameters with MSSQL having the least at 2100.
Are we just relying on the user to split up their calls to bulkInsert so that they are within the dialects supported number of allowed parameters?

The only reason for asking is that I initially was going to include this change in PR #16988 however I opted not to due to this potential issue.

@Americas
Copy link
Contributor Author

While I do understand this problem has a higher chance of happening in a bulk insert, the same is true in any other query that uses bind params. It certainly seems important to deal with this, or at least make sure the error is understandable.

To note, this PR does not add bind params to mssql, both mysql and postgres seem to handle around 65k, not sure how likely it is to hit that.

This PR also still allows to use replacement instead of bind. If this is truly a problem, I can make the default behavior be replacement instead of bind.

@ephys
Copy link
Member

ephys commented Mar 26, 2024

Note that there will eventually be a global option to configure the defaults, but the exact API is still TBD, so even if we pick replacements by default, you could configure your Sequelize instance to use bind for this query

@sequelize-bot sequelize-bot bot added the conflicted This PR has merge conflicts and will not be present in the list of PRs to review label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicted This PR has merge conflicts and will not be present in the list of PRs to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants