-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(search): search on aggregated results in HAVING clause (#524)
Although this use case is probably not really common or makes much sense, there were some failing `cds` tests which should be fixed by this. --- If the query contains a `group by`, the search is performed on the queries columns. If one of those columns is an aggregate function, we must put the search term in the `HAVING` clause instead of `WHERE`. Since the logical processing order of SQL queries is `FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY` the aggregated results are not accessible in the `WHERE` clause. This led to an error: ```sql > Uncaught SqliteError: misuse of aggregate: COUNT() in: SELECT COUNT(Books.stock) AS foo FROM sap_capire_bookshop_Books AS Books WHERE (IFNULL(INSTR(LOWER(COUNT(stock)), LOWER(?)), 0)) GROUP BY Books.title ``` With this change, the searchable columns of the query are checked against the aggregate functions of the ANSI SQL standard. If one is found and the query contains a `group by` (which should then always be the case), the search expression is put into the `HAVING` clause. --------- Co-authored-by: I543501 <[email protected]>
- Loading branch information
1 parent
99a1170
commit 61d348e
Showing
5 changed files
with
100 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require('./search.test') | ||
require('./read.test') | ||
require('./insert.test') | ||
require('./delete.test') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const cds = require('../../cds.js') | ||
const bookshop = require('path').resolve(__dirname, '../../bookshop') | ||
|
||
describe('Bookshop - Search', () => { | ||
const { expect } = cds.test(bookshop) | ||
|
||
// search expression operating on aggregated results, must be put into the having clause | ||
describe('with aggregate function', () => { | ||
test('min', async () => { | ||
const { Books } = cds.entities | ||
let res = await SELECT.from(Books) | ||
.columns({ args: [{ ref: ['title'] }], as: 'firstInAlphabet', func: 'MIN' }) | ||
.groupBy('title') | ||
.search('Cat') | ||
expect(res.length).to.be.eq(1) | ||
}) | ||
}) | ||
|
||
}) |