Skip to content

Commit 0476c6d

Browse files
committed
support limit and offset
1 parent 3d9e7fd commit 0476c6d

6 files changed

Lines changed: 33 additions & 27 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "1.10.2",
3+
"version": "2.0.0-beta",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "index.d.ts",

pegjs/bigquery.pegjs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,6 @@ struct_value
331331
return `${a[0].toLowerCase()} ${k.toLowerCase()}`
332332
}
333333

334-
expr_alias_list
335-
= head:expr_alias tail:(__ COMMA __ expr_alias)* {
336-
return createList(head, tail);
337-
}
338-
339334
expr_alias
340335
= e:expr __ alias:alias_clause? {
341336
return { expr: e, as:alias };
@@ -513,17 +508,6 @@ where_clause
513508
group_by_clause
514509
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
515510

516-
column_ref_index
517-
= l:column_ref_list
518-
/ l: literal_list {
519-
return l
520-
}
521-
522-
column_ref_list
523-
= head:column_ref tail:(__ COMMA __ column_ref)* {
524-
return createList(head, tail);
525-
}
526-
527511
having_clause
528512
= KW_HAVING __ e:expr { return e; }
529513

@@ -626,12 +610,12 @@ expr_list
626610

627611
expr
628612
= parentheses_list_expr
629-
/ array_expr
630613
/ struct_expr
631614
/ logic_operator_expr // support concatenation operator || and &&
632615
/ unary_expr
633616
/ or_expr
634617
/ select_stmt
618+
/ array_expr
635619

636620
parentheses_list_expr
637621
= head:parentheses_expr tail:(__ COMMA __ parentheses_expr)* {
@@ -643,7 +627,6 @@ parentheses_expr
643627
return c
644628
}
645629

646-
647630
array_expr
648631
= LBRAKE __ c:column_clause __ RBRAKE __ {
649632
return {
@@ -653,6 +636,15 @@ array_expr
653636
parentheses: true
654637
}
655638
}
639+
/ s:(array_type / KW_ARRAY)? LBRAKE __ c:literal_list __ RBRAKE __ {
640+
return {
641+
definition: s,
642+
array_path: c.map(l => ({ expr: l, as: null })),
643+
type: 'array',
644+
keyword: s && 'array',
645+
parentheses: true
646+
}
647+
}
656648
/ s:(array_type / KW_ARRAY)? __ LBRAKE __ c:expr __ RBRAKE __ {
657649
return {
658650
definition: s,

src/array-struct.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ function arrayStructValueToSQL(expr) {
55
const {
66
array_path: arrayPath,
77
expr_list: exprList,
8-
parentheses,
98
type,
109
} = expr
1110
switch (toUpper(type)) {
1211
case 'STRUCT':
1312
return `(${columnsToSQL(exprList)})`
1413
case 'ARRAY':
1514
if (exprList) return `[${exprList.map(col => `(${columnsToSQL(col)})`).filter(hasVal).join(', ')}]`
16-
if (arrayPath) return parentheses && `[${columnsToSQL(arrayPath)}]` || columnsToSQL(arrayPath)
17-
break
15+
return `[${columnsToSQL(arrayPath)}]`
1816
default:
1917
return ''
2018
}

src/column.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ function columnsToSQL(columns, tables) {
106106

107107
else str = `${str}\`${column.as}\``
108108
}
109-
110109
return str
111110
})
112111
.join(', ')

src/over.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ function overToSQL(over) {
77
const {
88
as_window_specification: asWindowSpec,
99
orderby,
10-
parentheses,
1110
partitionby,
1211
type,
1312
} = over
1413
if (toUpper(type) === 'WINDOW') {
1514
const windowSQL = asWindowSpecToSQL(asWindowSpec)
16-
return parentheses && `OVER (${windowSQL})` || `OVER ${windowSQL}`
15+
return `OVER ${windowSQL}`
1716
}
1817
const partition = orderOrPartitionByToSQL(partitionby, 'partition by')
1918
const order = orderOrPartitionByToSQL(orderby, 'order by')

test/bigquery.spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ describe('BigQuery', () => {
244244
title: 'select window clause list',
245245
sql: [
246246
`SELECT item, purchases, category, LAST_VALUE(item)
247-
OVER (d) AS most_popular
247+
OVER d AS most_popular
248248
FROM Produce
249249
WINDOW
250250
a AS (PARTITION BY category),
251251
b AS (a ORDER BY purchases),
252252
c AS (b ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING),
253253
d AS (c)`,
254-
'SELECT item, purchases, category, LAST_VALUE(item) OVER (d) AS most_popular FROM Produce WINDOW a AS (PARTITION BY category), b AS (a ORDER BY purchases ASC), c AS (b ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING), d AS (c)'
254+
'SELECT item, purchases, category, LAST_VALUE(item) OVER d AS most_popular FROM Produce WINDOW a AS (PARTITION BY category), b AS (a ORDER BY purchases ASC), c AS (b ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING), d AS (c)'
255255
]
256256
},
257257
{
@@ -267,6 +267,24 @@ describe('BigQuery', () => {
267267
'SELECT item, purchases, category, LAST_VALUE(item) OVER (c ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) AS most_popular FROM Produce WINDOW a AS (PARTITION BY category), b AS (a ORDER BY purchases ASC), c AS b'
268268
]
269269
},
270+
{
271+
title: 'select unnest array limit',
272+
sql: [
273+
`SELECT *
274+
FROM UNNEST(ARRAY<STRING>['a', 'b', 'c', 'd', 'e']) AS letter
275+
ORDER BY letter ASC LIMIT 2`,
276+
"SELECT * FROM UNNEST (ARRAY<STRING>['a', 'b', 'c', 'd', 'e']) AS letter ORDER BY letter ASC LIMIT 2"
277+
]
278+
},
279+
{
280+
title: 'select unnest array limit and offset',
281+
sql: [
282+
`SELECT *
283+
FROM UNNEST(ARRAY<STRING>['a', 'b', 'c', 'd', 'e']) AS letter
284+
ORDER BY letter ASC LIMIT 2 OFFSET 1`,
285+
"SELECT * FROM UNNEST (ARRAY<STRING>['a', 'b', 'c', 'd', 'e']) AS letter ORDER BY letter ASC LIMIT 2 OFFSET 1"
286+
]
287+
},
270288
]
271289

272290
SQL_LIST.forEach(sqlInfo => {

0 commit comments

Comments
 (0)