Skip to content

Commit ed5ae7e

Browse files
committed
feat: enhance median calculation in MySQL connector by managing session variable for group_concat_max_len
1 parent 235814e commit ed5ae7e

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

adminforth/dataConnectors/mysql.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -397,23 +397,39 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
397397
if (groupExpr) query += ` GROUP BY ${groupExpr} ORDER BY ${groupExpr} ASC`;
398398

399399
if (medianAliases.length > 0) {
400-
await this.client.execute("SET SESSION group_concat_max_len = 1000000;");
401-
}
402-
403-
const [rows]: any = await this.client.execute(query, filterValues);
404-
405-
if (medianAliases.length > 0) {
406-
return rows.map(row => {
407-
medianAliases.forEach(alias => {
408-
const raw = row[alias];
409-
const nums = raw ? raw.split(',').map(Number).filter(n => !isNaN(n)) : [];
410-
row[alias] = this.calculateMedian(nums);
400+
let connection;
401+
let originalMaxLen = null;
402+
try {
403+
connection = await this.client.getConnection();
404+
405+
const [originalLenRows]: any = await connection.execute("SELECT @@SESSION.group_concat_max_len as original_len");
406+
originalMaxLen = originalLenRows[0].original_len;
407+
408+
await connection.execute("SET SESSION group_concat_max_len = 1000000");
409+
410+
const [rows]: any = await connection.execute(query, filterValues);
411+
412+
return rows.map((row: any) => {
413+
medianAliases.forEach(alias => {
414+
const raw = row[alias];
415+
const nums = raw ? raw.split(',').map(Number).filter((n: number) => !isNaN(n)) : [];
416+
row[alias] = this.calculateMedian(nums);
417+
});
418+
return row;
411419
});
412-
return row;
413-
});
414-
}
415420

416-
return rows;
421+
} finally {
422+
if (connection) {
423+
if (originalMaxLen !== null) {
424+
await connection.execute(`SET SESSION group_concat_max_len = ${originalMaxLen}`);
425+
}
426+
connection.release();
427+
}
428+
}
429+
} else {
430+
const [rows]: any = await this.client.execute(query, filterValues);
431+
return rows;
432+
}
417433
}
418434

419435
async getDataWithOriginalTypes({ resource, limit, offset, sort, filters }): Promise<any[]> {

0 commit comments

Comments
 (0)