Skip to content
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 @@ -149,9 +149,7 @@ class StopWordsRemover(override val uid: String)
val inputType = schema($(inputCol)).dataType
require(inputType.sameType(ArrayType(StringType)),
s"Input type must be ArrayType(StringType) but got $inputType.")
val outputFields = schema.fields :+
StructField($(outputCol), inputType, schema($(inputCol)).nullable)
StructType(outputFields)
SchemaUtils.appendColumn(schema, $(outputCol), inputType, schema($(inputCol)).nullable)
}

override def copy(extra: ParamMap): StopWordsRemover = defaultCopy(extra)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ private[spark] object SchemaUtils {
def appendColumn(
schema: StructType,
colName: String,
dataType: DataType): StructType = {
dataType: DataType,
nullable: Boolean = false): StructType = {
if (colName.isEmpty) return schema
val fieldNames = schema.fieldNames
require(!fieldNames.contains(colName), s"Column $colName already exists.")
val outputFields = schema.fields :+ StructField(colName, dataType, nullable = false)
StructType(outputFields)
appendColumn(schema, StructField(colName, dataType, nullable))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,22 @@ class StopWordsRemoverSuite
.setCaseSensitive(true)
testDefaultReadWrite(t)
}

test("StopWordsRemover output column already exists") {
val outpuCol = "expected"
Copy link
Member

Choose a reason for hiding this comment

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

typo: "outputCol"

val remover = new StopWordsRemover()
.setInputCol("raw")
.setOutputCol(outpuCol)
.setCaseSensitive(true)
Copy link
Member

Choose a reason for hiding this comment

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

not needed for this test

val dataSet = sqlContext.createDataFrame(Seq(
Copy link
Member

Choose a reason for hiding this comment

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

Just copy one of the datasets from an above test. That should fix the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed out that second column in dataSet was totally empty... - and that was the problem...
I do not want to make that example too complicated, because this test does not even check correctness of execution result
I'm sorry for problems

(Seq("A"), Seq("A")),
(Seq("The", "the"), Seq("The"))
)).toDF("raw", outpuCol)

val thrown = intercept[IllegalArgumentException] {
testStopWordsRemover(remover, dataSet)
}
assert(thrown.getClass === classOf[IllegalArgumentException])
Copy link
Member

Choose a reason for hiding this comment

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

This is already checked in line 104.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to take pattern from:
github.com/apache/spark/blob/master/core/src/test/scala/org/apache/spark/FailureSuite.scala#L100-L106

I think this check theoretically could be useful:
https://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html
is non-abstract class and have subclasses,
intercept can catch e.g. PatternSyntaxException, which is not what we should treat as a test success.

But of course, PatternSyntaxException is very unlikely to have message "requirement failed: Column ${outpuCol} already exists."

Am I correct?
Maybe "assert(thrown.getClass === classOf[IllegalArgumentException])" line should stay?

Copy link
Member

Choose a reason for hiding this comment

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

I see; I bet you're correct. Don't worry about it for now, though.

assert(thrown.getMessage == s"requirement failed: Column ${outpuCol} already exists.")
}
}