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 @@ -509,24 +509,40 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
private def splitSemiColon(line: String): JList[String] = {
var insideSingleQuote = false
var insideDoubleQuote = false
var insideComment = false
var escape = false
var beginIndex = 0
var endIndex = line.length
val ret = new JArrayList[String]

for (index <- 0 until line.length) {
if (line.charAt(index) == '\'') {
if (line.charAt(index) == '\'' && !insideComment) {
// take a look to see if it is escaped
if (!escape) {
// flip the boolean variable
insideSingleQuote = !insideSingleQuote
}
} else if (line.charAt(index) == '\"') {
} else if (line.charAt(index) == '\"' && !insideComment) {
// take a look to see if it is escaped
if (!escape) {
// flip the boolean variable
insideDoubleQuote = !insideDoubleQuote
}
} else if (line.charAt(index) == '-') {
val hasNext: Boolean = index + 1 < line.length
if (insideDoubleQuote || insideSingleQuote || insideComment) {
// ignore
} else if (hasNext && line.charAt(index + 1) == '-') {
// ignore quotes and ;
insideComment = true
// ignore eol
endIndex = index
} else {
// continue
}

} else if (line.charAt(index) == ';') {
if (insideSingleQuote || insideDoubleQuote) {
if (insideSingleQuote || insideDoubleQuote || insideComment) {
// do not split
} else {
// split, do not include ; itself
Expand All @@ -543,7 +559,7 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
escape = true
}
}
ret.add(line.substring(beginIndex))
ret.add(line.substring(beginIndex, endIndex))
ret
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,21 @@ class CliSuite extends SparkFunSuite with BeforeAndAfterAll with Logging {
-> "1.000000000000000000"
)
}

test("SPARK-30049 Should not complaint for quotes in commented lines") {
runCliWithin(1.minute)(
"""SELECT concat('test', 'comment') -- someone's comment here
|;""".stripMargin -> "testcomment"
)
}

test("SPARK-30049 Should not complaint for quotes in commented with multi-lines") {
runCliWithin(1.minute)(
"""SELECT concat('test', 'comment') -- someone's comment here \\
|comment continues here with single ' quote
| extra '
|;""".stripMargin -> "testcomment"
)
}

Copy link
Member

@maropu maropu Feb 5, 2020

Choose a reason for hiding this comment

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

nit: Can you avoid this unnecessary blank?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@maropu Not sure, which blank?

}