Skip to content

Commit

Permalink
Change formatting of CREATE TABLE (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
nene authored Dec 6, 2023
2 parents d9db4ca + a6cf455 commit b64a3e9
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 82 deletions.
3 changes: 2 additions & 1 deletion src/languages/bigquery/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const reservedClauses = expandPhrases([
'UPDATE SET',
// Data definition, https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
'CREATE [OR REPLACE] [MATERIALIZED] VIEW [IF NOT EXISTS]',
'CREATE [OR REPLACE] [TEMP|TEMPORARY|SNAPSHOT|EXTERNAL] TABLE [IF NOT EXISTS]',

'CLUSTER BY',
'FOR SYSTEM_TIME AS OF', // CREATE SNAPSHOT TABLE
Expand All @@ -42,6 +41,8 @@ const reservedClauses = expandPhrases([
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [TEMP|TEMPORARY|SNAPSHOT|EXTERNAL] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/db2/db2.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ const reservedClauses = expandPhrases([
'INSERT',
// Data definition
'CREATE [OR REPLACE] VIEW',
'CREATE [GLOBAL TEMPORARY | EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | EXTERNAL] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/db2i/db2i.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ const reservedClauses = expandPhrases([
'DELETE',
'INSERT',
// Data definition - table
'CREATE [OR REPLACE] TABLE',
'FOR SYSTEM NAME',
// Data definition - view
'CREATE [OR REPLACE] [RECURSIVE] VIEW',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] TABLE',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/hive/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ const reservedClauses = expandPhrases([
'[OVERWRITE] INTO TABLE',
// Data definition
'CREATE [MATERIALIZED] VIEW [IF NOT EXISTS]',
'CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/mariadb/mariadb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
'CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS]',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE [LOW_PRIORITY] [IGNORE]',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/mysql/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [OR REPLACE] [SQL SECURITY DEFINER | SQL SECURITY INVOKER] VIEW [IF NOT EXISTS]',
'CREATE [TEMPORARY] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE [LOW_PRIORITY] [IGNORE]',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/plsql/plsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const reservedClauses = expandPhrases([
// Data definition
'CREATE [OR REPLACE] [NO FORCE | FORCE] [EDITIONING | EDITIONABLE | EDITIONABLE EDITIONING | NONEDITIONABLE] VIEW',
'CREATE MATERIALIZED VIEW',
'CREATE [GLOBAL TEMPORARY | PRIVATE TEMPORARY | SHARDED | DUPLICATED | IMMUTABLE BLOCKCHAIN | BLOCKCHAIN | IMMUTABLE] TABLE',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | PRIVATE TEMPORARY | SHARDED | DUPLICATED | IMMUTABLE BLOCKCHAIN | BLOCKCHAIN | IMMUTABLE] TABLE',
// - update:
'UPDATE [ONLY]',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ const reservedClauses = expandPhrases([
// Data definition
'CREATE [OR REPLACE] [TEMP | TEMPORARY] [RECURSIVE] VIEW',
'CREATE MATERIALIZED VIEW [IF NOT EXISTS]',
'CREATE [GLOBAL | LOCAL] [TEMPORARY | TEMP | UNLOGGED] TABLE [IF NOT EXISTS]',
// other
'RETURNING',
]);

const onelineClauses = expandPhrases([
// - create
'CREATE [GLOBAL | LOCAL] [TEMPORARY | TEMP | UNLOGGED] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE [ONLY]',
'WHERE CURRENT OF',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/redshift/redshift.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [OR REPLACE | MATERIALIZED] VIEW',
'CREATE [TEMPORARY | TEMP | LOCAL TEMPORARY | LOCAL TEMP] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY | TEMP | LOCAL TEMPORARY | LOCAL TEMP] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/singlestoredb/singlestoredb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE VIEW',
'CREATE [ROWSTORE] [REFERENCE | TEMPORARY | GLOBAL TEMPORARY] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [TEMPORARY] PROCEDURE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [EXTERNAL] FUNCTION',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [ROWSTORE] [REFERENCE | TEMPORARY | GLOBAL TEMPORARY] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
5 changes: 3 additions & 2 deletions src/languages/snowflake/snowflake.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const reservedClauses = expandPhrases([
// - view
'CREATE [OR REPLACE] [SECURE] [RECURSIVE] VIEW [IF NOT EXISTS]',
// - create/drop/merge table
'CREATE [OR REPLACE] [VOLATILE] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [LOCAL | GLOBAL] {TEMP|TEMPORARY} TABLE [IF NOT EXISTS]',
'CLUSTER BY',
'[WITH] {MASKING POLICY | TAG | ROW ACCESS POLICY}',
'COPY GRANTS',
Expand All @@ -42,6 +40,9 @@ const reservedClauses = expandPhrases([
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [OR REPLACE] [VOLATILE] TABLE [IF NOT EXISTS]',
'CREATE [OR REPLACE] [LOCAL | GLOBAL] {TEMP|TEMPORARY} TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/spark/spark.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ const reservedClauses = expandPhrases([
'[OVERWRITE] INTO TABLE',
// Data definition
'CREATE [OR REPLACE] [GLOBAL TEMPORARY | TEMPORARY] VIEW [IF NOT EXISTS]',
'CREATE [EXTERNAL] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [EXTERNAL] TABLE [IF NOT EXISTS]',
// - drop table:
'DROP TABLE [IF EXISTS]',
// - alter table:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/sql/sql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [RECURSIVE] VIEW',
'CREATE [GLOBAL TEMPORARY | LOCAL TEMPORARY] TABLE',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [GLOBAL TEMPORARY | LOCAL TEMPORARY] TABLE',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/sqlite/sqlite.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [TEMPORARY | TEMP] VIEW [IF NOT EXISTS]',
'CREATE [TEMPORARY | TEMP] TABLE [IF NOT EXISTS]',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE [TEMPORARY | TEMP] TABLE [IF NOT EXISTS]',
// - update:
'UPDATE [OR ABORT | OR FAIL | OR IGNORE | OR REPLACE | OR ROLLBACK]',
// - insert:
Expand Down
3 changes: 2 additions & 1 deletion src/languages/transactsql/transactsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const reservedClauses = expandPhrases([
'UPDATE SET',
// Data definition
'CREATE [OR ALTER] [MATERIALIZED] VIEW',
'CREATE TABLE',
'CREATE [OR ALTER] {PROC | PROCEDURE}',
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE TABLE',
// - update:
'UPDATE',
'WHERE CURRENT OF',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/trino/trino.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const reservedClauses = expandPhrases([
'SET',
// Data definition
'CREATE [OR REPLACE] [MATERIALIZED] VIEW',
'CREATE TABLE [IF NOT EXISTS]',
// MATCH_RECOGNIZE
'MATCH_RECOGNIZE',
'MEASURES',
Expand All @@ -40,6 +39,8 @@ const reservedClauses = expandPhrases([
]);

const onelineClauses = expandPhrases([
// - create:
'CREATE TABLE [IF NOT EXISTS]',
// - update:
'UPDATE',
// - delete:
Expand Down
3 changes: 1 addition & 2 deletions test/behavesLikeMariaDbFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ export default function behavesLikeMariaDbFormatter(format: FormatFn) {
{ keywordCase: 'upper' }
)
).toBe(dedent`
CREATE TABLE
account (id INT comment 'the most important column');
CREATE TABLE account (id INT comment 'the most important column');
SELECT
*
Expand Down
3 changes: 1 addition & 2 deletions test/bigquery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ describe('BigQueryFormatter', () => {
uris = ['gs://bucket/path1.csv']
)`;
const expected = dedent`
CREATE EXTERNAL TABLE
dataset.CsvTable
CREATE EXTERNAL TABLE dataset.CsvTable
WITH PARTITION COLUMNS
(field_1 STRING, field_2 INT64) OPTIONS(format = 'CSV', uris = ['gs://bucket/path1.csv'])`;
expect(format(input)).toBe(expected);
Expand Down
9 changes: 4 additions & 5 deletions test/features/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ export default function supportsConstraints(format: FormatFn, actions: string[])
);
`)
).toBe(dedent`
CREATE TABLE
foo (
update_time datetime ON UPDATE ${action},
delete_time datetime ON DELETE ${action},
);
CREATE TABLE foo (
update_time datetime ON UPDATE ${action},
delete_time datetime ON DELETE ${action},
);
`);
});
});
Expand Down
34 changes: 14 additions & 20 deletions test/features/createTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export default function supportsCreateTable(
) {
it('formats short CREATE TABLE', () => {
expect(format('CREATE TABLE tbl (a INT PRIMARY KEY, b TEXT);')).toBe(dedent`
CREATE TABLE
tbl (a INT PRIMARY KEY, b TEXT);
CREATE TABLE tbl (a INT PRIMARY KEY, b TEXT);
`);
});

Expand All @@ -26,30 +25,27 @@ export default function supportsCreateTable(
expect(
format('CREATE TABLE tbl (a INT PRIMARY KEY, b TEXT, c INT NOT NULL, doggie INT NOT NULL);')
).toBe(dedent`
CREATE TABLE
tbl (
a INT PRIMARY KEY,
b TEXT,
c INT NOT NULL,
doggie INT NOT NULL
);
CREATE TABLE tbl (
a INT PRIMARY KEY,
b TEXT,
c INT NOT NULL,
doggie INT NOT NULL
);
`);
});

if (orReplace) {
it('formats short CREATE OR REPLACE TABLE', () => {
expect(format('CREATE OR REPLACE TABLE tbl (a INT PRIMARY KEY, b TEXT);')).toBe(dedent`
CREATE OR REPLACE TABLE
tbl (a INT PRIMARY KEY, b TEXT);
CREATE OR REPLACE TABLE tbl (a INT PRIMARY KEY, b TEXT);
`);
});
}

if (ifNotExists) {
it('formats short CREATE TABLE IF NOT EXISTS', () => {
expect(format('CREATE TABLE IF NOT EXISTS tbl (a INT PRIMARY KEY, b TEXT);')).toBe(dedent`
CREATE TABLE IF NOT EXISTS
tbl (a INT PRIMARY KEY, b TEXT);
CREATE TABLE IF NOT EXISTS tbl (a INT PRIMARY KEY, b TEXT);
`);
});
}
Expand All @@ -59,20 +55,18 @@ export default function supportsCreateTable(
expect(
format(`CREATE TABLE tbl (a INT COMMENT 'Hello world!', b TEXT COMMENT 'Here we are!');`)
).toBe(dedent`
CREATE TABLE
tbl (
a INT COMMENT 'Hello world!',
b TEXT COMMENT 'Here we are!'
);
CREATE TABLE tbl (
a INT COMMENT 'Hello world!',
b TEXT COMMENT 'Here we are!'
);
`);
});
}

if (tableComment) {
it('formats short CREATE TABLE with comment', () => {
expect(format(`CREATE TABLE tbl (a INT, b TEXT) COMMENT = 'Hello, world!';`)).toBe(dedent`
CREATE TABLE
tbl (a INT, b TEXT) COMMENT = 'Hello, world!';
CREATE TABLE tbl (a INT, b TEXT) COMMENT = 'Hello, world!';
`);
});
}
Expand Down
3 changes: 1 addition & 2 deletions test/features/createView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export default function supportsCreateView(
if (ifNotExists) {
it('formats short CREATE TABLE IF NOT EXISTS', () => {
expect(format('CREATE TABLE IF NOT EXISTS tbl (a INT PRIMARY KEY, b TEXT);')).toBe(dedent`
CREATE TABLE IF NOT EXISTS
tbl (a INT PRIMARY KEY, b TEXT);
CREATE TABLE IF NOT EXISTS tbl (a INT PRIMARY KEY, b TEXT);
`);
});
}
Expand Down
13 changes: 6 additions & 7 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,12 @@ describe('PostgreSqlFormatter', () => {
deleted_at TIME WITH TIME ZONE,
modified_at TIMESTAMP(0) WITH TIME ZONE);`)
).toBe(dedent`
CREATE TABLE
time_table (
id INT,
created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIME WITH TIME ZONE,
modified_at TIMESTAMP(0) WITH TIME ZONE
);
CREATE TABLE time_table (
id INT,
created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIME WITH TIME ZONE,
modified_at TIMESTAMP(0) WITH TIME ZONE
);
`);
});

Expand Down
Loading

0 comments on commit b64a3e9

Please sign in to comment.