Skip to content

Commit 585bb8a

Browse files
committed
Merge Initial TiDB Support (PR #724)
2 parents e2bb28b + cd2db33 commit 585bb8a

File tree

14 files changed

+905
-3
lines changed

14 files changed

+905
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Boris Verkhovskiy <[email protected]>
1313
Christian Jorgensen <[email protected]>
1414
Christopher Manouvrier <[email protected]>
1515
Damon Davison <[email protected]>
16+
Daniël van Eeden <[email protected]>
1617
Davut Can Abacigil <[email protected]>
1718
1819
Erik Hirmo <[email protected]>

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
It started as a port of a [PHP Library][], but has since considerably diverged.
88

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

1313
It does not support:
@@ -123,7 +123,7 @@ sql-formatter -h
123123

124124
```
125125
usage: sql-formatter [-h] [-o OUTPUT] \
126-
[-l {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,transactsql,trino,tsql}] [-c CONFIG] [--version] [FILE]
126+
[-l {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,tidb,transactsql,trino,tsql}] [-c CONFIG] [--version] [FILE]
127127
128128
SQL Formatter
129129
@@ -135,7 +135,7 @@ optional arguments:
135135
-o, --output OUTPUT
136136
File to write SQL output (defaults to stdout)
137137
--fix Update the file in-place
138-
-l, --language {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,trino,tsql}
138+
-l, --language {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,tidb,trino,tsql}
139139
SQL dialect (defaults to basic sql)
140140
-c, --config CONFIG
141141
Path to config JSON file or json string (will use default configs if unspecified)

docs/dialect.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The following dialects can be imported from `"sql-formatter"` module:
2727
- `hive` - [Apache Hive][]
2828
- `mariadb` - [MariaDB][]
2929
- `mysql` - [MySQL][]
30+
- `tidb` - [TiDB][]
3031
- `n1ql` - [Couchbase N1QL][]
3132
- `plsql` - [Oracle PL/SQL][]
3233
- `postgresql` - [PostgreSQL][]
@@ -75,6 +76,7 @@ You likely only want to use this if your other alternative is to fork SQL Format
7576
[apache hive]: https://hive.apache.org/
7677
[mariadb]: https://mariadb.com/
7778
[mysql]: https://www.mysql.com/
79+
[tidb]: https://github.com/pingcap/tidb/
7880
[couchbase n1ql]: http://www.couchbase.com/n1ql
7981
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html
8082
[postgresql]: https://www.postgresql.org/

docs/language.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const result = format('SELECT * FROM tbl', { dialect: 'sqlite' });
1919
- `"hive"` - [Apache Hive][]
2020
- `"mariadb"` - [MariaDB][]
2121
- `"mysql"` - [MySQL][]
22+
- `"tidb"` - [TiDB][]
2223
- `"n1ql"` - [Couchbase N1QL][]
2324
- `"plsql"` - [Oracle PL/SQL][]
2425
- `"postgresql"` - [PostgreSQL][]
@@ -52,6 +53,7 @@ See docs for [dialect][] option.
5253
[apache hive]: https://hive.apache.org/
5354
[mariadb]: https://mariadb.com/
5455
[mysql]: https://www.mysql.com/
56+
[tidb]: https://github.com/pingcap/tidb/
5557
[couchbase n1ql]: http://www.couchbase.com/n1ql
5658
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html
5759
[postgresql]: https://www.postgresql.org/

docs/params.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The placeholder types available by default depend on SQL dialect used:
121121
- snowflake - _no support_
122122
- sqlite - `?`, `?1`, `:name`, `@name`, `$name`
123123
- spark - _no support_
124+
- tidb - `?`
124125
- tsql - `@name`, `@"name"`, `@[name]`
125126
- trino - _no support_
126127

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"db2",
3636
"db2i",
3737
"sqlite",
38+
"tidb",
3839
"trino",
3940
"presto",
4041
"prestosql",

src/allDialects.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { db2i } from './languages/db2i/db2i.formatter.js';
44
export { hive } from './languages/hive/hive.formatter.js';
55
export { mariadb } from './languages/mariadb/mariadb.formatter.js';
66
export { mysql } from './languages/mysql/mysql.formatter.js';
7+
export { tidb } from './languages/tidb/tidb.formatter.js';
78
export { n1ql } from './languages/n1ql/n1ql.formatter.js';
89
export { plsql } from './languages/plsql/plsql.formatter.js';
910
export { postgresql } from './languages/postgresql/postgresql.formatter.js';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { db2i } from './languages/db2i/db2i.formatter.js';
99
export { hive } from './languages/hive/hive.formatter.js';
1010
export { mariadb } from './languages/mariadb/mariadb.formatter.js';
1111
export { mysql } from './languages/mysql/mysql.formatter.js';
12+
export { tidb } from './languages/tidb/tidb.formatter.js';
1213
export { n1ql } from './languages/n1ql/n1ql.formatter.js';
1314
export { plsql } from './languages/plsql/plsql.formatter.js';
1415
export { postgresql } from './languages/postgresql/postgresql.formatter.js';

src/languages/tidb/tidb.formatter.ts

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
import { DialectOptions } from '../../dialect.js';
2+
import { expandPhrases } from '../../expandPhrases.js';
3+
import { postProcess } from '../mariadb/likeMariaDb.js';
4+
import { dataTypes, keywords } from './tidb.keywords.js';
5+
import { functions } from './tidb.functions.js';
6+
7+
const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT | DISTINCTROW]']);
8+
9+
const reservedClauses = expandPhrases([
10+
// queries
11+
'WITH [RECURSIVE]',
12+
'FROM',
13+
'WHERE',
14+
'GROUP BY',
15+
'HAVING',
16+
'WINDOW',
17+
'PARTITION BY',
18+
'ORDER BY',
19+
'LIMIT',
20+
'OFFSET',
21+
// Data manipulation
22+
// - insert:
23+
'INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO]',
24+
'REPLACE [LOW_PRIORITY | DELAYED] [INTO]',
25+
'VALUES',
26+
'ON DUPLICATE KEY UPDATE',
27+
// - update:
28+
'SET',
29+
]);
30+
31+
const standardOnelineClauses = expandPhrases(['CREATE [TEMPORARY] TABLE [IF NOT EXISTS]']);
32+
33+
const tabularOnelineClauses = expandPhrases([
34+
// https://docs.pingcap.com/tidb/stable/sql-statement-create-view
35+
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
36+
// https://docs.pingcap.com/tidb/stable/sql-statement-update
37+
'UPDATE [LOW_PRIORITY] [IGNORE]',
38+
// https://docs.pingcap.com/tidb/stable/sql-statement-delete
39+
'DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM',
40+
// https://docs.pingcap.com/tidb/stable/sql-statement-drop-table
41+
'DROP [TEMPORARY] TABLE [IF EXISTS]',
42+
// https://docs.pingcap.com/tidb/stable/sql-statement-alter-table
43+
'ALTER TABLE',
44+
'ADD [COLUMN]',
45+
'{CHANGE | MODIFY} [COLUMN]',
46+
'DROP [COLUMN]',
47+
'RENAME [TO | AS]',
48+
'RENAME COLUMN',
49+
'ALTER [COLUMN]',
50+
'{SET | DROP} DEFAULT', // for alter column
51+
// https://docs.pingcap.com/tidb/stable/sql-statement-truncate
52+
'TRUNCATE [TABLE]',
53+
// https://docs.pingcap.com/tidb/stable/sql-statement-alter-database
54+
'ALTER DATABASE',
55+
// https://docs.pingcap.com/tidb/stable/sql-statement-alter-instance
56+
'ALTER INSTANCE',
57+
'ALTER RESOURCE GROUP',
58+
'ALTER SEQUENCE',
59+
// https://docs.pingcap.com/tidb/stable/sql-statement-alter-user
60+
'ALTER USER',
61+
'ALTER VIEW',
62+
'ANALYZE TABLE',
63+
'CHECK TABLE',
64+
'CHECKSUM TABLE',
65+
'COMMIT',
66+
'CREATE DATABASE',
67+
'CREATE INDEX',
68+
'CREATE RESOURCE GROUP',
69+
'CREATE ROLE',
70+
'CREATE SEQUENCE',
71+
'CREATE USER',
72+
'DEALLOCATE PREPARE',
73+
'DESCRIBE',
74+
'DROP DATABASE',
75+
'DROP INDEX',
76+
'DROP RESOURCE GROUP',
77+
'DROP ROLE',
78+
'DROP TABLESPACE',
79+
'DROP USER',
80+
'DROP VIEW',
81+
'EXPLAIN',
82+
'FLUSH',
83+
// https://docs.pingcap.com/tidb/stable/sql-statement-grant-privileges
84+
'GRANT',
85+
'IMPORT TABLE',
86+
'INSTALL COMPONENT',
87+
'INSTALL PLUGIN',
88+
'KILL',
89+
'LOAD DATA',
90+
'LOCK INSTANCE FOR BACKUP',
91+
'LOCK TABLES',
92+
'OPTIMIZE TABLE',
93+
'PREPARE',
94+
'RELEASE SAVEPOINT',
95+
'RENAME TABLE',
96+
'RENAME USER',
97+
'REPAIR TABLE',
98+
'RESET',
99+
'REVOKE',
100+
'ROLLBACK',
101+
'ROLLBACK TO SAVEPOINT',
102+
'SAVEPOINT',
103+
'SET CHARACTER SET',
104+
'SET DEFAULT ROLE',
105+
'SET NAMES',
106+
'SET PASSWORD',
107+
'SET RESOURCE GROUP',
108+
'SET ROLE',
109+
'SET TRANSACTION',
110+
'SHOW',
111+
'SHOW BINARY LOGS',
112+
'SHOW BINLOG EVENTS',
113+
'SHOW CHARACTER SET',
114+
'SHOW COLLATION',
115+
'SHOW COLUMNS',
116+
'SHOW CREATE DATABASE',
117+
'SHOW CREATE TABLE',
118+
'SHOW CREATE USER',
119+
'SHOW CREATE VIEW',
120+
'SHOW DATABASES',
121+
'SHOW ENGINE',
122+
'SHOW ENGINES',
123+
'SHOW ERRORS',
124+
'SHOW EVENTS',
125+
'SHOW GRANTS',
126+
'SHOW INDEX',
127+
'SHOW MASTER STATUS',
128+
'SHOW OPEN TABLES',
129+
'SHOW PLUGINS',
130+
'SHOW PRIVILEGES',
131+
'SHOW PROCESSLIST',
132+
'SHOW PROFILE',
133+
'SHOW PROFILES',
134+
'SHOW STATUS',
135+
'SHOW TABLE STATUS',
136+
'SHOW TABLES',
137+
'SHOW TRIGGERS',
138+
'SHOW VARIABLES',
139+
'SHOW WARNINGS',
140+
// https://docs.pingcap.com/tidb/stable/sql-statement-table
141+
'TABLE',
142+
'UNINSTALL COMPONENT',
143+
'UNINSTALL PLUGIN',
144+
'UNLOCK INSTANCE',
145+
'UNLOCK TABLES',
146+
// https://docs.pingcap.com/tidb/stable/sql-statement-use
147+
'USE',
148+
]);
149+
150+
const reservedSetOperations = expandPhrases(['UNION [ALL | DISTINCT]']);
151+
152+
const reservedJoins = expandPhrases([
153+
'JOIN',
154+
'{LEFT | RIGHT} [OUTER] JOIN',
155+
'{INNER | CROSS} JOIN',
156+
'NATURAL [INNER] JOIN',
157+
'NATURAL {LEFT | RIGHT} [OUTER] JOIN',
158+
// non-standard joins
159+
'STRAIGHT_JOIN',
160+
]);
161+
162+
const reservedPhrases = expandPhrases([
163+
'ON {UPDATE | DELETE} [SET NULL]',
164+
'CHARACTER SET',
165+
'{ROWS | RANGE} BETWEEN',
166+
'IDENTIFIED BY',
167+
]);
168+
169+
// https://docs.pingcap.com/tidb/stable/basic-features
170+
export const tidb: DialectOptions = {
171+
name: 'tidb',
172+
tokenizerOptions: {
173+
reservedSelect,
174+
reservedClauses: [...reservedClauses, ...standardOnelineClauses, ...tabularOnelineClauses],
175+
reservedSetOperations,
176+
reservedJoins,
177+
reservedPhrases,
178+
supportsXor: true,
179+
reservedKeywords: keywords,
180+
reservedDataTypes: dataTypes,
181+
reservedFunctionNames: functions,
182+
// TODO: support _ char set prefixes such as _utf8, _latin1, _binary, _utf8mb4, etc.
183+
stringTypes: [
184+
'""-qq-bs',
185+
{ quote: "''-qq-bs", prefixes: ['N'] },
186+
{ quote: "''-raw", prefixes: ['B', 'X'], requirePrefix: true },
187+
],
188+
identTypes: ['``'],
189+
identChars: { first: '$', rest: '$', allowFirstCharNumber: true },
190+
variableTypes: [
191+
{ regex: '@@?[A-Za-z0-9_.$]+' },
192+
{ quote: '""-qq-bs', prefixes: ['@'], requirePrefix: true },
193+
{ quote: "''-qq-bs", prefixes: ['@'], requirePrefix: true },
194+
{ quote: '``', prefixes: ['@'], requirePrefix: true },
195+
],
196+
paramTypes: { positional: true },
197+
lineCommentTypes: ['--', '#'],
198+
operators: [
199+
'%',
200+
':=',
201+
'&',
202+
'|',
203+
'^',
204+
'~',
205+
'<<',
206+
'>>',
207+
'<=>',
208+
'->',
209+
'->>',
210+
'&&',
211+
'||',
212+
'!',
213+
'*.*', // Not actually an operator
214+
],
215+
postProcess,
216+
},
217+
formatOptions: {
218+
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
219+
tabularOnelineClauses,
220+
},
221+
};

0 commit comments

Comments
 (0)