Skip to content

fix(expand): proper subquery from construction #1126

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

Merged
merged 3 commits into from
Apr 9, 2025
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
3 changes: 2 additions & 1 deletion db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ function cqn4sql(originalQuery, model) {
})
} else {
outerAlias = transformedQuery.SELECT.from.as
const getInnermostTarget = q => q._target ? getInnermostTarget(q._target) : q
subqueryFromRef = [
...(transformedQuery.SELECT.from.ref || /* subq in from */ transformedQuery.SELECT.from.SELECT.from.ref),
...(transformedQuery.SELECT.from.ref || /* subq in from */ [getInnermostTarget(transformedQuery).name]),
...ref,
]
}
Expand Down
87 changes: 87 additions & 0 deletions db-service/test/cqn4sql/expand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,93 @@ describe('Unfold expands on associations to special subselects', () => {
}`
expect(JSON.parse(JSON.stringify(cqn4sql(q, model)))).to.eql(expected)
})

it('expand via subquery with path expressions', () => {
const q = cds.ql`SELECT from (SELECT from bookshop.Books as inner { author, ID } where author.name = 'King') as Outer {
ID,
author { name }
}`
const res = cqn4sql(q, model)
const expected = cds.ql`
SELECT from (
SELECT from bookshop.Books as inner
left join bookshop.Authors as author on author.ID = inner.author_ID {
inner.author_ID,
inner.ID
} where author.name = 'King'
) as Outer
{
Outer.ID,
(
SELECT from bookshop.Authors as $a {
$a.name
}
where Outer.author_ID = $a.ID
) as author
}`
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})
it('expand via subquery with path expressions nested', () => {
const q = cds.ql`SELECT from (SELECT from (SELECT from bookshop.Books as inner { author, ID } where author.name = 'King') as Mid { * }) as Outer {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw this revealed another issue, which is not subject to this PR. So I will ship a dedicated fix for #1127 once this is in main

ID,
author { name }
}`
const res = cqn4sql(q, model)
const expected = cds.ql`
SELECT from (
SELECT from (
SELECT from bookshop.Books as inner
left join bookshop.Authors as author on author.ID = inner.author_ID {
inner.author_ID,
inner.ID
} where author.name = 'King'
) as Mid {
Mid.author_ID,
Mid.ID
}
) as Outer
{
Outer.ID,
(
SELECT from bookshop.Authors as $a {
$a.name
}
where Outer.author_ID = $a.ID
) as author
}`
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})
it('expand via subquery with path expressions and scoped query', () => {
const q = cds.ql`SELECT from (SELECT from bookshop.Books:genre as inner { parent, ID } where parent.name = 'Drama') as Outer {
ID,
parent { name }
}`
const res = cqn4sql(q, model)
const expected = cds.ql`
SELECT from (
SELECT from bookshop.Genres as inner
left join bookshop.Genres as parent on parent.ID = inner.parent_ID {
inner.parent_ID,
inner.ID
} where
exists (
SELECT 1 from bookshop.Books as $B
where $B.genre_ID = inner.ID
)
and parent.name = 'Drama'
) as Outer
{
Outer.ID,
(
SELECT from bookshop.Genres as $p {
$p.name
}
where Outer.parent_ID = $p.ID
) as parent
}
`
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})
})

describe('Expands with aggregations are special', () => {
Expand Down
Loading