Skip to content

Commit a39d881

Browse files
authored
Merge PR #276: Support arrays in PostgreSQL
2 parents 16c9091 + 914401d commit a39d881

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

sql/arrays-and-maps.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Array literals `[1, "two", 3]`. Supported by:
99
- [BigQuery][bigquery-literals].
1010
- [N1QL][n1ql-literals].
1111

12+
Array literals `ARRAY[1, 2, 3]`. Supported by:
13+
14+
- [BigQuery][bigquery-literals].
15+
- [PostgreSQL][postgres-literals].
16+
1217
Map literals in JSON style `{"foo": 1, "bar": "John"}`. Supported by:
1318

1419
- [N1QL][n1ql-literals].
@@ -22,9 +27,17 @@ Supported by:
2227
- [Hive][]
2328
- [Spark][]
2429
- [N1QL][]
30+
- [PostgreSQL][]
31+
32+
Array subscript operator `arr[OFFSET(5)]`. Supported by:
33+
34+
- [BigQuery][]
2535

36+
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
2637
[hive]: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-OperatorsonComplexTypes
2738
[spark]: https://stackoverflow.com/questions/34916038/sparksql-sql-syntax-for-nth-item-in-array
2839
[n1ql-literals]: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/datatypes.html#arrays
2940
[n1ql]: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/nestedops.html#field-selection
41+
[postgresql]: https://www.postgresql.org/docs/current/arrays.html#ARRAYS-ACCESSING
3042
[bigquery-literals]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#array_literals
43+
[postgres-literals]: https://www.postgresql.org/docs/current/arrays.html

src/languages/postgresql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,8 @@ export default class PostgreSqlFormatter extends Formatter {
16751675
reservedJoins,
16761676
reservedDependentClauses,
16771677
reservedKeywords: dedupe([...Object.values(reservedFunctions).flat(), ...reservedKeywords]),
1678+
openParens: ['(', '['],
1679+
closeParens: [')', ']'],
16781680
stringTypes: [{ quote: "''", prefixes: ['U&', 'E', 'X', 'B'] }, '$$'],
16791681
identTypes: [{ quote: '""', prefixes: ['U&'] }],
16801682
identChars: { rest: '$' },

test/postgresql.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import supportsDeleteFrom from './features/deleteFrom';
1717
import supportsComments from './features/comments';
1818
import supportsIdentifiers from './features/identifiers';
1919
import supportsParams from './options/param';
20+
import supportsArrayAndMapAccessors from './features/arrayAndMapAccessors';
2021

2122
describe('PostgreSqlFormatter', () => {
2223
const language = 'postgresql';
@@ -26,6 +27,7 @@ describe('PostgreSqlFormatter', () => {
2627
supportsComments(format);
2728
supportsCreateTable(format);
2829
supportsConstraints(format);
30+
supportsArrayAndMapAccessors(format);
2931
supportsAlterTable(format);
3032
supportsDeleteFrom(format);
3133
supportsStrings(format, ["''", "U&''", "X''"]);
@@ -79,4 +81,11 @@ describe('PostgreSqlFormatter', () => {
7981
$$update$$
8082
`);
8183
});
84+
85+
it('supports ARRAY literals', () => {
86+
expect(format('SELECT ARRAY[1, 2, 3]')).toBe(dedent`
87+
SELECT
88+
ARRAY[1, 2, 3]
89+
`);
90+
});
8291
});

0 commit comments

Comments
 (0)