Skip to content

Commit

Permalink
Merge pull request #1999 from taozhi8833998/feat-from-unnest-athena
Browse files Browse the repository at this point in the history
feat: support from unnest in athena
  • Loading branch information
taozhi8833998 authored Jul 11, 2024
2 parents 63673f2 + ebd6886 commit 7bfe03b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
36 changes: 34 additions & 2 deletions pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'UNION': true,
'UPDATE': true,
'USING': true,
'UNNEST': true,

'VALUES': true,

Expand Down Expand Up @@ -1088,9 +1089,28 @@ column_list_item
}

alias_clause
= KW_AS __ i:alias_ident { return i; }
= KW_AS __ i:(func_call / alias_ident) { return i; }
/ KW_AS? __ i:ident { return i; }

with_offset
= KW_WITH __ KW_OFFSET __ alias:alias_clause? {
return {
keyword: 'with offset as',
as: alias
}
}

from_unnest_item
= 'UNNEST'i __ LPAREN __ a:expr? __ RPAREN __ alias:alias_clause? __ wf:with_offset? {
return {
type: 'unnest',
expr: a,
parentheses: true,
as: alias,
with_offset: wf,
}
}

from_clause
= KW_FROM __ l:table_ref_list { return l; }

Expand Down Expand Up @@ -1186,7 +1206,8 @@ table_join

//NOTE that, the table assigned to `var` shouldn't write in `table_join`
table_base
= KW_DUAL {
= from_unnest_item
/ KW_DUAL {
return {
type: 'dual'
};
Expand Down Expand Up @@ -2142,6 +2163,17 @@ literal
/ literal_bool
/ literal_null
/ literal_datetime
/ literal_array

literal_array
= s:KW_ARRAY __ LBRAKE __ c:expr_list? __ RBRAKE {
return {
expr_list: c || { type: 'origin', value: '' },
type: 'array',
keyword: 'array',
brackets: true
}
}

literal_list
= head:literal tail:(__ COMMA __ literal)* {
Expand Down
2 changes: 1 addition & 1 deletion src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function unnestToSQL(unnestExpr) {
const { type, as, expr, with_offset: withOffset } = unnestExpr
const result = [
`${toUpper(type)}(${expr && exprToSQL(expr) || ''})`,
commonOptionConnector('AS', identifierToSql, as),
commonOptionConnector('AS', typeof as === 'string' ? identifierToSql : exprToSQL, as),
commonOptionConnector(
toUpper(withOffset && withOffset.keyword),
identifierToSql,
Expand Down
66 changes: 66 additions & 0 deletions test/athena.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('util function test', () => {
expect(columnIdentifierToSql("id")).to.be.equal('"id"')
})

it.only('should support trim query option', () => {
it('should support trim query option', () => {
const opt = {
"database": "mysql",
"parseOptions": {
Expand Down

0 comments on commit 7bfe03b

Please sign in to comment.