Skip to content

Commit

Permalink
Merge pull request #1726 from taozhi8833998/fix-json-access-bigquery
Browse files Browse the repository at this point in the history
fix: json array access in where clause in bigquery
  • Loading branch information
taozhi8833998 authored Dec 18, 2023
2 parents b071138 + c9c5192 commit 239bdcd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-sql-parser",
"version": "4.16.0",
"version": "4.17.0",
"description": "simple node sql parser",
"main": "index.js",
"types": "types.d.ts",
Expand Down
19 changes: 16 additions & 3 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2320,14 +2320,27 @@ case_else = KW_ELSE __ result:expr {
}

column_ref
= tbl:column_without_kw col:(__ DOT __ column_without_kw)+ {
= tbl:column_without_kw col:(__ DOT __ column_without_kw)+ __ cof:(column_offset_expr_list __ (DOT __ column_without_kw)?)? {
const cols = col.map(c => c[3])
columnList.add(`select::${tbl}::${cols[0]}`)
const column = cof
? {
column: {
expr: {
type: 'column_ref',
table: null,
column: cols[0],
subFields: cols.slice(1)
},
offset: cof && cof[0],
suffix: cof && cof[2] && `.${cof[2][2]}`,
}
}
: { column: cols[0], subFields: cols.slice(1) }
return {
type: 'column_ref',
table: tbl,
column: cols[0],
subFields: cols.slice(1)
...column,
};
}
/ col:column {
Expand Down
4 changes: 2 additions & 2 deletions test/bigquery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,8 @@ describe('BigQuery', () => {
`WITH your_table AS (
SELECT [STRUCT(1 AS id, 'John' AS name), STRUCT(2 AS id, 'Jane' AS name)] AS some_array_column
)
SELECT some_array_column[SAFE_OFFSET(0)].id from your_table`,
"WITH your_table AS (SELECT [STRUCT(1 AS id, 'John' AS name), STRUCT(2 AS id, 'Jane' AS name)] AS some_array_column) SELECT some_array_column[SAFE_OFFSET(0)].id FROM your_table"
SELECT some_array_column[SAFE_OFFSET(0)].id from your_table where 1=1 and check_run.pull_requests[SAFE_OFFSET(0)].number = 6097 and check_run.status = 'completed' and check_run.output.title IS NOT NULL AND check_run.pull_requests[SAFE_OFFSET(0)].id is not null`,
"WITH your_table AS (SELECT [STRUCT(1 AS id, 'John' AS name), STRUCT(2 AS id, 'Jane' AS name)] AS some_array_column) SELECT some_array_column[SAFE_OFFSET(0)].id FROM your_table WHERE 1 = 1 AND check_run.pull_requests[SAFE_OFFSET(0)].number = 6097 AND check_run.status = 'completed' AND check_run.output.title IS NOT NULL AND check_run.pull_requests[SAFE_OFFSET(0)].id IS NOT NULL"
]
},
{
Expand Down

0 comments on commit 239bdcd

Please sign in to comment.