Skip to content

Commit dbef224

Browse files
Merge pull request taozhi8833998#2228 from taozhi8833998/fix-key-athena
fix: key as column in athena
2 parents 125b7c9 + befa43c commit dbef224

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

pegjs/athena.pegjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
'JOIN': true,
4848
'JSON': true,
4949

50-
'KEY': true,
50+
// 'KEY': true,
5151

5252
'LEFT': true,
5353
'LIKE': true,
@@ -1845,7 +1845,16 @@ unary_operator
18451845
= '!' / '-' / '+' / '~'
18461846

18471847
column_ref
1848-
= tbl:ident __ DOT __ col:column {
1848+
= schema:ident tbl:(__ DOT __ ident) col:(__ DOT __ column) {
1849+
columnList.add(`select::${schema}.${tbl[3]}::${col[3].value}`);
1850+
return {
1851+
type: 'column_ref',
1852+
schema: schema,
1853+
table: tbl[3],
1854+
column: col[3]
1855+
};
1856+
}
1857+
/ tbl:ident __ DOT __ col:column {
18491858
columnList.add(`select::${tbl}::${col}`);
18501859
return {
18511860
type: 'column_ref',

test/athena.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,33 @@ describe('athena', () => {
304304
) = 0;`
305305
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')
306306
})
307+
it('should support key as column name', () => {
308+
let sql = `WITH CTE AS (
309+
SELECT * FROM test_cte
310+
)
311+
SELECT
312+
organization,
313+
date,
314+
author_email,
315+
t.key AS tag,
316+
t.value AS count
317+
FROM CTE
318+
CROSS JOIN UNNEST(tags_counts) AS t(key, value)
319+
ORDER BY 1, 2`;
320+
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')
321+
sql = `SELECT
322+
j.id,
323+
h.created AS change_time,
324+
i.fromstring AS from_status,
325+
i.tostring AS to_status
326+
FROM
327+
"bronze_prod"."jira_issues" j
328+
CROSS JOIN
329+
UNNEST(j.changelog.histories) AS T (h)
330+
CROSS JOIN
331+
UNNEST(h.items) AS T (i)
332+
WHERE
333+
i.field = 'status'`
334+
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'")
335+
})
307336
})

0 commit comments

Comments
 (0)