-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25734][SQL] Literal should have a value corresponding to dataType #22724
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 |
|---|---|---|
|
|
@@ -40,9 +40,10 @@ import org.json4s.JsonAST._ | |
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow, ScalaReflection} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.catalyst.util.{ArrayData, DateTimeUtils, MapData} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.types._ | ||
| import org.apache.spark.util.Utils | ||
|
|
||
| object Literal { | ||
| val TrueLiteral: Literal = Literal(true, BooleanType) | ||
|
|
@@ -196,6 +197,47 @@ object Literal { | |
| case other => | ||
| throw new RuntimeException(s"no default for type $dataType") | ||
| } | ||
|
|
||
| private[expressions] def validateLiteralValue(value: Any, dataType: DataType): Unit = { | ||
| def doValidate(v: Any, dataType: DataType): Boolean = dataType match { | ||
| case _ if v == null => true | ||
| case BooleanType => v.isInstanceOf[Boolean] | ||
| case ByteType => v.isInstanceOf[Byte] | ||
| case ShortType => v.isInstanceOf[Short] | ||
| case IntegerType | DateType => v.isInstanceOf[Int] | ||
| case LongType | TimestampType => v.isInstanceOf[Long] | ||
| case FloatType => v.isInstanceOf[Float] | ||
| case DoubleType => v.isInstanceOf[Double] | ||
| case _: DecimalType => v.isInstanceOf[Decimal] | ||
| case CalendarIntervalType => v.isInstanceOf[CalendarInterval] | ||
| case BinaryType => v.isInstanceOf[Array[Byte]] | ||
| case StringType => v.isInstanceOf[UTF8String] | ||
| case st: StructType => | ||
| v.isInstanceOf[InternalRow] && { | ||
| val row = v.asInstanceOf[InternalRow] | ||
| st.fields.map(_.dataType).zipWithIndex.forall { | ||
| case (dt, i) => doValidate(row.get(i, dt), dt) | ||
| } | ||
| } | ||
| case at: ArrayType => | ||
| v.isInstanceOf[ArrayData] && { | ||
| val ar = v.asInstanceOf[ArrayData] | ||
| ar.numElements() == 0 || doValidate(ar.get(0, at.elementType), at.elementType) | ||
| } | ||
| case mt: MapType => | ||
| v.isInstanceOf[MapData] && { | ||
| val map = v.asInstanceOf[MapData] | ||
| doValidate(map.keyArray(), ArrayType(mt.keyType)) && | ||
| doValidate(map.valueArray(), ArrayType(mt.valueType)) | ||
| } | ||
|
Member
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. I'm wondering whether we don't need to check the whole elements for
Member
Author
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. Since the whole element check seems to be expensive, the current one is ok to me. |
||
| case ObjectType(cls) => cls.isInstance(v) | ||
| case udt: UserDefinedType[_] => doValidate(v, udt.sqlType) | ||
| case _ => false | ||
|
Member
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.
|
||
| } | ||
| require(doValidate(value, dataType), | ||
| s"Literal must have a corresponding value to ${dataType.catalogString}, " + | ||
| s"but class ${Utils.getSimpleName(value.getClass)} found.") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -240,6 +282,8 @@ object DecimalLiteral { | |
| */ | ||
| case class Literal (value: Any, dataType: DataType) extends LeafExpression { | ||
|
|
||
| Literal.validateLiteralValue(value, dataType) | ||
|
|
||
| override def foldable: Boolean = true | ||
| override def nullable: Boolean = value == null | ||
|
|
||
|
|
||
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.
can we do the same for array and map?
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.
done