Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
protected val OVERWRITE = Keyword("OVERWRITE")
protected val LIKE = Keyword("LIKE")
protected val RLIKE = Keyword("RLIKE")
protected val UPPER = Keyword("UPPER")
protected val LOWER = Keyword("LOWER")
protected val REGEXP = Keyword("REGEXP")
protected val ORDER = Keyword("ORDER")
protected val OUTER = Keyword("OUTER")
Expand Down Expand Up @@ -329,6 +331,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
AVG ~> "(" ~> expression <~ ")" ^^ { case exp => Average(exp) } |
MIN ~> "(" ~> expression <~ ")" ^^ { case exp => Min(exp) } |
MAX ~> "(" ~> expression <~ ")" ^^ { case exp => Max(exp) } |
UPPER ~> "(" ~> expression <~ ")" ^^ { case exp => Upper(exp) } |
LOWER ~> "(" ~> expression <~ ")" ^^ { case exp => Lower(exp) } |
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
case c ~ "," ~ t ~ "," ~ f => If(c,t,f)
} |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ trait StringRegexExpression {
}
}

trait CaseConversionExpression {
self: UnaryExpression =>

type EvaluatedType = Any

def convert(v: String): String

def nullable: Boolean = true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be null if the child is not nullable?

def dataType: DataType = StringType

override def eval(input: Row): Any = {
val beConverted = child.eval(input)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

converted seems better than beConverted here.

convert(beConverted.toString)
}
}

/**
* Simple RegEx pattern matching function
*/
Expand Down Expand Up @@ -115,3 +131,21 @@ case class RLike(left: Expression, right: Expression)
override def escape(v: String): String = v
override def matches(regex: Pattern, str: String): Boolean = regex.matcher(str).find(0)
}

/**
* System function upper()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, "A function that converts the characters of a string to uppercase."

* */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the redundant leading *.

case class Upper(child: Expression)
extends UnaryExpression with CaseConversionExpression {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fit on one line?


override def convert(v: String): String = {v.toUpperCase()}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please either remove the braces or start the function body in a new line.

}

/**
* System function lower()
* */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

case class Lower(child: Expression)
extends UnaryExpression with CaseConversionExpression {

override def convert(v: String): String = {v.toLowerCase()}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new line at the end of the file.

23 changes: 23 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,27 @@ class SQLQuerySuite extends QueryTest {
(3, "C"),
(4, "D")))
}

test("system function upper()") {
checkAnswer(
sql("SELECT n,UPPER(l) FROM lowerCaseData"),
Seq(
(1, "A"),
(2, "B"),
(3, "C"),
(4, "D")))
}

test("system function lower()") {
checkAnswer(
sql("SELECT N,LOWER(L) FROM upperCaseData"),
Seq(
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
(5, "e"),
(6, "f")))
}

}