-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-4151][SQL] Add string operation function trim, ltrim, rtrim, length to support SparkSql (HiveQL) #2998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5809496
Add description of SQL CLI and HiveServer.
OopsOutOfMemory c577589
Merge branch 'master' of https://github.com/apache/spark
OopsOutOfMemory e654704
Merge branch 'master' of https://github.com/apache/spark
OopsOutOfMemory 3269eab
Add system function trim
OopsOutOfMemory ce6899a
HiveQL support trim
OopsOutOfMemory e2781ee
modify keyword of Trim
OopsOutOfMemory 2166c77
correct spelling mistake
OopsOutOfMemory a4f4e4b
correct spelling mistake
OopsOutOfMemory 2137e28
add test suit for trim
OopsOutOfMemory 10d8ace
support 3 functions : ltrim rtrim length in SparkQL and HiveQl
OopsOutOfMemory 0a0f4e0
change the name of the trait CaseConversionExpression to StringTransf…
OopsOutOfMemory 0fa2cd6
change return type
OopsOutOfMemory b358048
deleted: sql/README.md
OopsOutOfMemory 558d7bf
new file: sql/README.md
OopsOutOfMemory ab29a7e
Merge branch 'sparksql' of https://github.com/OopsOutOfMemory/spark i…
OopsOutOfMemory 57111f5
change the implementation of ltrim and trim from regex to apache comm…
OopsOutOfMemory b7790f4
Merge branch 'master' of https://github.com/OopsOutOfMemory/spark int…
OopsOutOfMemory dca6adb
change to scala native implementation
OopsOutOfMemory 5989358
fix ident issues and add test case
OopsOutOfMemory 0925b32
spelling correct
OopsOutOfMemory addfbd9
remove unused golden file
OopsOutOfMemory File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import scala.collection.IndexedSeqOptimized | |
|
|
||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.UnresolvedException | ||
| import org.apache.spark.sql.catalyst.types.{BinaryType, BooleanType, DataType, StringType} | ||
| import org.apache.spark.sql.catalyst.types._ | ||
|
|
||
| trait StringRegexExpression { | ||
| self: BinaryExpression => | ||
|
|
@@ -71,7 +71,7 @@ trait StringRegexExpression { | |
| } | ||
| } | ||
|
|
||
| trait CaseConversionExpression { | ||
| trait StringTransformationExpression { | ||
| self: UnaryExpression => | ||
|
|
||
| type EvaluatedType = Any | ||
|
|
@@ -92,6 +92,34 @@ trait CaseConversionExpression { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * This trait is use for string calculation that return Integer Type | ||
| * Functions that return Integer Type should use this trait | ||
| * eg: length(s), instr( string1, string2, start_position,nth_appearance ) | ||
| */ | ||
|
|
||
| trait StringCalculationExpression { | ||
| self: UnaryExpression => | ||
|
|
||
| type EvaluatedType = Any | ||
|
|
||
| def calc(v: String): Int | ||
|
|
||
| override def foldable: Boolean = child.foldable | ||
| def nullable: Boolean = child.nullable | ||
| def dataType: DataType = IntegerType | ||
|
|
||
| override def eval(input: Row): Any = { | ||
| val evaluated = child.eval(input) | ||
| if (evaluated == null) { | ||
| null | ||
| } else { | ||
| calc(evaluated.toString) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Simple RegEx pattern matching function | ||
| */ | ||
|
|
@@ -134,10 +162,51 @@ case class RLike(left: Expression, right: Expression) | |
| override def matches(regex: Pattern, str: String): Boolean = regex.matcher(str).find(0) | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * A function that strip whitespace (or other characters) from the beginning of a string | ||
| */ | ||
| case class Ltrim(child: Expression) extends UnaryExpression with StringTransformationExpression { | ||
|
|
||
| override def convert(v: String): String = v.dropWhile(_ == ' ') | ||
|
|
||
| override def toString() = s"Ltrim($child)" | ||
| } | ||
|
|
||
| /** | ||
| * A function that strip whitespace (or other characters) from the end of a string | ||
| */ | ||
| case class Rtrim(child: Expression) extends UnaryExpression with StringTransformationExpression { | ||
|
|
||
| override def convert(v: String): String = v.reverse.dropWhile(_ == ' ').reverse | ||
|
|
||
| override def toString() = s"Rtrim($child)" | ||
| } | ||
|
|
||
| /** | ||
| * A function that calculate the length of a string | ||
| */ | ||
| case class Length(child: Expression) extends UnaryExpression with StringCalculationExpression { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to override the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so it is. |
||
| override def calc(v: String): Int = v.length() | ||
|
|
||
| override def toString() = s"Length($child)" | ||
| } | ||
|
|
||
| /** | ||
| * A function that trim the characters of a string | ||
| */ | ||
| case class Trim(child: Expression) extends UnaryExpression with StringTransformationExpression { | ||
|
|
||
| override def convert(v: String): String = v.trim() | ||
|
|
||
| override def toString() = s"Trim($child)" | ||
| } | ||
|
|
||
| /** | ||
| * A function that converts the characters of a string to uppercase. | ||
| */ | ||
| case class Upper(child: Expression) extends UnaryExpression with CaseConversionExpression { | ||
| case class Upper(child: Expression) extends UnaryExpression with StringTransformationExpression { | ||
|
|
||
| override def convert(v: String): String = v.toUpperCase() | ||
|
|
||
|
|
@@ -147,7 +216,7 @@ case class Upper(child: Expression) extends UnaryExpression with CaseConversionE | |
| /** | ||
| * A function that converts the characters of a string to lowercase. | ||
| */ | ||
| case class Lower(child: Expression) extends UnaryExpression with CaseConversionExpression { | ||
| case class Lower(child: Expression) extends UnaryExpression with StringTransformationExpression { | ||
|
|
||
| override def convert(v: String): String = v.toLowerCase() | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This trait seems only used by
Length, how about just merge them? I think we can extract the trait in the future if we have more string operators returnsIntegerType. Sorry not sure if you're planning on this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chenghao-intel yep, what I mean is to add a trait to support return type of Integer. Because function like LENGTH(n) which compute the size of a string return Integer, INSTR(chr1,chr2,[n,[m]]) which compute a position also return Integer. But I'm not sure whether this will cause some side effect or not.