Skip to content

Conversation

hannes
Copy link
Member

@hannes hannes commented Oct 18, 2024

This PR expands the accepted SQL of DuckDB once again to add support for Aliases before SELECT and FROM expressions. Often, having the alias first increases readability. We use the colon : to separate aliases from expressions. For example, the following query

SELECT 
  some_long_and_winding expression AS e1,
  a_shorter_expression AS e2
FROM
  long_schema.some_long_table_name AS t1,
  some_schema.some_table AS t2

can now be written like so:

SELECT 
  e1: some_long_and_winding expression,
  e2: a_shorter_expression 
FROM
  t1: long_schema.some_long_table_name,
  t2: some_schema.some_table

Aliases can be quoted if necessary, only double quotes are supported.

SELECT "my alias with spaces": 42

Prefix aliases also name expressions, function calls, subqueries, etc. e.g.:

SELECT 
  e: 1 + 2, 
  f: len('asdf'), 
  s: (SELECT 42);

This also works in the FROM clause, even if the example is somewhat nonsensical:

FROM
  r: range(10),
  v: (VALUES(42)),
  s: (FROM range(10))

One restriction is that due to parser limited lookahead (to be fixed eventually), there can not be column renames in prefix aliases in the FROM clause, so this does NOT work

FROM r1(r2) : range(10);

CC @lloydtabb @szarnyasg @Alex-Monahan

@hannes hannes requested a review from Mytherin October 18, 2024 10:45
@Mytherin Mytherin merged commit f162a9a into duckdb:feature Oct 18, 2024
40 checks passed
@Mytherin
Copy link
Collaborator

Thanks!

@hannes hannes deleted the colonprefixalias branch October 29, 2024 15:03
@szarnyasg szarnyasg added the Needs Documentation Use for issues or PRs that require changes in the documentation label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation Use for issues or PRs that require changes in the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants