Skip to content

Commit d0924f7

Browse files
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
1 parent d2616ac commit d0924f7

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

β€Žaddons/api/addon/utils/sqlite-query.jsβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ function addFilterConditions({ filters, parameters, conditions }) {
7575
continue;
7676
}
7777

78+
// When attribute conditions are a series of equals/notEquals
79+
// replace with in/notIn to avoid hitting the sqlite maximum
80+
// expression tree depth.
81+
const firstOperator = Object.keys(filterValueArray[0])[0];
82+
const allOperatorsEqual = filterValueArray
83+
.flatMap((item) => Object.keys(item))
84+
.every((op) => op === firstOperator);
85+
if (
86+
filterValueArray.length > 1 &&
87+
(firstOperator === 'equals' || firstOperator === 'notEquals') &&
88+
allOperatorsEqual
89+
) {
90+
const operation = firstOperator === 'equals' ? 'in' : 'notIn';
91+
const values = filterValueArray
92+
.filter((f) => f)
93+
.map((filterObjValue) => {
94+
let value = Object.values(filterObjValue)[0];
95+
if (typeOf(value) === 'date') {
96+
value = value.toISOString();
97+
}
98+
parameters.push(value);
99+
return value;
100+
});
101+
const filterCondition = `${key}${OPERATORS[operation](values)}`;
102+
conditions.push(parenthetical(filterCondition));
103+
continue;
104+
}
105+
78106
const filterConditions = filterValueArray
79107
.filter((f) => f)
80108
.map((filterObjValue) => {
@@ -195,6 +223,8 @@ const OPERATORS = {
195223
lt: ' < ?',
196224
lte: ' <= ?',
197225
contains: ' LIKE ?',
226+
in: (values) => ` IN (${values.map(() => '?').join(', ')})`,
227+
notIn: (values) => ` NOT IN (${values.map(() => '?').join(', ')})`,
198228
};
199229

200230
// Logical Operators

β€Žaddons/api/tests/unit/utils/sqlite-query-test.jsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
150150
},
151151
},
152152
expectedWhereClause:
153-
'WHERE (id != ? AND id != ?) AND (status = ? OR status = ?) AND (type = ?)',
153+
'WHERE (id NOT IN (?, ?)) AND (status IN (?, ?)) AND (type = ?)',
154154
expectedParams: ['id1', 'id2', 'active', 'pending', 'ssh'],
155155
},
156156
},
@@ -271,7 +271,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
271271
sql,
272272
`
273273
SELECT * FROM "target"
274-
WHERE (id != ? AND id != ?) AND (status = ? OR status = ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
274+
WHERE (id NOT IN (?, ?)) AND (status IN (?, ?)) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
275275
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
276276
);
277277
assert.deepEqual(parameters, [
@@ -307,7 +307,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
307307
sql,
308308
`
309309
SELECT data FROM "target"
310-
WHERE (type = ?) AND (status = ? OR status = ?) AND (created_time >= ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
310+
WHERE (type = ?) AND (status IN (?, ?)) AND (created_time >= ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
311311
ORDER BY name COLLATE NOCASE DESC, name DESC
312312
LIMIT ? OFFSET ?`.removeExtraWhiteSpace(),
313313
);

0 commit comments

Comments
Β (0)