-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-20399][SQL] Add a config to fallback string literal parsing consistent with old sql parser behavior #17887
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
11 commits
Select commit
Hold shift + click to select a range
d0b2c22
Add a config to fallback string literal parsing consistent with old s…
viirya 8ae0747
Address comments.
viirya ab77de7
Fix code comment.
viirya 04a9fd3
Merge tests.
viirya 9ce7eb0
Address comments.
viirya 3241b88
Fix config doc.
viirya c81f030
Update RLike function description.
viirya e854b10
Also update doc for Like expression.
viirya d8cd670
Fix doc.
viirya 8ecb2ea
Change java string literal to SQL shell string.
viirya 375eb9c
Fix doc.
viirya 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
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, _} | |
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{First, Last} | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types.CalendarInterval | ||
|
|
||
|
|
@@ -39,12 +40,17 @@ class ExpressionParserSuite extends PlanTest { | |
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
|
|
||
| def assertEqual(sqlCommand: String, e: Expression): Unit = { | ||
| compareExpressions(parseExpression(sqlCommand), e) | ||
| val defaultParser = CatalystSqlParser | ||
|
|
||
| def assertEqual( | ||
| sqlCommand: String, | ||
| e: Expression, | ||
| parser: ParserInterface = defaultParser): Unit = { | ||
| compareExpressions(parser.parseExpression(sqlCommand), e) | ||
| } | ||
|
|
||
| def intercept(sqlCommand: String, messages: String*): Unit = { | ||
| val e = intercept[ParseException](parseExpression(sqlCommand)) | ||
| val e = intercept[ParseException](defaultParser.parseExpression(sqlCommand)) | ||
| messages.foreach { message => | ||
| assert(e.message.contains(message)) | ||
| } | ||
|
|
@@ -101,7 +107,7 @@ class ExpressionParserSuite extends PlanTest { | |
| test("long binary logical expressions") { | ||
| def testVeryBinaryExpression(op: String, clazz: Class[_]): Unit = { | ||
| val sql = (1 to 1000).map(x => s"$x == $x").mkString(op) | ||
| val e = parseExpression(sql) | ||
| val e = defaultParser.parseExpression(sql) | ||
| assert(e.collect { case _: EqualTo => true }.size === 1000) | ||
| assert(e.collect { case x if clazz.isInstance(x) => true }.size === 999) | ||
| } | ||
|
|
@@ -160,6 +166,15 @@ class ExpressionParserSuite extends PlanTest { | |
| assertEqual("a not regexp 'pattern%'", !('a rlike "pattern%")) | ||
| } | ||
|
|
||
| test("like expressions with ESCAPED_STRING_LITERALS = true") { | ||
| val conf = new SQLConf() | ||
| conf.setConfString(SQLConf.ESCAPED_STRING_LITERALS.key, "true") | ||
| val parser = new CatalystSqlParser(conf) | ||
| assertEqual("a rlike '^\\x20[\\x20-\\x23]+$'", 'a rlike "^\\x20[\\x20-\\x23]+$", parser) | ||
| assertEqual("a rlike 'pattern\\\\'", 'a rlike "pattern\\\\", parser) | ||
| assertEqual("a rlike 'pattern\\t\\n'", 'a rlike "pattern\\t\\n", parser) | ||
| } | ||
|
|
||
| test("is null expressions") { | ||
| assertEqual("a is null", 'a.isNull) | ||
| assertEqual("a is not null", 'a.isNotNull) | ||
|
|
@@ -413,38 +428,79 @@ class ExpressionParserSuite extends PlanTest { | |
| } | ||
|
|
||
| test("strings") { | ||
|
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. how about something like
Member
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. Sure. |
||
| // Single Strings. | ||
| assertEqual("\"hello\"", "hello") | ||
| assertEqual("'hello'", "hello") | ||
|
|
||
| // Multi-Strings. | ||
| assertEqual("\"hello\" 'world'", "helloworld") | ||
| assertEqual("'hello' \" \" 'world'", "hello world") | ||
|
|
||
| // 'LIKE' string literals. Notice that an escaped '%' is the same as an escaped '\' and a | ||
| // regular '%'; to get the correct result you need to add another escaped '\'. | ||
| // TODO figure out if we shouldn't change the ParseUtils.unescapeSQLString method? | ||
| assertEqual("'pattern%'", "pattern%") | ||
| assertEqual("'no-pattern\\%'", "no-pattern\\%") | ||
| assertEqual("'pattern\\\\%'", "pattern\\%") | ||
| assertEqual("'pattern\\\\\\%'", "pattern\\\\%") | ||
|
|
||
| // Escaped characters. | ||
| // See: http://dev.mysql.com/doc/refman/5.7/en/string-literals.html | ||
| assertEqual("'\\0'", "\u0000") // ASCII NUL (X'00') | ||
| assertEqual("'\\''", "\'") // Single quote | ||
| assertEqual("'\\\"'", "\"") // Double quote | ||
| assertEqual("'\\b'", "\b") // Backspace | ||
| assertEqual("'\\n'", "\n") // Newline | ||
| assertEqual("'\\r'", "\r") // Carriage return | ||
| assertEqual("'\\t'", "\t") // Tab character | ||
| assertEqual("'\\Z'", "\u001A") // ASCII 26 - CTRL + Z (EOF on windows) | ||
|
|
||
| // Octals | ||
| assertEqual("'\\110\\145\\154\\154\\157\\041'", "Hello!") | ||
|
|
||
| // Unicode | ||
| assertEqual("'\\u0057\\u006F\\u0072\\u006C\\u0064\\u0020\\u003A\\u0029'", "World :)") | ||
| Seq(true, false).foreach { escape => | ||
| val conf = new SQLConf() | ||
| conf.setConfString(SQLConf.ESCAPED_STRING_LITERALS.key, escape.toString) | ||
| val parser = new CatalystSqlParser(conf) | ||
|
|
||
| // tests that have same result whatever the conf is | ||
| // Single Strings. | ||
| assertEqual("\"hello\"", "hello", parser) | ||
| assertEqual("'hello'", "hello", parser) | ||
|
|
||
| // Multi-Strings. | ||
| assertEqual("\"hello\" 'world'", "helloworld", parser) | ||
| assertEqual("'hello' \" \" 'world'", "hello world", parser) | ||
|
|
||
| // 'LIKE' string literals. Notice that an escaped '%' is the same as an escaped '\' and a | ||
| // regular '%'; to get the correct result you need to add another escaped '\'. | ||
| // TODO figure out if we shouldn't change the ParseUtils.unescapeSQLString method? | ||
| assertEqual("'pattern%'", "pattern%", parser) | ||
| assertEqual("'no-pattern\\%'", "no-pattern\\%", parser) | ||
|
|
||
| // tests that have different result regarding the conf | ||
| if (escape) { | ||
| // When SQLConf.ESCAPED_STRING_LITERALS is enabled, string literal parsing fallbacks to | ||
| // Spark 1.6 behavior. | ||
|
|
||
| // 'LIKE' string literals. | ||
| assertEqual("'pattern\\\\%'", "pattern\\\\%", parser) | ||
| assertEqual("'pattern\\\\\\%'", "pattern\\\\\\%", parser) | ||
|
|
||
| // Escaped characters. | ||
| assertEqual("'\0'", "\u0000", parser) // ASCII NUL (X'00') | ||
|
|
||
| // Note: Single quote follows 1.6 parsing behavior when ESCAPED_STRING_LITERALS is enabled. | ||
| val e = intercept[ParseException](parser.parseExpression("'\''")) | ||
| assert(e.message.contains("extraneous input '''")) | ||
|
|
||
| assertEqual("'\"'", "\"", parser) // Double quote | ||
| assertEqual("'\b'", "\b", parser) // Backspace | ||
| assertEqual("'\n'", "\n", parser) // Newline | ||
| assertEqual("'\r'", "\r", parser) // Carriage return | ||
| assertEqual("'\t'", "\t", parser) // Tab character | ||
|
|
||
| // Octals | ||
| assertEqual("'\110\145\154\154\157\041'", "Hello!", parser) | ||
| // Unicode | ||
| assertEqual("'\u0057\u006F\u0072\u006C\u0064\u0020\u003A\u0029'", "World :)", parser) | ||
| } else { | ||
| // Default behavior | ||
|
|
||
| // 'LIKE' string literals. | ||
| assertEqual("'pattern\\\\%'", "pattern\\%", parser) | ||
| assertEqual("'pattern\\\\\\%'", "pattern\\\\%", parser) | ||
|
|
||
| // Escaped characters. | ||
| // See: http://dev.mysql.com/doc/refman/5.7/en/string-literals.html | ||
| assertEqual("'\\0'", "\u0000", parser) // ASCII NUL (X'00') | ||
| assertEqual("'\\''", "\'", parser) // Single quote | ||
| assertEqual("'\\\"'", "\"", parser) // Double quote | ||
| assertEqual("'\\b'", "\b", parser) // Backspace | ||
| assertEqual("'\\n'", "\n", parser) // Newline | ||
| assertEqual("'\\r'", "\r", parser) // Carriage return | ||
| assertEqual("'\\t'", "\t", parser) // Tab character | ||
| assertEqual("'\\Z'", "\u001A", parser) // ASCII 26 - CTRL + Z (EOF on windows) | ||
|
|
||
| // Octals | ||
| assertEqual("'\\110\\145\\154\\154\\157\\041'", "Hello!", parser) | ||
|
|
||
| // Unicode | ||
| assertEqual("'\\u0057\\u006F\\u0072\\u006C\\u0064\\u0020\\u003A\\u0029'", "World :)", | ||
| parser) | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| test("intervals") { | ||
|
|
||
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.
shall we also update the document of LIKE?
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.
I was afraid of duplication info there. But OK, let me add few lines into
Liketoo.Uh oh!
There was an error while loading. Please reload this page.
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.
Ah. I think we don't need to update the doc ofLike. The two special symbols%and_are parsed in the same way as 1.6 parser.Rethink about this, we still need to add info about string literal parsing...
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.
I've updated the doc of
Like.