Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong odata count in filter with groupby #352

Merged
merged 19 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions db-service/lib/SQLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,15 @@ class SQLService extends DatabaseService {
if (max === undefined || (n < max && (n || !offset))) return n + offset
}
// REVISIT: made uppercase count because of HANA reserved word quoting
const cq = cds.ql.clone(query, {
columns: [{ func: 'count', as: 'COUNT' }],
const cq = SELECT.one([{ func: 'count', as: 'COUNT' }]).from(
cds.ql.clone(query, {
localized: false,
expand: false,
limit: 0,
orderBy: 0,
})
const { sql, values } = this.cqn2sql(cq)
const ps = await this.prepare(sql)
const { count, COUNT } = await ps.get(values)
limit: undefined,
orderBy: undefined,
}),
)
const { count, COUNT } = await this.onSELECT({ query: cq })
return count ?? COUNT
}

Expand Down
9 changes: 9 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('Bookshop - Read', () => {
expect(res.data['@odata.count']).to.be.eq(5)
})

test('Books $count with $top=2 and filter', async () => {
const res = await GET(
`/browse/ListOfBooks?$apply=filter(currency_code eq 'GBP')/groupby((ID),aggregate(ID with countdistinct as countBookings))&$count=true&$skip=0&$top=2`,
)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data['@odata.count']).to.be.eq(2)
})
larslutz96 marked this conversation as resolved.
Show resolved Hide resolved

test('Path expression', async () => {
const q = CQL`SELECT title, author.name as author FROM sap.capire.bookshop.Books where author.name LIKE '%a%'`
const res = await cds.run(q)
Expand Down
1 change: 1 addition & 0 deletions test/scenarios/sflight/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('SFlight - Read', () => {
// REVISIT: works in sflight not in tests
// 'Bookings?$apply=concat(groupby((BookingID,ConnectionID,CurrencyCode_code,FlightDate,ID,TravelID,airline,status))/aggregate($count%20as%20UI5__leaves),aggregate(FlightPrice,CurrencyCode_code),groupby((airline,airlineName),aggregate(FlightPrice,CurrencyCode_code))/concat(aggregate($count%20as%20UI5__count),top(53)))',
'Bookings?$apply=groupby((airline,airlineName),aggregate(FlightPrice with average as avgPrice,FlightPrice with max as maxPrice,FlightPrice with min as minPrice))&$skip=0&$top=1',
`Bookings?$apply=filter(airline eq 'EA' and status eq 'B')/groupby((ID),aggregate(FlightPrice,CurrencyCode_code))&$count=true&$skip=0&$top=1`
larslutz96 marked this conversation as resolved.
Show resolved Hide resolved
]

test.each(analyticsPaths)('/analytics/%s', async p => {
Expand Down
Loading