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 @@ -28,7 +28,6 @@ import org.apache.spark.annotation.Stable
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, InterpretedOrdering}
import org.apache.spark.sql.catalyst.parser.{CatalystSqlParser, LegacyTypeStringParser}
import org.apache.spark.sql.catalyst.util.{quoteIdentifier, truncatedString}
import org.apache.spark.util.Utils

/**
* A [[StructType]] object can be constructed by
Expand Down Expand Up @@ -57,7 +56,7 @@ import org.apache.spark.util.Utils
*
* // If this struct does not have a field called "d", it throws an exception.
* struct("d")
* // java.lang.IllegalArgumentException: Field "d" does not exist.
* // java.lang.IllegalArgumentException: d does not exist.
* // ...
*
* // Extract multiple StructFields. Field names are provided in a set.
Expand All @@ -69,7 +68,7 @@ import org.apache.spark.util.Utils
* // Any names without matching fields will throw an exception.
* // For the case shown below, an exception is thrown due to "d".
* struct(Set("b", "c", "d"))
* // java.lang.IllegalArgumentException: Field "d" does not exist.
* // java.lang.IllegalArgumentException: d does not exist.
* // ...
* }}}
*
Expand Down Expand Up @@ -272,22 +271,21 @@ case class StructType(fields: Array[StructField]) extends DataType with Seq[Stru
def apply(name: String): StructField = {
nameToField.getOrElse(name,
throw new IllegalArgumentException(
s"""Field "$name" does not exist.
|Available fields: ${fieldNames.mkString(", ")}""".stripMargin))
s"$name does not exist. Available: ${fieldNames.mkString(", ")}"))
}

/**
* Returns a [[StructType]] containing [[StructField]]s of the given names, preserving the
* original order of fields.
*
* @throws IllegalArgumentException if a field cannot be found for any of the given names
* @throws IllegalArgumentException if at least one given field name does not exist
*/
def apply(names: Set[String]): StructType = {
val nonExistFields = names -- fieldNamesSet
if (nonExistFields.nonEmpty) {
throw new IllegalArgumentException(
s"""Nonexistent field(s): ${nonExistFields.mkString(", ")}.
|Available fields: ${fieldNames.mkString(", ")}""".stripMargin)
s"${nonExistFields.mkString(", ")} do(es) not exist. " +
s"Available: ${fieldNames.mkString(", ")}")
}
// Preserve the original order of fields.
StructType(fields.filter(f => names.contains(f.name)))
Expand All @@ -301,8 +299,7 @@ case class StructType(fields: Array[StructField]) extends DataType with Seq[Stru
def fieldIndex(name: String): Int = {
nameToIndex.getOrElse(name,
throw new IllegalArgumentException(
s"""Field "$name" does not exist.
|Available fields: ${fieldNames.mkString(", ")}""".stripMargin))
s"$name does not exist. Available: ${fieldNames.mkString(", ")}"))
}

private[sql] def getFieldIndex(name: String): Option[Int] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ import org.apache.spark.sql.types.StructType.fromDDL

class StructTypeSuite extends SparkFunSuite {

val s = StructType.fromDDL("a INT, b STRING")
private val s = StructType.fromDDL("a INT, b STRING")

test("lookup a single missing field should output existing fields") {
val e = intercept[IllegalArgumentException](s("c")).getMessage
assert(e.contains("Available fields: a, b"))
assert(e.contains("Available: a, b"))
}

test("lookup a set of missing fields should output existing fields") {
val e = intercept[IllegalArgumentException](s(Set("a", "c"))).getMessage
assert(e.contains("Available fields: a, b"))
assert(e.contains("Available: a, b"))
}

test("lookup fieldIndex for missing field should output existing fields") {
val e = intercept[IllegalArgumentException](s.fieldIndex("c")).getMessage
assert(e.contains("Available fields: a, b"))
assert(e.contains("Available: a, b"))
}

test("SPARK-24849: toDDL - simple struct") {
Expand Down