Skip to content

Commit

Permalink
fix: array sqlify not right in bigquery
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Dec 13, 2023
1 parent 00bcbbb commit 7e5e2dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 9 additions & 7 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ proc_primary_list
return createList(head, tail);
}

proc_array =
LBRAKE __ l:proc_primary_list __ RBRAKE {
return { type: 'array', value: l };
proc_array
= LBRAKE __ l:proc_primary_list __ RBRAKE {
return { type: 'array', value: l, brackets: true };
}

set_list
Expand Down Expand Up @@ -2038,8 +2038,8 @@ array_expr
return {
array_path: c,
type: 'array',
brackets: true,
keyword: '',
parentheses: true
}
}
/ s:(array_type / KW_ARRAY)? LBRAKE __ c:literal_list __ RBRAKE {
Expand All @@ -2048,16 +2048,18 @@ array_expr
array_path: c.map(l => ({ expr: l, as: null })),
type: 'array',
keyword: s && 'array',
parentheses: true
brackets: true,
}
}
/ s:(array_type / KW_ARRAY)? __ (LBRAKE / LPAREN) __ c:(parentheses_list_expr / expr) __ (RBRAKE / RPAREN) {
/ s:(array_type / KW_ARRAY)? __ l:(LBRAKE / LPAREN) __ c:(parentheses_list_expr / expr) __ r:(RBRAKE / RPAREN) {
if (`${l}${r}` !== '[]' && `${l}${r}` !== '()') throw new Error('parentheses or brackets is not in pair')
return {
definition: s,
expr_list: c,
type: 'array',
keyword: s && 'array',
parentheses: true
brackets: l === '[' ? true : false,
parentheses: l === '(' ? true: false
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/array-struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ function arrayExprListToSQL(expr) {
parentheses,
} = expr
if (!exprList) return `[${columnsToSQL(arrayPath)}]`
if (Array.isArray(exprList)) return `[${exprList.map(col => `(${columnsToSQL(col)})`).filter(hasVal).join(', ')}]`
const result = exprToSQL(exprList)
const result = Array.isArray(exprList) ? exprList.map(col => `(${columnsToSQL(col)})`).filter(hasVal).join(', ') : exprToSQL(exprList)
if (brackets) return `[${result}]`
return parentheses ? `(${result})` : result
}
Expand Down
9 changes: 8 additions & 1 deletion test/bigquery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,13 @@ describe('BigQuery', () => {
'SELECT string_agg(DISTINCT column1) AS some_column1, string_agg(column2) AS some_column1 FROM table1'
]
},
{
title: 'if',
sql: [
'select if(((a)), b, null)',
'SELECT if(((a)), b, NULL)'
]
},
]

SQL_LIST.forEach(sqlInfo => {
Expand Down Expand Up @@ -856,7 +863,7 @@ describe('BigQuery', () => {
type: 'string',
value: 'abc'
}
expect(arrayStructValueToSQL(expr)).to.equal(`'${expr.expr_list.value}'`)
expect(arrayStructValueToSQL(expr)).to.equal(`['${expr.expr_list.value}']`)
})

it('should return undefined and dataType', () => {
Expand Down

0 comments on commit 7e5e2dc

Please sign in to comment.