prestodb / presto

The official home of the Presto distributed SQL query engine for big data
http://prestodb.io
Apache License 2.0
15.93k stars 5.33k forks source link

Support Google-style trailing comma in SELECT #23569

Open the80srobot opened 2 weeks ago

the80srobot commented 2 weeks ago

I don't think this has been requested before, but I'd like to propose support for a trailing comma in SELECT. This is a convenience feature supported by some other dialects, most notably by Google SQL.

Expected Behavior or Use Case

Currently, this example from the Google SQL docs is not accepted in Presto:

SELECT name, release_date, FROM Books

Accepting the comma after "release_date" leads to no ambiguity and makes it easier to edit the query in the common case where columns are listed on separate lines:

SELECT
  name,
  release_date,
FROM Books

Presto Component, Service, or Connector

SQL parser.

Possible Implementation

Specifically in SqlBase.g4 we'd need to change QuerySpecification.

The code in com.facebook.presto.sql.parser looks like it wouldn't need many changes.

I don't propose having the formatter emit the trailing comma, so no changes would be required there.

Example Screenshots (if appropriate):

See above.

Context

This is useful for two reasons:

  1. As more SQL engines support the trailing comma, it's one less thing to worry about when switching back and forth between them.
  2. Editing multiline queries is a lot more convenient if you don't have to delete and add back the commas all the time
ZacBlanco commented 2 weeks ago

Presto usually adheres to ANSI SQL spec, which is probably why we don't support this already. However, I think allowing trailing commas is a relatively harmless changes and generally removes a bunch of headaches with syntax errors when forgetting to remove the trailing comma. Many programming languages have adopted this style as well in argument lists (e.g. Python, Rust, TypeScript). I would be in favor of this improvement.

aditi-pandit commented 2 weeks ago

+1 as well.

elharo commented 2 weeks ago

I'm a weak -1. I prefer to avoid new discrepancies with ANSI SQL and also like to avoid multiple ways to do things.