Skip to content

Commit 9233e6c

Browse files
Merge pull request #2524 from taozhi8833998/feat-func-call-array-pg
feat: support func call in array index in pg
2 parents 2dca390 + 3910461 commit 9233e6c

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

ast/postgresql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ export type column_list_items = column_list_item[];
939939

940940
export type column_clause = 'ALL' | '*' | column_list_item[] | column_list_items;
941941

942-
export type array_index = { brackets: boolean, number: number };
942+
export type array_index = { brackets: boolean, index: literal_numeric | literal_string | func_call };
943943

944944
export type array_index_list = array_index[];
945945

pegjs/postgresql.pegjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,8 +3425,8 @@ column_clause
34253425
/ column_list_items
34263426

34273427
array_index
3428-
= LBRAKE __ n:(literal_numeric / literal_string) __ RBRAKE {
3429-
// => { brackets: boolean, number: number }
3428+
= LBRAKE __ n:(literal_numeric / literal_string / func_call) __ RBRAKE {
3429+
// => { brackets: boolean, index: literal_numeric | literal_string | func_call }
34303430
return {
34313431
brackets: true,
34323432
index: n

src/column.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function arrayIndexToSQL(arrayIndexList) {
2929
if (!arrayIndexList || arrayIndexList.length === 0) return ''
3030
const result = []
3131
for (const arrayIndex of arrayIndexList) {
32-
let arrayIndexStr = arrayIndex.brackets ? `[${literalToSQL(arrayIndex.index)}]` : `${arrayIndex.notation}${literalToSQL(arrayIndex.index)}`
32+
let arrayIndexStr = arrayIndex.brackets ? `[${exprToSQL(arrayIndex.index)}]` : `${arrayIndex.notation}${exprToSQL(arrayIndex.index)}`
3333
if (arrayIndex.property) arrayIndexStr = `${arrayIndexStr}.${literalToSQL(arrayIndex.property)}`
3434
result.push(arrayIndexStr)
3535
}

test/postgres.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,26 @@ describe('Postgres', () => {
18931893
'CREATE TABLE "sal_emp" (name TEXT, pay_by_quarter INTEGER[], schedule TEXT[][])'
18941894
]
18951895
},
1896+
{
1897+
title: 'bracket array index with func call',
1898+
sql: [
1899+
`SELECT
1900+
p.description AS description,
1901+
p.invoice_date AS invoice_date
1902+
FROM anonymized_schema.payments p
1903+
JOIN anonymized_schema.entities e
1904+
ON p.entity_id = e.entity_id
1905+
LEFT JOIN anonymized_schema.entities pe
1906+
ON p.parent_entity_id = pe.entity_id
1907+
LEFT JOIN anonymized_schema.entities ppe
1908+
ON pe.parent_ids[ array_upper(pe.parent_ids, 1) ] = ppe.entity_id
1909+
WHERE e.entity_type IN ('A', 'B')
1910+
AND p.invoice_date >= '2025-08-01'
1911+
AND p.invoice_date < '2025-09-01'
1912+
AND CAST(p.amount AS NUMERIC) > 0`,
1913+
`SELECT "p".description AS "description", "p".invoice_date AS "invoice_date" FROM "anonymized_schema"."payments" AS "p" INNER JOIN "anonymized_schema"."entities" AS "e" ON "p".entity_id = "e".entity_id LEFT JOIN "anonymized_schema"."entities" AS "pe" ON "p".parent_entity_id = "pe".entity_id LEFT JOIN "anonymized_schema"."entities" AS "ppe" ON "pe".parent_ids[array_upper("pe".parent_ids, 1)] = "ppe".entity_id WHERE "e".entity_type IN ('A', 'B') AND "p".invoice_date >= '2025-08-01' AND "p".invoice_date < '2025-09-01' AND CAST("p".amount AS NUMERIC) > 0`
1914+
]
1915+
},
18961916
]
18971917
function neatlyNestTestedSQL(sqlList){
18981918
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)