-
Notifications
You must be signed in to change notification settings - Fork 28.8k
[SPARK-53470][SQL] ExtractValue expressions should always do type checking #52216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ package org.apache.spark.sql.catalyst.expressions | |
import org.apache.spark.QueryContext | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.analysis._ | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch | ||
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode} | ||
import org.apache.spark.sql.catalyst.trees.TreePattern.{EXTRACT_VALUE, TreePattern} | ||
import org.apache.spark.sql.catalyst.util.{quoteIdentifier, ArrayData, GenericArrayData, MapData, TypeUtils} | ||
|
@@ -146,7 +145,9 @@ trait ExtractValue extends Expression with QueryErrorsBase { | |
* For example, when get field `yEAr` from `<year: int, month: int>`, we should pass in `yEAr`. | ||
*/ | ||
case class GetStructField(child: Expression, ordinal: Int, name: Option[String] = None) | ||
extends UnaryExpression with ExtractValue { | ||
extends UnaryExpression with ExtractValue with ExpectsInputTypes { | ||
|
||
override def inputTypes: Seq[AbstractDataType] = Seq(StructType, IntegralType) | ||
|
||
lazy val childSchema = child.dataType.asInstanceOf[StructType] | ||
|
||
|
@@ -207,6 +208,13 @@ case class GetArrayStructFields( | |
numFields: Int, | ||
containsNull: Boolean) extends UnaryExpression with ExtractValue { | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = child.dataType match { | ||
case ArrayType(_: StructType, _) => TypeCheckResult.TypeCheckSuccess | ||
// This should never happen, unless we hit a bug. | ||
case other => TypeCheckResult.TypeCheckFailure( | ||
"GetArrayStructFields.child must be array of struct type, but got " + other) | ||
} | ||
|
||
override def dataType: DataType = ArrayType(field.dataType, containsNull) | ||
override def toString: String = s"$child.${field.name}" | ||
override def sql: String = s"${child.sql}.${quoteIdentifier(field.name)}" | ||
|
@@ -285,8 +293,7 @@ case class GetArrayItem( | |
with ExtractValue | ||
with SupportQueryContext { | ||
|
||
// We have done type checking for child in `ExtractValue`, so only need to check the `ordinal`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment also didn't look correct. Actually we did checking the datatype of the child in |
||
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType, IntegralType) | ||
override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType, IntegralType) | ||
|
||
override def toString: String = s"$child[$ordinal]" | ||
override def sql: String = s"${child.sql}[${ordinal.sql}]" | ||
|
@@ -355,30 +362,6 @@ case class GetArrayItem( | |
}) | ||
} | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
(left.dataType, right.dataType) match { | ||
case (_: ArrayType, e2) if !e2.isInstanceOf[IntegralType] => | ||
DataTypeMismatch( | ||
errorSubClass = "UNEXPECTED_INPUT_TYPE", | ||
messageParameters = Map( | ||
"paramIndex" -> ordinalNumber(1), | ||
"requiredType" -> toSQLType(IntegralType), | ||
"inputSql" -> toSQLExpr(right), | ||
"inputType" -> toSQLType(right.dataType)) | ||
) | ||
case (e1, _) if !e1.isInstanceOf[ArrayType] => | ||
DataTypeMismatch( | ||
errorSubClass = "UNEXPECTED_INPUT_TYPE", | ||
messageParameters = Map( | ||
"paramIndex" -> ordinalNumber(0), | ||
"requiredType" -> toSQLType(TypeCollection(ArrayType)), | ||
"inputSql" -> toSQLExpr(left), | ||
"inputType" -> toSQLType(left.dataType)) | ||
) | ||
case _ => TypeCheckResult.TypeCheckSuccess | ||
} | ||
} | ||
|
||
override protected def withNewChildrenInternal( | ||
newLeft: Expression, newRight: Expression): GetArrayItem = | ||
copy(child = newLeft, ordinal = newRight) | ||
|
@@ -507,16 +490,19 @@ case class GetMapValue(child: Expression, key: Expression) | |
|
||
private[catalyst] def keyType = child.dataType.asInstanceOf[MapType].keyType | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
super.checkInputDataTypes() match { | ||
case f if f.isFailure => f | ||
case TypeCheckResult.TypeCheckSuccess => | ||
TypeUtils.checkForOrderingExpr(keyType, prettyName) | ||
} | ||
override def checkInputDataTypes(): TypeCheckResult = child.dataType match { | ||
case _: MapType => | ||
super.checkInputDataTypes() match { | ||
case f if f.isFailure => f | ||
case TypeCheckResult.TypeCheckSuccess => | ||
TypeUtils.checkForOrderingExpr(keyType, prettyName) | ||
} | ||
// This should never happen, unless we hit a bug. | ||
case other => TypeCheckResult.TypeCheckFailure( | ||
"GetMapValue.child must be map type, but got " + other) | ||
} | ||
|
||
// We have done type checking for child in `ExtractValue`, so only need to check the `key`. | ||
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType, keyType) | ||
override def inputTypes: Seq[AbstractDataType] = Seq(MapType, keyType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, if it is possible that the child data type could be other than MapType, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is why I override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh okay. |
||
|
||
override def toString: String = s"$child[$key]" | ||
override def sql: String = s"${child.sql}[${key.sql}]" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to keep the null type error behavior. #50590 improved the error message for it, but also made
GetArrayItem
fail for null type input. This usually doesn't matter asGetArrayItem
should never get null type input except fromGet
, but this PR restores it for consistency with otherExtractValue
expressions.