Skip to content

[SPARK-38075][SQL] Fix hasNext in HiveScriptTransformationExec's process output iterator #35368

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
Show file tree
Hide file tree
Changes from 3 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 @@ -64,7 +64,7 @@ private[hive] case class HiveScriptTransformationExec(
outputSoi: StructObjectInspector,
hadoopConf: Configuration): Iterator[InternalRow] = {
new Iterator[InternalRow] with HiveInspectors {
var curLine: String = null
Copy link
Member

Choose a reason for hiding this comment

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

Although this is irrelevant to this correctness issue, the clean-up looks okay.

var completed = false
Copy link
Member

Choose a reason for hiding this comment

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

nit: private

val scriptOutputStream = new DataInputStream(inputStream)

val scriptOutputReader =
Expand All @@ -78,13 +78,17 @@ private[hive] case class HiveScriptTransformationExec(
lazy val unwrappers = outputSoi.getAllStructFieldRefs.asScala.map(unwrapperFor)

override def hasNext: Boolean = {
if (completed) {
return false
}
try {
if (scriptOutputWritable == null) {
scriptOutputWritable = reusedWritableObject

if (scriptOutputReader != null) {
if (scriptOutputReader.next(scriptOutputWritable) <= 0) {
checkFailureAndPropagate(writerThread, null, proc, stderrBuffer)
completed = true
Copy link
Contributor Author

@bersprockets bersprockets Jan 30, 2022

Choose a reason for hiding this comment

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

Alternatively, I could just set scriptOutputWritable to null here, and I wouldn't need the if statement at the top. That seemed to work. However, it feels a little unhygienic to read from an inputStream that has already returned EOF, so I added the completed flag instead.

return false
}
} else {
Expand All @@ -97,6 +101,7 @@ private[hive] case class HiveScriptTransformationExec(
// there can be a lag between EOF being written out and the process
// being terminated. So explicitly waiting for the process to be done.
checkFailureAndPropagate(writerThread, null, proc, stderrBuffer)
completed = true
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,21 @@ class HiveScriptTransformationSuite extends BaseScriptTransformationSuite with T
assert(e.contains("java.lang.ArithmeticException: long overflow"))
}
}

test("SPARK-38075: ORDER BY with LIMIT adds fake rows") {
withTempView("v") {
val df = Seq((1), (2), (3)).toDF("a")
df.createTempView("v")
checkAnswer(sql(
"""
|SELECT TRANSFORM(a)
| USING 'cat' AS (a)
|FROM v
|ORDER BY a
|LIMIT 10
|""".stripMargin),
identity,
Row("1") :: Row("2") :: Row("3") :: Nil)
}
}
}