Skip to content

Commit

Permalink
Support ARRAY() constructor syntax in PostgreSQL
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
nene committed Feb 28, 2024
1 parent cff863b commit ea7c879
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cst/Expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type Expr =
| BetweenExpr
| CaseExpr
| RowConstructor
| ArrayConstructor
| IntervalExpr
| StringWithCharset
| QuantifierExpr
Expand Down Expand Up @@ -343,6 +344,13 @@ export interface RowConstructor extends BaseNode {
row: ParenExpr<ListExpr<Expr | Default>>;
}

// PostgreSQL
export interface ArrayConstructor extends BaseNode {
type: "array_constructor";
arrayKw: Keyword<"ARRAY">;
expr: ParenExpr<SubSelect>;
}

// MySQL, MariaDB, BigQuery
export interface IntervalExpr extends BaseNode {
type: "interval_expr";
Expand Down
13 changes: 11 additions & 2 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,14 +1501,23 @@ values_row
/ &mysql list:(paren$list$expr_or_default / row_constructor) { return list; }

row_constructor
= kw:(ROW __) row:paren$list$expr_or_default {
= kw:(ROW __) row:paren$compound_select_stmt {
return loc({
type: "row_constructor",
rowKw: read(kw),
row,
});
}

array_constructor
= kw:(ARRAY __) expr:select_stmt {
return loc({
type: "array_constructor",
arrayKw: read(kw),
expr: expr,
});
}

expr_or_default
= expr / default

Expand Down Expand Up @@ -6295,7 +6304,7 @@ primary
/ &bigquery x:(typed_array_expr / array_expr / typed_struct_expr) { return x; }
/ &postgres x:typed_array_expr { return x; }
/ cast_expr
/ &postgres x:row_constructor { return x; }
/ &postgres x:(row_constructor / array_constructor) { return x; }
/ &sqlite e:raise_expr { return e; }
/ (&mysql / &bigquery / &postgres) e:extract_expr { return e; }
/ case_expr
Expand Down
1 change: 1 addition & 0 deletions src/showNode/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const exprMap: FullTransformMap<string, AllExprNodes> = {
cast_format: (node) => show([node.formatKw, node.string, node.timezone]),
cast_format_timezone: (node) => show([node.atTimeZoneKw, node.timezone]),
row_constructor: (node) => show([node.rowKw, node.row]),
array_constructor: (node) => show([node.arrayKw, node.expr]),
raise_expr: (node) => show([node.raiseKw, node.args]),
raise_expr_type: (node) => show(node.typeKw),
extract_expr: (node) => show([node.extractKw, node.args]),
Expand Down
4 changes: 4 additions & 0 deletions test/expr/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ describe("array", () => {
it("supports empty ARRAY[]", () => {
testExprWc(`ARRAY[]`);
});

it("supports ARRAY() constructor", () => {
testExprWc(`ARRAY(SELECT row from tbl)`);
});
});

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

0 comments on commit ea7c879

Please sign in to comment.