Skip to content

Commit

Permalink
Merge pull request #2228 from taozhi8833998/fix-key-athena
Browse files Browse the repository at this point in the history
fix: key as column in athena
  • Loading branch information
taozhi8833998 authored Nov 20, 2024
2 parents 125b7c9 + befa43c commit dbef224
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
'JOIN': true,
'JSON': true,

'KEY': true,
// 'KEY': true,

'LEFT': true,
'LIKE': true,
Expand Down Expand Up @@ -1845,7 +1845,16 @@ unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:ident __ DOT __ col:column {
= schema:ident tbl:(__ DOT __ ident) col:(__ DOT __ column) {
columnList.add(`select::${schema}.${tbl[3]}::${col[3].value}`);
return {
type: 'column_ref',
schema: schema,
table: tbl[3],
column: col[3]
};
}
/ tbl:ident __ DOT __ col:column {
columnList.add(`select::${tbl}::${col}`);
return {
type: 'column_ref',
Expand Down
29 changes: 29 additions & 0 deletions test/athena.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,33 @@ describe('athena', () => {
) = 0;`
expect(getParsedSql(sql)).to.be.equal('SELECT `id`, CAST(CURRENT_TIMESTAMP AS TIMESTAMP(6)) AS `dbt_insert_time` FROM `some_table` WHERE cardinality(FILTER(map_values(`note`), VALUE -> `VALUE` IS NOT NULL)) = 0')
})
it('should support key as column name', () => {
let sql = `WITH CTE AS (
SELECT * FROM test_cte
)
SELECT
organization,
date,
author_email,
t.key AS tag,
t.value AS count
FROM CTE
CROSS JOIN UNNEST(tags_counts) AS t(key, value)
ORDER BY 1, 2`;
expect(getParsedSql(sql)).to.be.equal('WITH `CTE` AS (SELECT * FROM `test_cte`) SELECT `organization`, DATE , `author_email`, `t`.`key` AS `tag`, `t`.`value` AS `count` FROM `CTE` CROSS JOIN UNNEST(`tags_counts`) AS t(`key`, `value`) ORDER BY 1 ASC, 2 ASC')
sql = `SELECT
j.id,
h.created AS change_time,
i.fromstring AS from_status,
i.tostring AS to_status
FROM
"bronze_prod"."jira_issues" j
CROSS JOIN
UNNEST(j.changelog.histories) AS T (h)
CROSS JOIN
UNNEST(h.items) AS T (i)
WHERE
i.field = 'status'`
expect(getParsedSql(sql)).to.be.equal("SELECT `j`.`id`, `h`.`created` AS `change_time`, `i`.`fromstring` AS `from_status`, `i`.`tostring` AS `to_status` FROM `bronze_prod`.`jira_issues` AS `j` CROSS JOIN UNNEST(`j`.`changelog`.`histories`) AS T(`h`) CROSS JOIN UNNEST(`h`.`items`) AS T(`i`) WHERE `i`.`field` = 'status'")
})
})

0 comments on commit dbef224

Please sign in to comment.