Skip to content
Open
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 @@ -148,7 +148,10 @@ trait ExtractValue extends Expression with QueryErrorsBase {
case class GetStructField(child: Expression, ordinal: Int, name: Option[String] = None)
extends UnaryExpression with ExtractValue {

lazy val childSchema = child.dataType.asInstanceOf[StructType]
Copy link
Contributor

Choose a reason for hiding this comment

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

The assumption is that the child must be StructType so that we will construct GetStructField.

lazy val childSchema = child.dataType match {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure that this is a error message improvement and not an actual bug in the analyzer? It might be that somehow we don't apply type coercion correctly or similar, right?

case structType: StructType => structType
case _ => throw QueryCompilationErrors.schemaIsNotStructTypeError(child, child.dataType)
}

override lazy val canonicalized: Expression = {
copy(child = child.canonicalized, name = None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,18 @@ class ComplexTypesSuite extends QueryTest with SharedSparkSession {
val dataType = createNamedStruct.dataType
assert(dataType.asInstanceOf[StructType].fields.head.metadata == metadata)
}

test("SPARK-53396: Throw proper error message when child of GetStructField is not StructType") {
val withInvalidChildSchema = GetStructField(Literal("col1"), 1)
checkError(
exception = intercept[AnalysisException] {
withInvalidChildSchema.dataType
},
condition = "INVALID_SCHEMA.NON_STRUCT_TYPE",
parameters = Map(
"inputSchema" -> "\"col1\"",
"dataType" -> "\"STRING\""
)
)
}
}