Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support formatting Trino queries #297

Merged
merged 4 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

Run `yarn` after checkout to install all dependencies.

## Branch Guidelines

New branches: Please branch off of the `develop` branch.

### Naming
## Branch Naming

Please use one of the following prefixes: (ie. feature/new-feature)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
It started as a port of a [PHP Library][], but has since considerably diverged.

It supports various SQL dialects:
GCP BigQuery, IBM DB2, Apache Hive, MariaDB, MySQL, Couchbase N1QL, Oracle PL/SQL, PostgreSQL, Amazon Redshift, Spark, SQL Server Transact-SQL.
GCP BigQuery, IBM DB2, Apache Hive, MariaDB, MySQL, Couchbase N1QL, Oracle PL/SQL, PostgreSQL, Amazon Redshift, Spark, SQL Server Transact-SQL, Trino/Presto.
See [language option docs](docs/language.md) for more details.

It does not support:
Expand Down Expand Up @@ -95,7 +95,7 @@ sql-formatter -h

```
usage: sql-formatter [-h] [-o OUTPUT] \
[-l {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,sqlite,tsql}] [-c CONFIG] [--version] [FILE]
[-l {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,sqlite,trino,tsql}] [-c CONFIG] [--version] [FILE]

SQL Formatter

Expand All @@ -106,7 +106,7 @@ optional arguments:
-h, --help show this help message and exit
-o, --output OUTPUT
File to write SQL output (defaults to stdout)
-l, --language {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,sqlite,tsql}
-l, --language {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,sqlite,trino,tsql}
SQL dialect (defaults to standard sql)
-c, --config CONFIG
Path to config json file (will use default configs if unspecified)
Expand Down
3 changes: 3 additions & 0 deletions docs/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Specifies the SQL dialect to use.
- `"redshift"` - [Amazon Redshift][]
- `"spark"` - [Spark][]
- `"sqlite"` - [SQLite][sqlite]
- `"trino"` - [Trino][] / [Presto][]
- `"tsql"` - [SQL Server Transact-SQL][tsql]

The default `"sql"` dialect is meant for cases where you don't know which dialect of SQL you're about to format.
Expand All @@ -32,7 +33,9 @@ Better to always pick something more specific if possible.
[couchbase n1ql]: http://www.couchbase.com/n1ql
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html
[postgresql]: https://www.postgresql.org/
[presto]: https://prestodb.io/docs/current/
[amazon redshift]: https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html
[spark]: https://spark.apache.org/docs/latest/api/sql/index.html
[sqlite]: https://sqlite.org/index.html
[trino]: https://trino.io/docs/current/
[tsql]: https://docs.microsoft.com/en-us/sql/sql-server/
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"pl/sql",
"transact-sql",
"db2",
"sqlite"
"sqlite",
"trino",
"presto",
"prestosql"
],
"contributors": [
"Adrien Pyke <adpyke@gmail.com>",
Expand Down
3 changes: 3 additions & 0 deletions sql/arrays-and-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Array literals `ARRAY[1, 2, 3]`. Supported by:

- [BigQuery][bigquery-literals].
- [PostgreSQL][postgres-literals].
- [Trino][]

Map literals in JSON style `{"foo": 1, "bar": "John"}`. Supported by:

Expand All @@ -28,6 +29,7 @@ Supported by:
- [Spark][]
- [N1QL][]
- [PostgreSQL][]
- [Trino][]

Array subscript operator `arr[OFFSET(5)]`. Supported by:

Expand All @@ -39,5 +41,6 @@ Array subscript operator `arr[OFFSET(5)]`. Supported by:
[n1ql-literals]: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/datatypes.html#arrays
[n1ql]: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/nestedops.html#field-selection
[postgresql]: https://www.postgresql.org/docs/current/arrays.html#ARRAYS-ACCESSING
[trino]: https://trino.io/docs/current/functions/array.html
[bigquery-literals]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#array_literals
[postgres-literals]: https://www.postgresql.org/docs/current/arrays.html
5 changes: 5 additions & 0 deletions sql/create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ _No support for CREATE TABLE._

CREATE TABLE

[Trino][]:

CREATE TABLE [IF NOT EXISTS]

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#_11_3_table_definition
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=statements-create-table
Expand All @@ -74,3 +78,4 @@ _No support for CREATE TABLE._
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-create-table.html
[sqlite]: https://www.sqlite.org/lang_createtable.html
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver15
[trino]: https://trino.io/docs/current/sql/create-table.html
5 changes: 5 additions & 0 deletions sql/create-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ _No support for CREATE VIEW._

CREATE [OR ALTER] VIEW

[Trino][]:

CREATE [OR REPLACE] VIEW

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#_11_22_view_definition
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_view_statement
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=statements-create-view
Expand All @@ -77,3 +81,4 @@ _No support for CREATE VIEW._
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-create-view.html
[sqlite]: https://www.sqlite.org/lang_createview.html
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15
[trino]: https://trino.io/docs/current/sql/create-view.html
5 changes: 5 additions & 0 deletions sql/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ _No support for DELETE_
[WHERE condition | WHERE CURRENT OF [GLOBAL] cursor_name]
[OPTION query_hints]

[Trino][]:

DELETE FROM table_name [WHERE condition]

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#_14_8_delete_statement_searched
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#delete_statement
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=statements-delete
Expand All @@ -109,3 +113,4 @@ _No support for DELETE_
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax.html#dml-statements
[sqlite]: https://www.sqlite.org/lang_delete.html
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql?view=sql-server-ver16
[trino]: https://trino.io/docs/current/sql/delete.html
3 changes: 3 additions & 0 deletions sql/identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The differences from this are listed below:
- [SQLite][sqlite-syntax-pdf]: _(no differences)_
- [Transact-SQL][]: `@` and `#` are allowed as first chars plus `$` in the rest. Also unicode letters are allowed.
Though the beginning `@` signifies a local variable or parameter and `#` a temporary table or procedure.
- [Trino][]: `[a-zA-Z0-9_]+`, no first-letter restrictions

## Delimited identifiers

Expand All @@ -36,6 +37,7 @@ There is a considerable variation in implementations:
- `` `..` `` [Spark][]
- `".."`, `` `..` ``, `[..]` [SQLite][sqlite-keywords]
- `".."`<sup>3</sup>, `[..]` [Transact-SQL][]
- `".."`, `` `..` `` [Trino][]

Notes:

Expand All @@ -56,3 +58,4 @@ Notes:
[sqlite-keywords]: https://www.sqlite.org/lang_keywords.html
[sqlite-syntax-pdf]: https://www.pearsonhighered.com/assets/samplechapter/0/6/7/2/067232685X.pdf
[transact-sql]: https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers?view=sql-server-ver15
[trino]: https://github.com/trinodb/trino/blob/ca7dcaa873b9dd24185e9a69cecdd1dd8717694c/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4#L1175-L1189
5 changes: 5 additions & 0 deletions sql/insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ All dialects (except Hive) suppurt this syntax, plus a bunch of extra stuff:

INSERT [TOP ( expression ) [PERCENT]] [INTO] table_name

[Trino][]:

INSERT INTO table_name

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#insert-statement
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#insert_statement
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=statements-insert
Expand All @@ -73,3 +77,4 @@ All dialects (except Hive) suppurt this syntax, plus a bunch of extra stuff:
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax-dml-insert-table.html
[sqlite]: https://www.sqlite.org/lang_insert.html
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-ver16
[trino]: https://trino.io/docs/current/sql/insert.html
2 changes: 2 additions & 0 deletions sql/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ These come in the form of single question mark (`?`), supported by:
- [N1QL][]
- [SQLite][]
- [Transact-SQL][]<sup>1</sup>
- [Trino][]

## Numbered parameters

Expand Down Expand Up @@ -51,3 +52,4 @@ These come in the form of single question mark (`?`), supported by:
[redshift]: https://docs.aws.amazon.com/redshift/latest/dg/r_PREPARE.html
[sqlite]: https://sqlite.org/c3ref/bind_blob.html
[transact-sql]: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/configuring-parameters-and-parameter-data-types
[trino]: https://trino.io/docs/current/sql/prepare.html
5 changes: 5 additions & 0 deletions sql/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ All dialects support that, but also quite a bit extra stuff:
[ALL | DISTINCT]
[TOP ( expression ) [PERCENT] [WITH TIES]]

[Trino][]:

SELECT [ALL | DISTINCT]

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#query-specification
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=queries-subselect
Expand All @@ -79,3 +83,4 @@ All dialects support that, but also quite a bit extra stuff:
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html
[sqlite]: https://www.sqlite.org/lang_select.html
[transact-sql]: https://docs.microsoft.com/en-US/sql/t-sql/queries/select-transact-sql?view=sql-server-ver15
[trino]: https://trino.io/docs/current/sql/select.html
5 changes: 5 additions & 0 deletions sql/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ The real world implementations have lots of variation:
- `'..'` (two single quotes `''` are used for escaping)
- (`".."`<sup>3</sup>)
- `N'..'` (`N".."`<sup>3</sup>) unicode strings
- [Trino][]:
- `'..'` (two single quotes `''` are used for escaping)
- `U&'..'`, `u&'..'` string with unicode escapes (two single quotes `''` are used for escaping)
- `X'..'`, `x'..'` hex string

### Notes:

Expand All @@ -67,3 +71,4 @@ The real world implementations have lots of variation:
[spark]: https://spark.apache.org/docs/latest/sql-ref-literals.html#string-literal
[sqlite]: https://www.sqlite.org/lang_expr.html#literal_values_constants_
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/data-types/constants-transact-sql?view=sql-server-ver15
[trino]: https://github.com/trinodb/trino/blob/ca7dcaa873b9dd24185e9a69cecdd1dd8717694c/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4#L1146-L1159
7 changes: 7 additions & 0 deletions sql/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ _No support for UPDATE_
[WHERE condition | WHERE CURRENT OF [GLOBAL] cursor_name]
[OPTION query_hints]

[Trino][]:

UPDATE table_name
SET [(column = expression [, ... ])]
[WHERE condition]

[sql standard]: https://jakewheat.github.io/sql-overview/sql-2008-foundation-grammar.html#_14_13_update_statement_searched
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#update_statement
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=statements-update
Expand All @@ -121,3 +127,4 @@ _No support for UPDATE_
[spark]: https://spark.apache.org/docs/latest/sql-ref-syntax.html#dml-statements
[sqlite]: https://www.sqlite.org/lang_update.html
[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/queries/update-transact-sql?view=sql-server-ver16
[trino]: https://trino.io/docs/current/sql/update.html
1 change: 1 addition & 0 deletions sql/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Spark][]: `${name}` Like with Hive, these are substitution variables.
- SQLite: _N/A_
- [Transact-SQL][]: `@name` (using identifier syntax for name)
- Trino: _N/A_

[parameters]: ./parameters.md
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language
Expand Down
Loading