A local-first personal finance app
[AI] Omit empty WHERE clauses from AQL instead of emitting WHERE 1 (#8678)
Matiss Janis Aboltins690313a95e0f
diff --git a/packages/loot-core/src/server/aql/compiler.test.ts b/packages/loot-core/src/server/aql/compiler.test.tsindex 5f74cb31a4..22fe38ac95 100644--- a/packages/loot-core/src/server/aql/compiler.test.ts+++ b/packages/loot-core/src/server/aql/compiler.test.ts@@ -118,7 +118,6 @@ describe('sheet language', () => {SELECT transactions1.id AS trans1, transactions2.id AS trans2, accounts.id AS id FROM accountsLEFT JOIN transactions transactions1 ON transactions1.id = accounts.trans1LEFT JOIN transactions transactions2 ON transactions2.id = accounts.trans2- WHERE 1`),);});@@ -172,7 +171,6 @@ describe('sheet language', () => {LEFT JOIN transactions transactions1 ON transactions1.id = accounts.trans1LEFT JOIN transactions transactions2 ON transactions2.id = accounts.trans2LEFT JOIN transactions transactions3 ON transactions3.id = accounts.trans3- WHERE 1`),);@@ -188,7 +186,6 @@ describe('sheet language', () => {LEFT JOIN transactions transactions2 ON transactions2.id = accounts.trans2LEFT JOIN transactions transactions3 ON transactions3.id = accounts.trans3LEFT JOIN payees payees4 ON payees4.id = transactions1.payee- WHERE 1`),);});@@ -210,7 +207,7 @@ describe('sheet language', () => {expect(sqlLines(result.sql)).toEqual(sqlLines(`SELECT transactions.amount AS amount, transactions.id AS id FROM transactions- WHERE 1 AND transactions.tombstone = 0+ WHERE transactions.tombstone = 0`),);@@ -222,7 +219,6 @@ describe('sheet language', () => {expect(sqlLines(result.sql)).toEqual(sqlLines(`SELECT transactions.amount AS amount, transactions.id AS id FROM transactions- WHERE 1`),);@@ -236,7 +232,6 @@ describe('sheet language', () => {SELECT transactions1.amount AS "trans.amount", payees2.name AS "trans.payee.name", accounts.id AS id FROM accountsLEFT JOIN transactions transactions1 ON transactions1.id = accounts.trans AND transactions1.tombstone = 0LEFT JOIN payees payees2 ON payees2.id = transactions1.payee AND payees2.tombstone = 0- WHERE 1`),);@@ -373,7 +368,6 @@ describe('sheet language', () => {sqlLines(`SELECT transactions.id AS id FROM transactionsLEFT JOIN payees payees1 ON payees1.id = transactions.payee- WHERE 1GROUP BY payees1.name`),);@@ -398,7 +392,6 @@ describe('sheet language', () => {sqlLines(`SELECT transactions.id AS id FROM transactionsLEFT JOIN payees payees1 ON payees1.id = transactions.payee- WHERE 1ORDER BY payees1.name`),);@@ -558,6 +551,30 @@ describe('sheet language', () => {);});+ it('ignores an empty $or filter', () => {+ const result = generateSQLWithState(+ q('transactions').filter({ $or: [] }).select(['id']).serialize(),+ basicSchema,+ );+ expect(sqlLines(result.sql)).toEqual(+ sqlLines(`+ SELECT transactions.id AS id FROM transactions+ `),+ );+ });++ it('ignores a null $or filter', () => {+ const result = generateSQLWithState(+ q('transactions').filter({ $or: null }).select(['id']).serialize(),+ basicSchema,+ );+ expect(sqlLines(result.sql)).toEqual(+ sqlLines(`+ SELECT transactions.id AS id FROM transactions+ `),+ );+ });+it('allows functions in `filter`', () => {// Allows transforming the inputlet result = generateSQLWithState(@@ -675,7 +692,7 @@ describe('sheet language', () => {expect(sqlLines(result.sql)).toEqual(sqlLines(`SELECT v_transactions.amount AS amount, v_transactions.id AS id FROM v_transactions- WHERE 1 AND (v_transactions.amount > 0)+ WHERE (v_transactions.amount > 0)`),);@@ -699,7 +716,6 @@ describe('sheet language', () => {sqlLines(`SELECT transactions1.amount AS "trans1.amount", accounts.id AS id FROM accountsLEFT JOIN v_transactions transactions1 ON transactions1.id = accounts.trans1 AND (transactions1.amount > 0)- WHERE 1`),);@@ -730,7 +746,6 @@ describe('sheet language', () => {expect(sqlLines(result.sql)).toEqual(sqlLines(`SELECT v_transactions.amount AS amount, v_transactions.id AS id FROM v_transactions- WHERE 1`),);});diff --git a/packages/loot-core/src/server/aql/compiler.ts b/packages/loot-core/src/server/aql/compiler.tsindex 2d5c27676a..bbdbf059a7 100644--- a/packages/loot-core/src/server/aql/compiler.ts+++ b/packages/loot-core/src/server/aql/compiler.ts@@ -25,7 +25,16 @@ function dateToInt(date) {function addTombstone(schema, tableName, tableId, whereStr) {const hasTombstone = schema[tableName].tombstone != null;- return hasTombstone ? `${whereStr} AND ${tableId}.tombstone = 0` : whereStr;+ if (!hasTombstone) {+ return whereStr;+ }+ return whereStr+ ? `${whereStr} AND ${tableId}.tombstone = 0`+ : `WHERE ${tableId}.tombstone = 0`;+}++export function appendWhere(whereStr: string, condition: string) {+ return whereStr ? `${whereStr} AND ${condition}` : `WHERE ${condition}`;}function popPath(path) {@@ -811,7 +820,16 @@ function compileAnd(state, conds) {}const compileWhere = saveStack('filter', (state, conds) => {- return compileAnd(state, conds);+ // Unlike compileAnd, an empty top-level filter omits the WHERE clause+ // entirely instead of emitting the always-true `1` placeholder.+ if (!conds) {+ return null;+ }+ const res = compileConditions(state, conds);+ if (res.length === 0) {+ return null;+ }+ return '(' + res.join('\n AND ') + ')';});function compileJoins(state, tableRef, internalTableFilters) {@@ -1132,17 +1150,18 @@ export function compileQuery(orderExpressions,);+ where = '';if (filterExpressions.length > 0) {const result = compileWhere(state, filterExpressions);- where = 'WHERE ' + result;- } else {- where = 'WHERE 1';+ if (result) {+ where = 'WHERE ' + result;+ }}if (!rawMode) {const filters = internalTableFilters(tableName);if (filters.length > 0) {- where += ' AND ' + compileAnd(state, filters);+ where = appendWhere(where, compileAnd(state, filters));}}diff --git a/packages/loot-core/src/server/aql/schema/executors.ts b/packages/loot-core/src/server/aql/schema/executors.tsindex f2dae017a7..b4c2883ef8 100644--- a/packages/loot-core/src/server/aql/schema/executors.ts+++ b/packages/loot-core/src/server/aql/schema/executors.ts@@ -1,6 +1,6 @@// @ts-strict-ignore-import { isAggregateQuery } from '#server/aql/compiler';+import { appendWhere, isAggregateQuery } from '#server/aql/compiler';import type {CompilerState,OutputTypes,@@ -113,7 +113,7 @@ async function execTransactionsGrouped(const s = { ...sqlPieces };// Modify the where to only include non-parents- s.where = `${s.where} AND ${s.from}.is_parent = 0`;+ s.where = appendWhere(s.where, `${s.from}.is_parent = 0`);// We also want to exclude deleted transactions. Normally we// handle this manually down below, but now that we are doing a@@ -138,7 +138,7 @@ async function execTransactionsGrouped(SELECT ${sqlPieces.from}.id as group_idFROM ${sqlPieces.from}${sqlPieces.joins}- ${sqlPieces.where} AND is_child = 0 ${whereDead}+ ${appendWhere(sqlPieces.where, `is_child = 0 ${whereDead}`)}${sqlPieces.orderBy}${sqlPieces.limit != null ? `LIMIT ${sqlPieces.limit}` : ''}${sqlPieces.offset != null ? `OFFSET ${sqlPieces.offset}` : ''}@@ -156,7 +156,7 @@ async function execTransactionsGrouped(FROM ${sqlPieces.from}LEFT JOIN transactions _t2 ON ${sqlPieces.from}.is_child = 1 AND _t2.id = ${sqlPieces.from}.parent_id${sqlPieces.joins}- ${sqlPieces.where} AND ${sqlPieces.from}.tombstone = 0 AND IFNULL(_t2.tombstone, 0) = 0+ ${appendWhere(sqlPieces.where, `${sqlPieces.from}.tombstone = 0 AND IFNULL(_t2.tombstone, 0) = 0`)})GROUP BY group_id)@@ -237,9 +237,9 @@ async function execTransactionsBasic(if (splitType !== 'all') {if (splitType === 'none') {- s.where = `${s.where} AND ${s.from}.parent_id IS NULL`;+ s.where = appendWhere(s.where, `${s.from}.parent_id IS NULL`);} else {- s.where = `${s.where} AND ${s.from}.is_parent = 0`;+ s.where = appendWhere(s.where, `${s.from}.is_parent = 0`);}}diff --git a/upcoming-release-notes/aql-omit-empty-where.md b/upcoming-release-notes/aql-omit-empty-where.mdnew file mode 100644index 0000000000..8e0e7892b3--- /dev/null+++ b/upcoming-release-notes/aql-omit-empty-where.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [MatissJanis]+---++Omit empty WHERE clauses from AQL instead of emitting a tautology placeholder