Skip to content

Commit

Permalink
fix: Remove orderBy ref check for sql_simple_queries (#964)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Vogel <[email protected]>
  • Loading branch information
BobdenOs and johannes-vogel authored Jan 8, 2025
1 parent 19e6166 commit 1b77725
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class HANAService extends SQLService {
throw new Error('CQN query using joins must specify the selected columns.')
}

let { limit, one, from, orderBy, having, expand, columns = ['*'], localized, count, parent } = q.SELECT
let { limit, one, distinct, from, orderBy, having, expand, columns = ['*'], localized, count, parent } = q.SELECT

// When one of these is defined wrap the query in a sub query
if (expand || (parent && (limit || one || orderBy))) {
Expand Down Expand Up @@ -373,6 +373,7 @@ class HANAService extends SQLService {

let orderByHasOutputColumnRef = false
if (orderBy) {
if (distinct) orderByHasOutputColumnRef = true
// Ensure that all columns used in the orderBy clause are exposed
orderBy = orderBy.map((c, i) => {
if (!c.ref) {
Expand Down
11 changes: 11 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ describe('Bookshop - Read', () => {
expect(res[0].price).to.be.eq('150')
})

test('select distinct order by selected result column with alias', async () => {
const { Authors } = cds.entities('sap.capire.bookshop')
const res = await SELECT.distinct
.columns`ID`
.from`${Authors} as a`
.orderBy`a.ID`

expect(res.length).to.be.eq(4)
expect(res[0].ID).to.be.eq(101)
})

test('reuse already executed select as subselect', async () => {
let s = SELECT.columns('ID').from('sap.capire.bookshop.Books')
let res = await s
Expand Down

0 comments on commit 1b77725

Please sign in to comment.