Skip to content
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 @@ -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,19 @@ class StopWordsRemoverSuite
.setCaseSensitive(true)
testDefaultReadWrite(t)
}

test("StopWordsRemover output column already exists") {
val outputCol = "expected"
val remover = new StopWordsRemover()
.setInputCol("raw")
.setOutputCol(outputCol)
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("The", "the", "swift"), Seq("swift"))
)).toDF("raw", outputCol)

val thrown = intercept[IllegalArgumentException] {
testStopWordsRemover(remover, dataSet)
}
assert(thrown.getMessage == s"requirement failed: Column $outputCol already exists.")
}
}