Skip to content

Commit f43cc9a

Browse files
committed
Parse PostgreSQL array literals differently from BigQuery array literals
- ARRAY[1,2] in postgres will now result in array_literal_expr - ARRAY[1,2] in bigquery will result in typed_expr (as before) Fixes #127
1 parent be9277c commit f43cc9a

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/cst/Expr.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type Expr =
6464
| Variable
6565
| Parameter
6666
| TypedExpr
67+
| ArrayLiteralExpr
6768
| ArrayExpr
6869
| StructExpr;
6970

@@ -509,14 +510,20 @@ export interface Parameter extends BaseNode {
509510
text: string;
510511
}
511512

512-
// BigQuery, PostgreSQL
513-
// TODO: It feels awkward that PostgreSQL array literals have the ARRAY type attached to them
513+
// BigQuery
514514
export interface TypedExpr extends BaseNode {
515515
type: "typed_expr";
516516
dataType: DataType;
517517
expr: ArrayExpr | StructExpr;
518518
}
519519

520+
// PostgreSQL
521+
export interface ArrayLiteralExpr extends BaseNode {
522+
type: "array_literal_expr";
523+
arrayKw: Keyword<"ARRAY">;
524+
expr: ArrayExpr;
525+
}
526+
520527
// BigQuery, PostgreSQL
521528
export interface ArrayExpr extends BaseNode {
522529
type: "array_expr";

src/parser.pegjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7770,7 +7770,7 @@ primary
77707770
/ primary_paren_expr
77717771
/ paren$compound_select_stmt
77727772
/ &bigquery x:(typed_array_expr / array_expr / typed_struct_expr) { return x; }
7773-
/ &postgres x:typed_array_expr { return x; }
7773+
/ &postgres x:array_literal_expr { return x; }
77747774
/ cast_expr
77757775
/ &postgres x:(row_constructor / array_constructor) { return x; }
77767776
/ &sqlite e:raise_expr { return e; }
@@ -8669,6 +8669,15 @@ system_variable
86698669
* *
86708670
* ------------------------------------------------------------------------------------ *
86718671
*/
8672+
array_literal_expr
8673+
= kw:(ARRAY __) expr:array_expr {
8674+
return loc({
8675+
type: "array_literal_expr",
8676+
arrayKw: read(kw),
8677+
expr,
8678+
});
8679+
}
8680+
86728681
typed_array_expr
86738682
= type:(bigquery_array_type __) expr:array_expr {
86748683
return loc({

src/showNode/expr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const exprMap: FullTransformMap<string, AllExprNodes> = {
4444
interval_unit_range: (node) => show([node.fromUnit, node.toKw, node.toUnit]),
4545
interval_unit: (node) => show([node.unitKw]),
4646
typed_expr: (node) => show([node.dataType, node.expr]),
47+
array_literal_expr: (node) => show([node.arrayKw, node.expr]),
4748
array_expr: (node) => show(["[", node.expr, "]"]),
4849
struct_expr: (node) => show(["(", node.expr, ")"]),
4950
quantifier_expr: (node) => show([node.quantifierKw, node.expr]),

test/expr/array.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,37 @@ describe("array", () => {
8686
it("supports ARRAY() constructor", () => {
8787
testExprWc(`ARRAY(SELECT row from tbl)`);
8888
});
89+
90+
it("parses array literal", () => {
91+
expect(parseExpr(`ARRAY[1, 2]`)).toMatchInlineSnapshot(`
92+
{
93+
"arrayKw": {
94+
"name": "ARRAY",
95+
"text": "ARRAY",
96+
"type": "keyword",
97+
},
98+
"expr": {
99+
"expr": {
100+
"items": [
101+
{
102+
"text": "1",
103+
"type": "number_literal",
104+
"value": 1,
105+
},
106+
{
107+
"text": "2",
108+
"type": "number_literal",
109+
"value": 2,
110+
},
111+
],
112+
"type": "list_expr",
113+
},
114+
"type": "array_expr",
115+
},
116+
"type": "array_literal_expr",
117+
}
118+
`);
119+
});
89120
});
90121

91122
dialect(["mysql", "mariadb"], () => {

0 commit comments

Comments
 (0)