Skip to content

Commit

Permalink
fix(cqn4sql): expand structured keys in on-conditions (#421)
Browse files Browse the repository at this point in the history
this was forgotten and becomes especially important with universal csn
in the context of lean draft. Where a `SiblingEntity` association might
have a structured key comparison in the on condition.
  • Loading branch information
patricebender authored Jan 18, 2024
1 parent 78e8f7e commit b1e0677
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ function cqn4sql(originalQuery, model = cds.context?.model || cds.model) {
else {
const lhsLeafArt = lhs.ref && lhs.$refLinks[lhs.$refLinks.length - 1].definition
const rhsLeafArt = rhs.ref && rhs.$refLinks[rhs.$refLinks.length - 1].definition
if (lhsLeafArt?.target || rhsLeafArt?.target) {
if (lhsLeafArt?.target && rhsLeafArt?.target || lhsLeafArt?.elements && rhsLeafArt?.elements) {
if (rhs.$refLinks[0].definition !== assocRefLink.definition) {
rhs.ref.unshift(targetSideRefLink.alias)
rhs.$refLinks.unshift(targetSideRefLink)
Expand Down
10 changes: 10 additions & 0 deletions db-service/test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,13 @@ entity PartialStructuredKey {
author : Association to Authors;
accessGroup : Composition of AccessGroups;
}

entity Unmanaged {
key struct: {
leaf: Int16;
toBook: Association to Books;
};
field: Integer;
// needs to be expanded in join-conditions
toSelf: Association to Unmanaged on struct = toSelf.struct;
}
15 changes: 15 additions & 0 deletions db-service/test/cqn4sql/tupleExpansion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,19 @@ describe('Structural comparison', () => {
)
})
})
it('Struct needs to be unfolded in on-condition of join', () => {
const queryString = `SELECT from bookshop.Unmanaged {
toSelf.field
}`

const expected = CQL`SELECT from bookshop.Unmanaged as Unmanaged
left join bookshop.Unmanaged as toSelf
on Unmanaged.struct_leaf = toSelf.struct_leaf and Unmanaged.struct_toBook_ID = toSelf.struct_toBook_ID {
toSelf.field as toSelf_field
}
`
// const unfolded = cds.compile.for.nodejs(JSON.parse(JSON.stringify(model)))
const res = cqn4sql(CQL(queryString), model)
expect(res).to.eql(expected)
})
})

0 comments on commit b1e0677

Please sign in to comment.