Skip to content

Commit f9575c0

Browse files
committed
Support ":" as struct key-value separator
Instead of the array-slice operator (which for now doesn't quite work.
1 parent d1dfc3b commit f9575c0

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

src/languages/duckdb/duckdb.formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export const duckdb: DialectOptions = {
209209
],
210210
},
211211
formatOptions: {
212-
alwaysDenseOperators: ['::', ':'],
212+
alwaysDenseOperators: ['::'],
213213
onelineClauses: [...standardOnelineClauses, ...tabularOnelineClauses],
214214
tabularOnelineClauses,
215215
},

test/behavesLikePostgresqlFormatter.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ export default function behavesLikePostgresqlFormatter(format: FormatFn) {
6464
`);
6565
});
6666

67-
// Regression test for issue #624
68-
it('supports array slice operator', () => {
69-
expect(format('SELECT foo[:5], bar[1:], baz[1:5], zap[:];')).toBe(dedent`
70-
SELECT
71-
foo[:5],
72-
bar[1:],
73-
baz[1:5],
74-
zap[:];
75-
`);
76-
});
77-
7867
it('formats ALTER TABLE ... ALTER COLUMN', () => {
7968
expect(
8069
format(

test/duckdb.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,65 @@ describe('DuckDBFormatter', () => {
101101
supportsParams(format, { positional: true, numbered: ['$'], quoted: ['$""'] });
102102
supportsLimiting(format, { limit: true, offset: true });
103103

104+
it('formats {} struct literal (string keys)', () => {
105+
expect(format("SELECT {'id':1,'type':'Tarzan'} AS obj;")).toBe(dedent`
106+
SELECT
107+
{'id': 1, 'type': 'Tarzan'} AS obj;
108+
`);
109+
});
110+
111+
it('formats {} struct literal (identifier keys)', () => {
112+
expect(format("SELECT {id:1,type:'Tarzan'} AS obj;")).toBe(dedent`
113+
SELECT
114+
{id: 1, type: 'Tarzan'} AS obj;
115+
`);
116+
});
117+
118+
it('formats {} struct literal (quoted identifier keys)', () => {
119+
expect(format(`SELECT {"id":1,"type":'Tarzan'} AS obj;`)).toBe(dedent`
120+
SELECT
121+
{"id": 1, "type": 'Tarzan'} AS obj;
122+
`);
123+
});
124+
125+
it('formats large struct and list literals', () => {
126+
const result = format(`
127+
INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id': 1, 'type': 'Tarzan',
128+
'array': [123456789, 123456789, 123456789, 123456789, 123456789], 'hello': 'world'});
129+
`);
130+
expect(result).toBe(dedent`
131+
INSERT INTO
132+
heroes (KEY, VALUE)
133+
VALUES
134+
(
135+
'123',
136+
{
137+
'id': 1,
138+
'type': 'Tarzan',
139+
'array': [
140+
123456789,
141+
123456789,
142+
123456789,
143+
123456789,
144+
123456789
145+
],
146+
'hello': 'world'
147+
}
148+
);
149+
`);
150+
});
151+
152+
// TODO: This currently conflicts with ":"-operator in struct literals
153+
it.skip('supports array slice operator', () => {
154+
expect(format('SELECT foo[:5], bar[1:], baz[1:5], zap[:];')).toBe(dedent`
155+
SELECT
156+
foo[:5],
157+
bar[1:],
158+
baz[1:5],
159+
zap[:];
160+
`);
161+
});
162+
104163
it('formats TIMESTAMP WITH TIME ZONE syntax', () => {
105164
expect(
106165
format(`

test/postgresql.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ describe('PostgreSqlFormatter', () => {
139139
supportsLimiting(format, { limit: true, offset: true, fetchFirst: true, fetchNext: true });
140140
supportsDataTypeCase(format);
141141

142+
// Regression test for issue #624
143+
it('supports array slice operator', () => {
144+
expect(format('SELECT foo[:5], bar[1:], baz[1:5], zap[:];')).toBe(dedent`
145+
SELECT
146+
foo[:5],
147+
bar[1:],
148+
baz[1:5],
149+
zap[:];
150+
`);
151+
});
152+
142153
// Regression test for issue #447
143154
it('formats empty SELECT', () => {
144155
expect(format('SELECT;')).toBe(dedent`

0 commit comments

Comments
 (0)