Skip to content

Commit 8158bf1

Browse files
Merge pull request taozhi8833998#2601 from taozhi8833998/fix-trim-func
fix: trim function with multiple args in all dialects
2 parents a030048 + 425bc81 commit 8158bf1

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

pegjs/snowflake.pegjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
function commonStrToLiteral(strOrLiteral) {
178178
return typeof strOrLiteral === 'string' ? { type: 'same', value: strOrLiteral } : strOrLiteral
179179
}
180-
180+
181181
function getSurroundFromLiteralType(literal) {
182182
switch (literal.type) {
183183
case 'double_quote_string':
@@ -2571,7 +2571,7 @@ table_name
25712571
v.table = v.name;
25722572
return v;
25732573
}
2574-
2574+
25752575

25762576
or_and_expr
25772577
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {

src/func.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@ function funcToSQL(expr) {
104104
const suffixStr = exprToSQL(suffix)
105105
const funcName = [literalToSQL(name.schema), name.name.map(literalToSQL).join('.')].filter(hasVal).join('.')
106106
if (!args) return [funcName, withinGroupStr, overStr].filter(hasVal).join(' ')
107-
let separator = expr.separator || ', '
108-
if (toUpper(funcName) === 'TRIM') separator = ' '
107+
const separator = expr.separator || ', '
108+
let fromPosition = 0
109+
if (toUpper(funcName) === 'TRIM') {
110+
for (let i = 0, len = args.value.length; i < len; ++i) {
111+
const arg = args.value[i]
112+
if (arg.type === 'origin' && arg.value === 'from') {
113+
fromPosition = i
114+
break
115+
}
116+
}
117+
}
109118
let str = [funcName]
110119
str.push(args_parentheses === false ? ' ' : '(')
111120
const argsList = exprToSQL(args)
@@ -115,6 +124,8 @@ function funcToSQL(expr) {
115124
argsSQL = [argsSQL, argsList[i]].join(` ${exprToSQL(separator[i - 1])} `)
116125
}
117126
str.push(argsSQL)
127+
} else if (toUpper(funcName) === 'TRIM' && fromPosition > 0) {
128+
str.push([argsList.slice(0, fromPosition + 1).join(' '), argsList.slice(fromPosition + 1).join(', ')].join(' '))
118129
} else {
119130
str.push(argsList.join(separator))
120131
}

test/snowflake.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,13 @@ describe('snowflake', () => {
552552
'SELECT * FROM my_table',
553553
'SELECT * FROM my_table'
554554
]
555+
},
556+
{
557+
title: 'trim function with multiple args',
558+
sql: [
559+
"SELECT trim(col, ' ') FROM DUAL;",
560+
`SELECT trim("col", ' ') FROM DUAL`
561+
]
555562
}
556563
]
557564
SQL_LIST.forEach(sqlInfo => {

0 commit comments

Comments
 (0)