chenhao-db commented on code in PR #47907: URL: https://github.com/apache/spark/pull/47907#discussion_r1739215575
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/variantExpressions.scala: ########## @@ -114,6 +114,81 @@ 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) + + override def genCode(ctx: CodegenContext): ExprCode = { Review Comment: Very few expressions override `genCode`, and I don't feel it is necessary for this one to do so. You can put the logic inside `doGenCode`. -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org