Skip to content

Sending a cqn targeting non-model entities, fails for subquery selects #1013

@patricebender

Description

@patricebender

Go into the bookshop and start a repl session:

> sql = `
... SELECT author_name, total_books
... FROM (
...     SELECT 
...         a.name AS author_name, 
...         COUNT(b.ID) AS total_books
...     FROM sap_capire_bookshop_Authors a
...     LEFT JOIN sap_capire_bookshop_Books b ON a.ID = b.author_ID
...     GROUP BY a.name
... ) AS author_summary
... ORDER BY total_books DESC;
> await cds.run(sql)
[
  { author_name: 'Edgar Allen Poe', total_books: 2 },
  { author_name: 'Charlotte Brontë', total_books: 1 },
  { author_name: 'Emily Brontë', total_books: 1 },
  { author_name: 'Richard Carpenter', total_books: 1 }
]

so far so good. We had a customer doing (weird) stuff like this:

> await cds.run( cds.parse.cql(sql) )
Uncaught Error: "sap_capire_bookshop_Authors" not found in the definitions of your model
    at inferTarget (/Users/patricebender/SAPDevelop/dev/cds-dbs/db-service/lib/infer/index.js:105:53)

what usually doesn't bother us, because here:

cqn4sql(q) {
if (
!cds.env.features.db_strict &&
!q.SELECT?.from?.join &&
!q.SELECT?.from?.SELECT &&
!this.model?.definitions[_target_name4(q)]
) return q
else return cqn4sql(q, this.model)
}

we check for non-modeled entities and if that is the case, we just send the query to the database. However, that mechanism does not work, because the unresolvable entity lives within the subquery.

@BobdenOs @johannes-vogel FYI

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcqn2sqlcqn-to-sql transformation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions