Skip to content

Commit 8953443

Browse files
authored
Merge PR #479: Cleanup for Snowflake tests and ALTER TABLE syntax tweaks
2 parents 117b248 + c2baad5 commit 8953443

File tree

11 files changed

+118
-56
lines changed

11 files changed

+118
-56
lines changed

src/languages/snowflake/snowflake.formatter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,23 @@ const reservedCommands = expandPhrases([
6161
'ALTER TABLE [IF EXISTS]',
6262
'RENAME TO',
6363
'SWAP WITH',
64-
'{SET | UNSET} [TAG | DATA_RETENTION_TIME_IN_DAYS | MAX_DATA_EXTENSION_TIME_IN_DAYS | CHANGE_TRACKING | DEFAULT_DDL_COLLATION | COMMENT]',
65-
'{ADD | DROP} ROW ACCESS POLICY',
66-
'DROP ALL ROW ACCESS POLICIES',
6764
'[SUSPEND | RESUME] RECLUSTER',
6865
'DROP CLUSTERING KEY',
6966
'ADD [COLUMN]',
7067
'RENAME COLUMN',
7168
'{ALTER | MODIFY} [COLUMN]',
7269
'DROP [COLUMN]',
70+
'{ADD | ALTER | MODIFY | DROP} [CONSTRAINT]',
71+
'RENAME CONSTRAINT',
72+
'{ADD | DROP} SEARCH OPTIMIZATION',
73+
'{SET | UNSET} [TAG]',
74+
'{ADD | DROP} ROW ACCESS POLICY',
75+
'DROP ALL ROW ACCESS POLICIES',
7376
'{SET | DROP} DEFAULT', // for alter column
74-
'SET NOT NULL', // for alter column
75-
'DROP NOT NULL', // for alter column
77+
'{SET | DROP} NOT NULL', // for alter column
7678
'[SET DATA] TYPE', // for alter column
7779
'[UNSET] COMMENT', // for alter column
78-
'{SET | UNSET} {MASKING POLICY | TAG}', // for alter column
79-
'[FOREIGN KEY] REFERENCES', // for alter column
80-
'{ADD | RENAME | ALTER | MODIFY | DROP} CONSTRAINT', // for alter column
81-
'{ADD | DROP} SEARCH OPTIMIZATION', // for alter column
80+
'{SET | UNSET} MASKING POLICY', // for alter column
8281

8382
// other
8483
// https://docs.snowflake.com/en/sql-reference/sql-all.html

test/bigquery.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('BigQueryFormatter', () => {
3131

3232
behavesLikeSqlFormatter(format);
3333
supportsComments(format, { hashComments: true });
34-
supportsCreateView(format, { orReplace: true, materialized: true });
34+
supportsCreateView(format, { orReplace: true, materialized: true, ifNotExists: true });
3535
supportsCreateTable(format, { orReplace: true, ifNotExists: true });
3636
supportsDropTable(format, { ifExists: true });
3737
supportsAlterTable(format, {

test/features/insertInto.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { FormatFn } from 'src/sqlFormatter';
44

55
interface InsertIntoConfig {
66
withoutInto?: boolean;
7-
withOverwrite?: boolean;
87
}
98

109
export default function supportsInsertInto(
1110
format: FormatFn,
12-
{ withoutInto, withOverwrite }: InsertIntoConfig = {}
11+
{ withoutInto }: InsertIntoConfig = {}
1312
) {
1413
it('formats simple INSERT INTO', () => {
1514
const result = format(
@@ -36,18 +35,4 @@ export default function supportsInsertInto(
3635
`);
3736
});
3837
}
39-
40-
if (withOverwrite) {
41-
it('formats INSERT with OVERWRITE', () => {
42-
const result = format(
43-
"INSERT OVERWRITE INTO Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');"
44-
);
45-
expect(result).toBe(dedent`
46-
INSERT OVERWRITE INTO
47-
Customers (ID, MoneyBalance, Address, City)
48-
VALUES
49-
(12, -123.4, 'Skagen 2111', 'Stv');
50-
`);
51-
});
52-
}
5338
}

test/features/strings.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type StringType =
1212
| "''-bs" // with backslash escaping
1313
| "U&''" // with repeated-quote escaping
1414
| "N''" // with escaping style depending on whether also ''-qq or ''-bs was specified
15-
| '$$' // no escaping
1615
| "X''" // no escaping
1716
| 'X""' // no escaping
1817
| "B''" // no escaping
@@ -113,15 +112,6 @@ export default function supportsStrings(format: FormatFn, stringTypes: StringTyp
113112
});
114113
}
115114

116-
if (stringTypes.includes('$$')) {
117-
it('supports quotes and dollar signs inside $$ string', () => {
118-
expect(format(`$$foo' JOIN"$bar$$`)).toBe(`$$foo' JOIN"$bar$$`);
119-
});
120-
121-
it('detects consecutive $$ strings as separate ones', () => {
122-
expect(format('$$foo$$$$bar$$')).toBe('$$foo$$ $$bar$$');
123-
});
124-
}
125115
if (stringTypes.includes("N''")) {
126116
it('supports T-SQL unicode strings', () => {
127117
expect(format("N'foo JOIN bar'")).toBe("N'foo JOIN bar'");

test/features/truncateTable.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import dedent from 'dedent-js';
33
import { FormatFn } from 'src/sqlFormatter';
44

55
interface TruncateTableConfig {
6-
ifExists?: boolean;
76
withoutTable?: boolean;
87
}
98

109
export default function supportsTruncateTable(
1110
format: FormatFn,
12-
{ ifExists, withoutTable }: TruncateTableConfig = {}
11+
{ withoutTable }: TruncateTableConfig = {}
1312
) {
1413
it('formats TRUNCATE TABLE statement', () => {
1514
const result = format('TRUNCATE TABLE Customers;');
@@ -19,16 +18,6 @@ export default function supportsTruncateTable(
1918
`);
2019
});
2120

22-
if (ifExists) {
23-
it('formats TRUNCATE TABLE IF EXISTS statments', () => {
24-
const result = format('TRUNCATE TABLE IF EXISTS Customers;');
25-
expect(result).toBe(dedent`
26-
TRUNCATE TABLE IF EXISTS
27-
Customers;
28-
`);
29-
});
30-
}
31-
3221
if (withoutTable) {
3322
it('formats TRUNCATE statement (without TABLE)', () => {
3423
const result = format('TRUNCATE Customers;');

test/hive.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('HiveFormatter', () => {
3030

3131
behavesLikeSqlFormatter(format);
3232
supportsComments(format);
33-
supportsCreateView(format, { materialized: true });
33+
supportsCreateView(format, { materialized: true, ifNotExists: true });
3434
supportsCreateTable(format, { ifNotExists: true });
3535
supportsDropTable(format, { ifExists: true });
3636
supportsAlterTable(format, { renameTo: true });

test/mariadb.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('MariaDbFormatter', () => {
3939
supportsCreateTable(format, { orReplace: true, ifNotExists: true });
4040
supportsConstraints(format, ['RESTRICT', 'CASCADE', 'SET NULL', 'NO ACTION', 'SET DEFAULT']);
4141
supportsParams(format, { positional: true });
42-
supportsCreateView(format, { orReplace: true });
42+
supportsCreateView(format, { orReplace: true, ifNotExists: true });
4343
supportsAlterTable(format, {
4444
addColumn: true,
4545
dropColumn: true,

test/postgresql.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('PostgreSqlFormatter', () => {
3232

3333
behavesLikeSqlFormatter(format);
3434
supportsComments(format, { nestedBlockComments: true });
35-
supportsCreateView(format, { orReplace: true, materialized: true });
35+
supportsCreateView(format, { orReplace: true, materialized: true, ifNotExists: true });
3636
supportsCreateTable(format, { ifNotExists: true });
3737
supportsDropTable(format, { ifExists: true });
3838
supportsConstraints(format, ['NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT']);

test/snowflake.test.ts

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ describe('SnowflakeFormatter', () => {
3636
supportsAlterTable(format, {
3737
addColumn: true,
3838
dropColumn: true,
39+
modify: true,
3940
renameTo: true,
4041
renameColumn: true,
4142
});
4243
supportsDeleteFrom(format);
43-
supportsInsertInto(format, { withOverwrite: true });
44+
supportsInsertInto(format);
4445
supportsUpdate(format);
45-
supportsTruncateTable(format, { withoutTable: true, ifExists: true });
46-
supportsStrings(format, ['$$', "''-bs", "''-qq"]);
46+
supportsTruncateTable(format, { withoutTable: true });
47+
supportsStrings(format, ["''-bs", "''-qq"]);
4748
supportsIdentifiers(format, [`""-qq`]);
4849
supportsBetween(format);
4950
// ':' and '::' are tested later, since they should always be dense
@@ -80,4 +81,102 @@ describe('SnowflakeFormatter', () => {
8081
2::numeric AS foo;
8182
`);
8283
});
84+
85+
it('supports $$-quoted strings', () => {
86+
expect(format(`SELECT $$foo' JOIN"$bar$$, $$foo$$$$bar$$`)).toBe(dedent`
87+
SELECT
88+
$$foo' JOIN"$bar$$,
89+
$$foo$$ $$bar$$
90+
`);
91+
});
92+
93+
it('formats ALTER TABLE ... ALTER COLUMN', () => {
94+
expect(
95+
format(
96+
`ALTER TABLE t ALTER COLUMN foo SET DATA TYPE VARCHAR;
97+
ALTER TABLE t ALTER COLUMN foo SET DEFAULT 5;
98+
ALTER TABLE t ALTER COLUMN foo DROP DEFAULT;
99+
ALTER TABLE t ALTER COLUMN foo SET NOT NULL;
100+
ALTER TABLE t ALTER COLUMN foo DROP NOT NULL;
101+
ALTER TABLE t ALTER COLUMN foo COMMENT 'blah';
102+
ALTER TABLE t ALTER COLUMN foo UNSET COMMENT;
103+
ALTER TABLE t ALTER COLUMN foo SET MASKING POLICY polis;
104+
ALTER TABLE t ALTER COLUMN foo UNSET MASKING POLICY;
105+
ALTER TABLE t ALTER COLUMN foo SET TAG tname = 10;
106+
ALTER TABLE t ALTER COLUMN foo UNSET TAG tname;`
107+
)
108+
).toBe(dedent`
109+
ALTER TABLE
110+
t
111+
ALTER COLUMN
112+
foo
113+
SET DATA TYPE
114+
VARCHAR;
115+
116+
ALTER TABLE
117+
t
118+
ALTER COLUMN
119+
foo
120+
SET DEFAULT
121+
5;
122+
123+
ALTER TABLE
124+
t
125+
ALTER COLUMN
126+
foo
127+
DROP DEFAULT;
128+
129+
ALTER TABLE
130+
t
131+
ALTER COLUMN
132+
foo
133+
SET NOT NULL;
134+
135+
ALTER TABLE
136+
t
137+
ALTER COLUMN
138+
foo
139+
DROP NOT NULL;
140+
141+
ALTER TABLE
142+
t
143+
ALTER COLUMN
144+
foo
145+
COMMENT
146+
'blah';
147+
148+
ALTER TABLE
149+
t
150+
ALTER COLUMN
151+
foo
152+
UNSET COMMENT;
153+
154+
ALTER TABLE
155+
t
156+
ALTER COLUMN
157+
foo
158+
SET MASKING POLICY
159+
polis;
160+
161+
ALTER TABLE
162+
t
163+
ALTER COLUMN
164+
foo
165+
UNSET MASKING POLICY;
166+
167+
ALTER TABLE
168+
t
169+
ALTER COLUMN
170+
foo
171+
SET TAG
172+
tname = 10;
173+
174+
ALTER TABLE
175+
t
176+
ALTER COLUMN
177+
foo
178+
UNSET TAG
179+
tname;
180+
`);
181+
});
83182
});

test/spark.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('SparkFormatter', () => {
2525

2626
behavesLikeSqlFormatter(format);
2727
supportsComments(format);
28-
supportsCreateView(format, { orReplace: true });
28+
supportsCreateView(format, { orReplace: true, ifNotExists: true });
2929
supportsCreateTable(format, { ifNotExists: true });
3030
supportsDropTable(format, { ifExists: true });
3131
supportsAlterTable(format, {

0 commit comments

Comments
 (0)