Skip to content

Commit

Permalink
fix: deep groupby expand queries
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs committed Aug 2, 2024
1 parent 4ac37de commit d941933
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ function cqn4sql(originalQuery, model) {

if (expand.expand) {
const nested = _subqueryForGroupBy(expand, fullRef, expand.as || expand.ref.map(idOnly).join('_'))
setElementOnColumns(nested, expand.element)
elements[expand.as || expand.ref.map(idOnly).join('_')] = nested
return nested
}
Expand Down
15 changes: 11 additions & 4 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,18 @@ class HANAService extends SQLService {
if (col.ref?.length > 1) {
const colName = this.column_name(col)
if (col.ref[0] !== parent.as) {
// Inject foreign columns into parent select
// Inject foreign columns into parent selects (recursively)
const as = `$$${col.ref.join('.')}$$`
parent.SELECT.columns.push({ __proto__: col, ref: col.ref, as })
let curPar = parent
while (curPar) {
if (curPar.SELECT.from?.args?.some(a => a.as === col.ref[0])) {
curPar.SELECT.columns.push({ __proto__: col, ref: col.ref, as })
break
} else {
curPar.SELECT.columns.push({ __proto__: col, ref: [curPar.SELECT.parent.as, as], as })
curPar = curPar.SELECT.parent
}
}
col.as = colName
col.ref = [parent.as, as]
} else if (!parent.SELECT.columns.some(c => this.column_name(c) === colName)) {
Expand Down Expand Up @@ -557,8 +566,6 @@ class HANAService extends SQLService {
)
.filter(a => a)

if (sql.length === 0) sql = '*'

if (SELECT.expand === 'root') {
this._blobs = blobs
const blobColumns = Object.keys(blobs)
Expand Down
8 changes: 8 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('Bookshop - Read', () => {
).to.be.true
})

test('same as above, with more depth', async () => {
const res = await GET(
'/admin/Books?$apply=filter(title%20ne%20%27bar%27)/groupby((genre/parent/name),aggregate(price with sum as totalAmount))',
admin,
)
expect(res.data.value[0].genre.parent.name).to.be.eq('Fiction')
})

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

0 comments on commit d941933

Please sign in to comment.