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: deep groupby expand queries #768

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading