Skip to content
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

[SPARK-31102][SQL][3.0] Spark-sql fails to parse when contains comment #28565

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -1784,7 +1784,7 @@ fragment LETTER
;

SIMPLE_COMMENT
: '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN)
: '--' ('\\\n' | ~[\r\n])* '\r'? '\n'? -> channel(HIDDEN)
;

BRACKETED_EMPTY_COMMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ class PlanParserSuite extends AnalysisTest {
With(plan, ctes)
}

test("single comment case one") {
val plan = table("a").select(star())
assertEqual("-- single comment\nSELECT * FROM a", plan)
}

test("single comment case two") {
val plan = table("a").select(star())
assertEqual("-- single comment\\\nwith line continuity\nSELECT * FROM a", plan)
}

test("case insensitive") {
val plan = table("a").select(star())
assertEqual("sELEct * FroM a", plan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
var insideComment = false
var escape = false
var beginIndex = 0
var endIndex = line.length
val ret = new JArrayList[String]

for (index <- 0 until line.length) {
Expand Down Expand Up @@ -552,8 +551,6 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
} else if (hasNext && line.charAt(index + 1) == '-') {
// ignore quotes and ;
insideComment = true
// ignore eol
endIndex = index
}
} else if (line.charAt(index) == ';') {
if (insideSingleQuote || insideDoubleQuote || insideComment) {
Expand All @@ -563,8 +560,11 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
ret.add(line.substring(beginIndex, index))
beginIndex = index + 1
}
} else {
// nothing to do
} else if (line.charAt(index) == '\n') {
// with a new line the inline comment should end.
if (!escape) {
insideComment = false
}
}
// set the escape
if (escape) {
Expand All @@ -573,7 +573,7 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging {
escape = true
}
}
ret.add(line.substring(beginIndex, endIndex))
ret.add(line.substring(beginIndex))
ret
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,20 @@ class CliSuite extends SparkFunSuite with BeforeAndAfterAll with Logging {
)
}

test("SPARK-30049 Should not complain for quotes in commented with multi-lines") {
test("SPARK-31102 spark-sql fails to parse when contains comment") {
runCliWithin(1.minute)(
"""SELECT concat('test', 'comment') -- someone's comment here \\
| comment continues here with single ' quote \\
| extra ' \\
|;""".stripMargin -> "testcomment"
"""SELECT concat('test', 'comment'),
| -- someone's comment here
| 2;""".stripMargin -> "testcomment"
)
}

test("SPARK-30049 Should not complain 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"
"""SELECT concat('test', 'comment') -- someone's comment here \
| comment continues here with single ' quote \
| extra ' \
|;""".stripMargin -> "testcomment"
)
}

Expand Down