Skip to content

Commit 701405c

Browse files
committed
feat: support position function in redshift
1 parent 05bb927 commit 701405c

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

pegjs/redshift.pegjs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4410,8 +4410,34 @@ tablefunc_clause
44104410
}
44114411
}
44124412

4413+
position_func_args
4414+
= s:literal_string __ KW_IN __ e:expr start:(__ KW_FROM __ literal_numeric)? {
4415+
// => expr_list
4416+
let value = [s, { type: 'origin', value: 'in' }, e]
4417+
if (start) {
4418+
value.push({ type: 'origin', value: 'from' })
4419+
value.push(start[3])
4420+
}
4421+
return {
4422+
type: 'expr_list',
4423+
value,
4424+
}
4425+
}
4426+
4427+
position_func_clause
4428+
= 'POSITION'i __ LPAREN __ args:position_func_args __ RPAREN {
4429+
// => { type: 'function'; name: string; args: expr_list; }
4430+
return {
4431+
type: 'function',
4432+
name: { name: [{ type: 'origin', value: 'position' }]},
4433+
separator: ' ',
4434+
args,
4435+
...getLocationObject(),
4436+
};
4437+
}
4438+
44134439
func_call
4414-
= trim_func_clause / tablefunc_clause
4440+
= trim_func_clause / tablefunc_clause / position_func_clause
44154441
/ name:'now'i __ LPAREN __ l:expr_list? __ RPAREN __ 'at'i __ KW_TIME __ 'zone'i __ z:literal_string {
44164442
// => { type: 'function'; name: proc_func_name; args: expr_list; suffix: literal_string; }
44174443
z.prefix = 'at time zone'

pegjs/snowflake.pegjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3881,8 +3881,33 @@ json_visit_list
38813881
value: createList(head, tail, 1)
38823882
}
38833883
}
3884+
position_func_args
3885+
= s:literal_string __ KW_IN __ e:expr start:(__ KW_FROM __ literal_numeric)? {
3886+
// => expr_list
3887+
let value = [s, { type: 'origin', value: 'in' }, e]
3888+
if (start) {
3889+
value.push({ type: 'origin', value: 'from' })
3890+
value.push(start[3])
3891+
}
3892+
return {
3893+
type: 'expr_list',
3894+
value,
3895+
}
3896+
}
3897+
3898+
position_func_clause
3899+
= 'POSITION'i __ LPAREN __ args:position_func_args __ RPAREN {
3900+
// => { type: 'function'; name: string; args: expr_list; }
3901+
return {
3902+
type: 'function',
3903+
name: { name: [{ type: 'origin', value: 'position' }]},
3904+
separator: ' ',
3905+
args,
3906+
...getLocationObject(),
3907+
};
3908+
}
38843909
func_call
3885-
= trim_func_clause
3910+
= trim_func_clause / position_func_clause
38863911
/ name:'now'i __ LPAREN __ l:expr_list? __ RPAREN __ 'at'i __ KW_TIME __ 'zone'i __ z:literal_string {
38873912
// => { type: 'function'; name: string; args: expr_list; suffix: literal_string; }
38883913
z.prefix = 'at time zone'

test/redshift.spec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,19 @@ describe('redshift', () => {
4141
const sql = 'select * from model_a a-- comment'
4242
expect(getParsedSql(sql)).to.be.equal('SELECT * FROM "model_a" AS "a"')
4343
})
44-
45-
})
44+
45+
it('should support position function', () => {
46+
const sql = `SELECT pv.email,
47+
pv.action_date_id,
48+
SUM(CASE
49+
WHEN event_category_name = 'Download'
50+
AND POSITION('PNG' IN event_full_name) > 0
51+
THEN 1
52+
ELSE 0
53+
END) AS download_png
54+
FROM dwh.dwh_fact_pageviews pv
55+
WHERE action_date_id >= '2022-01-01'
56+
GROUP BY 1, 2`
57+
expect(getParsedSql(sql)).to.be.equal(`SELECT "pv".email, "pv".action_date_id, SUM(CASE WHEN event_category_name = 'Download' AND POSITION('PNG' IN event_full_name) > 0 THEN 1 ELSE 0 END) AS "download_png" FROM "dwh"."dwh_fact_pageviews" AS "pv" WHERE action_date_id >= '2022-01-01' GROUP BY 1, 2`)
58+
})
59+
})

0 commit comments

Comments
 (0)