This is an automated email from the ASF dual-hosted git repository.
dongjoon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 0523f5e [SPARK-14023][CORE][SQL] Don't reference 'field' in
StructField errors for clarity in exceptions
0523f5e is described below
commit 0523f5e378e69f406104fabaf3ebe913de976bdb
Author: Sean Owen <[email protected]>
AuthorDate: Sun Dec 23 21:09:44 2018 -0800
[SPARK-14023][CORE][SQL] Don't reference 'field' in StructField errors for
clarity in exceptions
## What changes were proposed in this pull request?
Variation of https://github.com/apache/spark/pull/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 <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
.../scala/org/apache/spark/sql/types/StructType.scala | 17 +++++++----------
.../org/apache/spark/sql/types/StructTypeSuite.scala | 8 ++++----
2 files changed, 11 insertions(+), 14 deletions(-)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
index 6e8bbde..e01d7c5 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/StructType.scala
@@ -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
@@ -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.
@@ -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.
* // ...
* }}}
*
@@ -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)))
@@ -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] = {
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
index 53a78c9..b4ce26b 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
@@ -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") {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]