Skip to content

Commit 2313b9f

Browse files
authored
Merge PR #281: Fix BigQuery STRUCT formatting
2 parents e132c30 + 7e67cb2 commit 2313b9f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/languages/bigquery.formatter.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,9 @@ function combineParameterizedTypes(tokens: Token[]) {
892892
const endIndex = findClosingAngleBracketIndex(tokens, i + 1);
893893
const typeDefTokens = tokens.slice(i, endIndex + 1);
894894
processed.push({
895-
...token,
896-
value: typeDefTokens.map(t => t.value).join(''),
897-
text: typeDefTokens.map(t => t.text).join(''),
895+
type: TokenType.IDENT,
896+
value: typeDefTokens.map(formatTypeDefToken('value')).join(''),
897+
text: typeDefTokens.map(formatTypeDefToken('text')).join(''),
898898
});
899899
i = endIndex;
900900
} else {
@@ -904,6 +904,16 @@ function combineParameterizedTypes(tokens: Token[]) {
904904
return processed;
905905
}
906906

907+
const formatTypeDefToken =
908+
(key: 'text' | 'value') =>
909+
(token: Token): string => {
910+
if (token.type === TokenType.IDENT || token.value === ',') {
911+
return token[key] + ' ';
912+
} else {
913+
return token[key];
914+
}
915+
};
916+
907917
function findClosingAngleBracketIndex(tokens: Token[], startIndex: number): number {
908918
let level = 0;
909919
for (let i = startIndex; i < tokens.length; i++) {

test/bigquery.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ describe('BigQueryFormatter', () => {
121121
`);
122122
});
123123

124+
// Issue #279
125+
it('supports parametric STRUCT with named fields', () => {
126+
expect(format('SELECT STRUCT<y INT64, z STRING>(1,"foo"), STRUCT<arr ARRAY<INT64>>([1,2,3]);'))
127+
.toBe(dedent`
128+
SELECT
129+
STRUCT<y INT64, z STRING>(1, "foo"),
130+
STRUCT<arr ARRAY<INT64>>([1, 2, 3]);
131+
`);
132+
});
133+
134+
it('supports uppercasing of STRUCT', () => {
135+
expect(format('select struct<Nr int64, myName string>(1,"foo");', { keywordCase: 'upper' }))
136+
.toBe(dedent`
137+
SELECT
138+
STRUCT<Nr INT64, myName STRING>(1, "foo");
139+
`);
140+
});
141+
142+
// XXX: This is hard to achieve with our current type-parameter processing hack.
143+
// At least we're preserving the case of identifier names here,
144+
// and lowercasing is luckily less used than uppercasing.
145+
it('does not support lowercasing of STRUCT', () => {
146+
expect(format('SELECT STRUCT<Nr INT64, myName STRING>(1,"foo");', { keywordCase: 'lower' }))
147+
.toBe(dedent`
148+
select
149+
STRUCT<Nr INT64, myName STRING>(1, "foo");
150+
`);
151+
});
152+
124153
it('supports parameterised types', () => {
125154
const result = format(
126155
`

0 commit comments

Comments
 (0)