fix: 🐛 max depth error for active sessions filter in targets (#3004)

* fix: 🐛 max depth error for active sessions filter in targets

* refactor: 💡 revert route and convert operators in parser

* refactor: 💡 add expr limit

* refactor: 💡 add parameters to IN/NOT IN conditions

* test: 💍 fix tests

* docs: ✏️ add comment
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2025-09-17 15:32:39 -07:00
1 parent d2616acf36
commit d0924f7e6d
2 files changed
+33 -3

No files matched your search

+30
View File
@@ -75,6 +75,34 @@ function addFilterConditions({ filters, parameters, conditions }) {
continue;
}
// When attribute conditions are a series of equals/notEquals
// replace with in/notIn to avoid hitting the sqlite maximum
// expression tree depth.
const firstOperator = Object.keys(filterValueArray[0])[0];
const allOperatorsEqual = filterValueArray
.flatMap((item) => Object.keys(item))
.every((op) => op === firstOperator);
if (
filterValueArray.length > 1 &&
(firstOperator === 'equals' || firstOperator === 'notEquals') &&
allOperatorsEqual
) {
const operation = firstOperator === 'equals' ? 'in' : 'notIn';
const values = filterValueArray
.filter((f) => f)
.map((filterObjValue) => {
let value = Object.values(filterObjValue)[0];
if (typeOf(value) === 'date') {
value = value.toISOString();
}
parameters.push(value);
return value;
});
const filterCondition = `${key}${OPERATORS[operation](values)}`;
conditions.push(parenthetical(filterCondition));
continue;
}
const filterConditions = filterValueArray
.filter((f) => f)
.map((filterObjValue) => {
@@ -195,6 +223,8 @@ const OPERATORS = {
lt: ' < ?',
lte: ' <= ?',
contains: ' LIKE ?',
in: (values) => ` IN (${values.map(() => '?').join(', ')})`,
notIn: (values) => ` NOT IN (${values.map(() => '?').join(', ')})`,
};
// Logical Operators
@@ -150,7 +150,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
},
},
expectedWhereClause:
'WHERE (id != ? AND id != ?) AND (status = ? OR status = ?) AND (type = ?)',
'WHERE (id NOT IN (?, ?)) AND (status IN (?, ?)) AND (type = ?)',
expectedParams: ['id1', 'id2', 'active', 'pending', 'ssh'],
},
},
@@ -271,7 +271,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
sql,
`
SELECT * FROM "target"
WHERE (id != ? AND id != ?) AND (status = ? OR status = ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
WHERE (id NOT IN (?, ?)) AND (status IN (?, ?)) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
);
assert.deepEqual(parameters, [
@@ -307,7 +307,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
sql,
`
SELECT data FROM "target"
WHERE (type = ?) AND (status = ? OR status = ?) AND (created_time >= ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
WHERE (type = ?) AND (status IN (?, ?)) AND (created_time >= ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
ORDER BY name COLLATE NOCASE DESC, name DESC
LIMIT ? OFFSET ?`.removeExtraWhiteSpace(),
);