Skip to content

Commit

Permalink
fix: groupby path expression with overlapping identifier (#992)
Browse files Browse the repository at this point in the history
Currently, the HANA service is unable to `GROUP BY foobar.foobar`,
raising an error like `invalid column name: foobar.foobar`, if the query
includes an aggregation and `sql syntax error: incorrect syntax near
"FROM"`, if it does not. The problem is caused by the overlapping column
names and the HANA service not correctly injecting the column groupby
requires into the parent select - if the parent includes a column with
the same `column_name` as the 'grouped-by' column.

This fix enables the required injection, by skipping the overlapping
names check for columns that contain a select.

---------

Co-authored-by: Bob den Os <[email protected]>
Co-authored-by: Johannes Vogel <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent 59a598c commit b579da8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class HANAService extends SQLService {
// if (col.ref?.length === 1) { col.ref.unshift(parent.as) }
if (col.ref?.length > 1) {
const colName = this.column_name(col)
if (!parent.SELECT.columns.some(c => this.column_name(c) === colName)) {
if (!parent.SELECT.columns.some(c => !c.elements && this.column_name(c) === colName)) {
const isSource = from => {
if (from.as === col.ref[0]) return true
return from.args?.some(a => {
Expand Down
12 changes: 12 additions & 0 deletions test/compliance/SELECT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,18 @@ describe('SELECT', () => {
const res = await cds.run(cqn)
assert.strictEqual(res.length, 3, 'Ensure that all rows are coming back')
})

test('navigation with duplicate identifier in path', async () => {
const { Books } = cds.entities('complex.associations')
const res = await cds.ql`SELECT name { name } FROM ${Books} GROUP BY name.name`
assert.strictEqual(res.length, 1, 'Ensure that all rows are coming back')
})

test('navigation with duplicate identifier in path and aggregation', async () => {
const { Books } = cds.entities('complex.associations')
const res = await cds.ql`SELECT name { name }, count(1) as total FROM ${Books} GROUP BY name.name`
assert.strictEqual(res.length, 1, 'Ensure that all rows are coming back')
})
})

describe('having', () => {
Expand Down
1 change: 1 addition & 0 deletions test/compliance/resources/db/complex/associations.cds
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ entity Books {
key ID : Integer;
title : String(111);
author : Association to Authors;
name : Association to Authors on $self.author.ID = name.ID;
}

entity Authors {
Expand Down

0 comments on commit b579da8

Please sign in to comment.