From 7ed6e9f9c03e494ef254d8d6cf9ece4bfaec7708 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Wed, 20 Nov 2024 18:59:06 +0800 Subject: [PATCH 1/2] fix: key as column in athena --- pegjs/athena.pegjs | 2 +- test/athena.spec.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pegjs/athena.pegjs b/pegjs/athena.pegjs index 7ed0484d..8f1e2881 100644 --- a/pegjs/athena.pegjs +++ b/pegjs/athena.pegjs @@ -47,7 +47,7 @@ 'JOIN': true, 'JSON': true, - 'KEY': true, + // 'KEY': true, 'LEFT': true, 'LIKE': true, diff --git a/test/athena.spec.js b/test/athena.spec.js index 81784a44..76d633db 100644 --- a/test/athena.spec.js +++ b/test/athena.spec.js @@ -304,4 +304,19 @@ 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', () => { + const 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') + }) }) \ No newline at end of file From befa43c3918c7a17936d227675121c89d3356909 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Wed, 20 Nov 2024 19:06:57 +0800 Subject: [PATCH 2/2] refactor: support db.schema.table in athena --- pegjs/athena.pegjs | 11 ++++++++++- test/athena.spec.js | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pegjs/athena.pegjs b/pegjs/athena.pegjs index 8f1e2881..d40944cb 100644 --- a/pegjs/athena.pegjs +++ b/pegjs/athena.pegjs @@ -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', diff --git a/test/athena.spec.js b/test/athena.spec.js index 76d633db..c62afd69 100644 --- a/test/athena.spec.js +++ b/test/athena.spec.js @@ -305,7 +305,7 @@ describe('athena', () => { 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', () => { - const sql = `WITH CTE AS ( + let sql = `WITH CTE AS ( SELECT * FROM test_cte ) SELECT @@ -318,5 +318,19 @@ describe('athena', () => { 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'") }) }) \ No newline at end of file