harshmotw-db commented on code in PR #47907:
URL: https://github.com/apache/spark/pull/47907#discussion_r1747880228
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/variantExpressions.scala:
##########
@@ -114,6 +114,73 @@ case class IsVariantNull(child: Expression) extends
UnaryExpression
copy(child = newChild)
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(expr) - Convert a nested input (array/map/struct) into a
variant where maps and structs are converted to variant objects which are
unordered unlike SQL structs. Input maps can only have string keys.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(named_struct('a', 1, 'b', 2));
+ {"a":1,"b":2}
+ > SELECT _FUNC_(array(1, 2, 3));
+ [1,2,3]
+ > SELECT _FUNC_(array(named_struct('a', 1)));
+ [{"a":1}]
+ > SELECT _FUNC_(array(map("a", 2)));
+ [{"a":2}]
+ """,
+ since = "4.0.0",
+ group = "variant_funcs")
+// scalastyle:on line.size.limit
+case class ToVariantObject(child: Expression)
+ extends UnaryExpression
+ with NullIntolerant
+ with QueryErrorsBase {
+
+ override val dataType: DataType = VariantType
+
+ // Only accept nested types at the root but any types can be nested inside.
+ override def checkInputDataTypes(): TypeCheckResult = {
+ val checkResult: Boolean = child.dataType match {
+ case _: StructType | _: ArrayType | _: MapType =>
+ VariantGet.checkDataType(child.dataType, allowStructsAndMaps = true)
+ case _ => false
+ }
+ if (!checkResult) {
+ DataTypeMismatch(
+ errorSubClass = "CAST_WITHOUT_SUGGESTION",
+ messageParameters =
+ Map("srcType" -> toSQLType(child.dataType), "targetType" ->
toSQLType(VariantType)))
+ } else {
+ TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ override def prettyName: String = "to_variant_object"
+
+ override protected def withNewChildInternal(newChild: Expression):
ToVariantObject =
+ copy(child = newChild)
+
+ protected override def nullSafeEval(input: Any): Any =
+ VariantExpressionEvalUtils.castToVariant(input, child.dataType)
+
+ override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
Review Comment:
Yes, every test added in `VariantEndToEndSuite.scala` tests with Codegen
enabled/disabled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]