Skip to content

Commit

Permalink
Support TRIM(BOTH/LEADING/TRAILING FROM str) format
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyum committed Jun 17, 2019
1 parent b7b4452 commit a30fa00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ primaryExpression
| '(' query ')' #subqueryExpression
| qualifiedName '(' (setQuantifier? argument+=expression (',' argument+=expression)*)? ')'
(OVER windowSpec)? #functionCall
| qualifiedName '(' trimOption=(BOTH | LEADING | TRAILING) argument+=expression
| qualifiedName '(' trimOption=(BOTH | LEADING | TRAILING) (argument+=expression)?
FROM argument+=expression ')' #functionCall
| IDENTIFIER '->' expression #lambda
| '(' IDENTIFIER (',' IDENTIFIER)+ ')' '->' expression #lambda
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,12 @@ object StringTrim {
usage = """
_FUNC_(str) - Removes the leading and trailing space characters from `str`.
_FUNC_(BOTH FROM str) - Removes the leading and trailing space characters from `str`.
_FUNC_(LEADING FROM str) - Removes the leading space characters from `str`.
_FUNC_(TRAILING FROM str) - Removes the trailing space characters from `str`.
_FUNC_(BOTH trimStr FROM str) - Remove the leading and trailing `trimStr` characters from `str`
_FUNC_(LEADING trimStr FROM str) - Remove the leading `trimStr` characters from `str`
Expand All @@ -626,6 +632,12 @@ object StringTrim {
Examples:
> SELECT _FUNC_(' SparkSQL ');
SparkSQL
> SELECT _FUNC_(BOTH FROM ' SparkSQL ');
SparkSQL
> SELECT _FUNC_(LEADING FROM ' SparkSQL ');
SparkSQL
> SELECT _FUNC_(TRAILING FROM ' SparkSQL ');
SparkSQL
> SELECT _FUNC_('SL', 'SSparkSQLS');
parkSQ
> SELECT _FUNC_(BOTH 'SL' FROM 'SSparkSQLS');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
}

/**
* Create a (windowed) Function expression.
* Create a (windowed)/trim Function expression.
*/
override def visitFunctionCall(ctx: FunctionCallContext): Expression = withOrigin(ctx) {
def replaceFunctions(
Expand All @@ -1420,7 +1420,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
case SqlBaseParser.LEADING => funcID.copy(funcName = "ltrim")
case SqlBaseParser.TRAILING => funcID.copy(funcName = "rtrim")
case _ => throw new ParseException("Function trim doesn't support with " +
s"type ${opt.getType}. Please use BOTH, LEADING or Trailing as trim type", ctx)
s"type ${opt.getType}. Please use BOTH, LEADING or TRAILING as trim type", ctx)
}
} else {
funcID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,19 @@ class PlanParserSuite extends AnalysisTest {
"SELECT TRIM(TRAILING 'c&^,.' FROM 'bc...,,,&&&ccc')",
OneRowRelation().select('rtrim.function("c&^,.", "bc...,,,&&&ccc"))
)

assertEqual(
"SELECT TRIM(BOTH FROM ' bunch o blanks ')",
OneRowRelation().select('TRIM.function(" bunch o blanks "))
)
assertEqual(
"SELECT TRIM(LEADING FROM ' bunch o blanks ')",
OneRowRelation().select('ltrim.function(" bunch o blanks "))
)
assertEqual(
"SELECT TRIM(TRAILING FROM ' bunch o blanks ')",
OneRowRelation().select('rtrim.function(" bunch o blanks "))
)
}

test("precedence of set operations") {
Expand Down

0 comments on commit a30fa00

Please sign in to comment.