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

fix(Microsoft SQL Node): Prevent MSSQL max parameters error by chunking #8390

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/nodes-base/nodes/Microsoft/Sql/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,34 @@ const escapeTableName = (table: string) => {
}
};

const MSSQL_PARAMETER_LIMIT = 2100;

export function mssqlChunk(rows: IDataObject[]): IDataObject[][] {
const chunked: IDataObject[][] = [[]];
let currentParamCount = 0;

for (const row of rows) {
const rowValues = Object.values(row);
const valueCount = rowValues.length;

if (currentParamCount + valueCount >= MSSQL_PARAMETER_LIMIT) {
chunked.push([]);
currentParamCount = 0;
}

chunked[chunked.length - 1].push(row);

currentParamCount += valueCount;
}

return chunked;
}

export async function insertOperation(tables: ITables, pool: mssql.ConnectionPool) {
return await executeQueryQueue(
tables,
({ table, columnString, items }: OperationInputData): Array<Promise<object>> => {
return chunk(items, 1000).map(async (insertValues) => {
return mssqlChunk(items).map(async (insertValues) => {
const request = pool.request();

const valuesPlaceholder = [];
Expand Down
12 changes: 12 additions & 0 deletions packages/nodes-base/nodes/Microsoft/Sql/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
configurePool,
deleteOperation,
insertOperation,
mssqlChunk,
updateOperation,
} from '../GenericFunctions';

Expand Down Expand Up @@ -142,4 +143,15 @@ describe('MSSQL tests', () => {
expect(querySpy).toHaveBeenCalledWith('DELETE FROM [users] WHERE [id] IN (@v0);');
assertParameters({ v0: 2 });
});

describe('mssqlChunk', () => {
it('should chunk insert values correctly', () => {
const chunks = mssqlChunk(
new Array(3000)
.fill(null)
.map((_, index) => ({ id: index, name: 'John Doe', verified: true })),
);
expect(chunks.map((chunk) => chunk.length)).toEqual([699, 699, 699, 699, 204]);
});
});
});
Loading