diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index 964ecc393..1ee7f957a 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -1274,7 +1274,7 @@ function cqn4sql(originalQuery, model = cds.context?.model || cds.model) { transformedTokenStream.push({ list: [] }) } } else { - transformedTokenStream.push({ list: getTransformedTokenStream(token.list) }) + transformedTokenStream.push({ list: getTransformedTokenStream(token.list, $baseLink) }) } } else if (tokenStream.length === 1 && token.val && $baseLink) { // infix filter - OData variant w/o mentioning key --> flatten out and compare each leaf to token.val diff --git a/db-service/test/cqn4sql/table-alias.test.js b/db-service/test/cqn4sql/table-alias.test.js index 5a680ed25..b4c102d4c 100644 --- a/db-service/test/cqn4sql/table-alias.test.js +++ b/db-service/test/cqn4sql/table-alias.test.js @@ -950,6 +950,41 @@ describe('table alias access', () => { expect(res).to.deep.equal(expected) }) + it('handles ref in list of from with scoped query', () => { + const query = SELECT.from({ + ref: [ + { + id: 'bookshop.Books', + where: [ + { list: [{ ref: ['dedication', 'addressee', 'ID'] }] }, + 'in', + CQL`SELECT Books.ID from bookshop.Books as Books where Books.ID = 5`, + ], + }, + 'coAuthorUnmanaged', + ], + }).columns('ID') + + const list = [ + { + list: [{ ref: ['Books', 'dedication_addressee_ID'] }], + }, + 'in', + CQL`SELECT Books.ID from bookshop.Books as Books where Books.ID = 5`, + ] + + const expected = SELECT.from('bookshop.Authors as coAuthorUnmanaged').columns('coAuthorUnmanaged.ID').where(` + exists ( + SELECT 1 from bookshop.Books as Books where coAuthorUnmanaged.ID = Books.coAuthor_ID_unmanaged + ) + `) + + expected.SELECT.where[1].SELECT.where.push('and', ...list) + + const res = cqn4sql(query, model) + expect(res).to.deep.equal(expected) + }) + it('handles value subquery in WHERE', () => { let query = cqn4sql( CQL`SELECT from bookshop.Books { ID }