Skip to content

chore(cqn4sql): make it possible to use nice aliases again#1466

Open
patricebender wants to merge 5 commits intomainfrom
patrice/nice-aliases
Open

chore(cqn4sql): make it possible to use nice aliases again#1466
patricebender wants to merge 5 commits intomainfrom
patrice/nice-aliases

Conversation

@patricebender
Copy link
Member

@patricebender patricebender commented Jan 15, 2026

A while back we introduced technical aliases (like $B, $a, etc.) to prevent ambiguities for some OData requests.

However, sometimes it is nice to have readable aliases again, e.g., for presentations or debugging.

Set the environment variable USE_TECHNICAL_ALIAS=false to get nice aliases again:

USE_TECHNICAL_ALIAS=false cds r --run .
> cds.ql`SELECT author.name, genre.name from ${Books} where exists author[books.genre.name == 'Fantasy']`.toSQL().sql
--- nice.sql
+++ technical.sql
@@ -1,15 +1,15 @@
 SELECT author.name as author_name,
   genre.name as genre_name
-FROM sap_capire_bookshop_Books as Books
-  left JOIN sap_capire_bookshop_Authors as author ON author.ID = Books.author_ID
-  left JOIN sap_capire_bookshop_Genres as genre ON genre.ID = Books.genre_ID
+FROM sap_capire_bookshop_Books as "$B"
+  left JOIN sap_capire_bookshop_Authors as author ON author.ID = "$B".author_ID
+  left JOIN sap_capire_bookshop_Genres as genre ON genre.ID = "$B".genre_ID
 WHERE exists (
     SELECT 1 as "1"
-    FROM sap_capire_bookshop_Authors as author2
-      inner JOIN sap_capire_bookshop_Books as books2 ON books2.author_ID = author2.ID
+    FROM sap_capire_bookshop_Authors as "$a"
+      inner JOIN sap_capire_bookshop_Books as books ON books.author_ID = "$a".ID
       inner JOIN sap_capire_bookshop_Genres as genre2 ON genre2.ID = books.genre_ID
-    WHERE author2.ID = Books.author_ID
+    WHERE "$a".ID = "$B".author_ID
       and genre2.name is 'Fantasy'
   );

A while back we introduced [technical aliases](#1082) (like `$B`, `$a`, etc.)
to prevent ambiguities for some OData requests.

However, sometimes it is nice to have readable aliases again, e.g., for presentations or debugging.

Set the environment variable `USE_TECHNICAL_ALIAS=false` to get nice aliases again:

```js
USE_TECHNICAL_ALIAS=false cds r --run .
> cds.ql`SELECT author.name, genre.name from ${Books} where exists author[books.genre.name == 'Fantasy']`.toSQL().sql
```

```diff
--- a/query.sql
+++ b/query.sql
@@ -1,15 +1,15 @@
 SELECT author.name as author_name,
   genre.name as genre_name
-FROM sap_capire_bookshop_Books as Books
-  left JOIN sap_capire_bookshop_Authors as author ON author.ID = Books.author_ID
-  left JOIN sap_capire_bookshop_Genres as genre ON genre.ID = Books.genre_ID
+FROM sap_capire_bookshop_Books as "$B"
+  left JOIN sap_capire_bookshop_Authors as author ON author.ID = "$B".author_ID
+  left JOIN sap_capire_bookshop_Genres as genre ON genre.ID = "$B".genre_ID
 WHERE exists (
     SELECT 1 as "1"
-    FROM sap_capire_bookshop_Authors as author2
-      inner JOIN sap_capire_bookshop_Books as books2 ON books2.author_ID = author2.ID
-      inner JOIN sap_capire_bookshop_Genres as genre2 ON genre2.ID = books2.genre_ID
-    WHERE author2.ID = Books.author_ID
+    FROM sap_capire_bookshop_Authors as "$a"
+      inner JOIN sap_capire_bookshop_Books as books ON books.author_ID = "$a".ID
+      inner JOIN sap_capire_bookshop_Genres as genre2 ON genre2.ID = books.genre_ID
+    WHERE "$a".ID = "$B".author_ID
       and genre2.name is 'Fantasy'
   );
```
@patricebender patricebender marked this pull request as draft January 15, 2026 16:46
@Akatuoro
Copy link
Contributor

Akatuoro commented Feb 17, 2026

This is really good for making the queries more readable 👍 It does not need to be used in production where there may be reasons for technical aliases, but especially for inspecting the SQL, it is quite helpful:

Screenshot 2026-02-17 at 08 42 10

Maybe we could use CDS_USE_TECHNICAL_ALIAS as environmental variable (or some option in the cds env) to make sure that we don't clash with application defined ones (as unlikely as that is).

@patricebender patricebender marked this pull request as ready for review February 24, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants