-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-28109][SQL] Fix TRIM(type trimStr FROM str) returns incorrect value #24911
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
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
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 |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| package org.apache.spark.sql.catalyst.parser | ||
|
|
||
| import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier} | ||
| import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedAttribute, UnresolvedFunction, UnresolvedGenerator, UnresolvedInlineTable, UnresolvedRelation, UnresolvedSubqueryColumnAliases, UnresolvedTableValuedFunction} | ||
| import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedAlias, UnresolvedAttribute, UnresolvedFunction, UnresolvedGenerator, UnresolvedInlineTable, UnresolvedRelation, UnresolvedSubqueryColumnAliases, UnresolvedTableValuedFunction} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
|
|
@@ -702,33 +702,40 @@ class PlanParserSuite extends AnalysisTest { | |
| } | ||
|
|
||
| test("TRIM function") { | ||
| intercept("select ltrim(both 'S' from 'SS abc S'", "missing ')' at '<EOF>'") | ||
| intercept("select rtrim(trailing 'S' from 'SS abc S'", "missing ')' at '<EOF>'") | ||
| def assertTrimPlans(inputSQL: String, expectedExpression: Expression): Unit = { | ||
| comparePlans( | ||
| parsePlan(inputSQL), | ||
| Project(Seq(UnresolvedAlias(expectedExpression)), OneRowRelation()) | ||
| ) | ||
| } | ||
|
|
||
| assertEqual( | ||
| intercept("select ltrim(both 'S' from 'SS abc S'", "mismatched input 'from' expecting {')'") | ||
| intercept("select rtrim(trailing 'S' from 'SS abc S'", "mismatched input 'from' expecting {')'") | ||
|
Member
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. This is the change of error message, but it looks okay. The previous error message is the following. |
||
|
|
||
| assertTrimPlans( | ||
| "SELECT TRIM(BOTH '@$%&( )abc' FROM '@ $ % & ()abc ' )", | ||
| OneRowRelation().select('TRIM.function("@$%&( )abc", "@ $ % & ()abc ")) | ||
| StringTrim(Literal("@ $ % & ()abc "), Some(Literal("@$%&( )abc"))) | ||
| ) | ||
| assertEqual( | ||
| assertTrimPlans( | ||
| "SELECT TRIM(LEADING 'c []' FROM '[ ccccbcc ')", | ||
| OneRowRelation().select('ltrim.function("c []", "[ ccccbcc ")) | ||
| StringTrimLeft(Literal("[ ccccbcc "), Some(Literal("c []"))) | ||
| ) | ||
| assertEqual( | ||
| assertTrimPlans( | ||
| "SELECT TRIM(TRAILING 'c&^,.' FROM 'bc...,,,&&&ccc')", | ||
| OneRowRelation().select('rtrim.function("c&^,.", "bc...,,,&&&ccc")) | ||
| StringTrimRight(Literal("bc...,,,&&&ccc"), Some(Literal("c&^,."))) | ||
| ) | ||
|
|
||
| assertEqual( | ||
| assertTrimPlans( | ||
| "SELECT TRIM(BOTH FROM ' bunch o blanks ')", | ||
| OneRowRelation().select('TRIM.function(" bunch o blanks ")) | ||
| StringTrim(Literal(" bunch o blanks "), None) | ||
| ) | ||
| assertEqual( | ||
| assertTrimPlans( | ||
| "SELECT TRIM(LEADING FROM ' bunch o blanks ')", | ||
| OneRowRelation().select('ltrim.function(" bunch o blanks ")) | ||
| StringTrimLeft(Literal(" bunch o blanks "), None) | ||
| ) | ||
| assertEqual( | ||
| assertTrimPlans( | ||
| "SELECT TRIM(TRAILING FROM ' bunch o blanks ')", | ||
| OneRowRelation().select('rtrim.function(" bunch o blanks ")) | ||
| StringTrimRight(Literal(" bunch o blanks "), None) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -40,11 +40,11 @@ SELECT substring('Spark SQL' from -3); | |
| SELECT substring('Spark SQL' from 5 for 1); | ||
|
|
||
| -- trim/ltrim/rtrim | ||
| SELECT trim('yxTomxx', 'xyz'); | ||
| SELECT trim('xxxbarxxx', 'x'); | ||
| SELECT ltrim('zzzytest', 'xyz'); | ||
| SELECT ltrim('zzzytestxyz', 'xyz'); | ||
| SELECT ltrim('xyxXxyLAST WORD', 'xy'); | ||
| SELECT rtrim('testxxzx', 'xyz'); | ||
| SELECT rtrim('xyztestxxzx', 'xyz'); | ||
| SELECT rtrim('TURNERyxXxy', 'xy'); | ||
| SELECT trim('yxTomxx', 'xyz'), trim(BOTH 'xyz' FROM 'yxTomxx'); | ||
| SELECT trim('xxxbarxxx', 'x'), trim(BOTH 'x' FROM 'xxxbarxxx'); | ||
| SELECT ltrim('zzzytest', 'xyz'), trim(LEADING 'xyz' FROM 'zzzytest'); | ||
| SELECT ltrim('zzzytestxyz', 'xyz'), trim(LEADING 'xyz' FROM 'zzzytestxyz'); | ||
| SELECT ltrim('xyxXxyLAST WORD', 'xy'), trim(LEADING 'xy' FROM 'xyxXxyLAST WORD'); | ||
| SELECT rtrim('testxxzx', 'xyz'), trim(TRAILING 'xyz' FROM 'testxxzx'); | ||
| SELECT rtrim('xyztestxxzx', 'xyz'), trim(TRAILING 'xyz' FROM 'xyztestxxzx'); | ||
| SELECT rtrim('TURNERyxXxy', 'xy'), trim(TRAILING 'xy' FROM 'TURNERyxXxy'); | ||
|
Member
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. Thank you for the new test coverage! |
||
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.
The old one was a hacky way.