Skip to content

Commit

Permalink
[SPARK-14023][CORE][SQL] Don't reference 'field' in StructField error…
Browse files Browse the repository at this point in the history
…s for clarity in exceptions

## What changes were proposed in this pull request?

Variation of #20500
I cheated by not referencing fields or columns at all as this exception propagates in contexts where both would be applicable.

## How was this patch tested?

Existing tests

Closes #23373 from srowen/SPARK-14023.2.

Authored-by: Sean Owen <sean.owen@databricks.com>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
  • Loading branch information
srowen authored and dongjoon-hyun committed Dec 24, 2018
1 parent 1008ab0 commit 0523f5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
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

0 comments on commit 0523f5e

Please sign in to comment.