fqaiser94 commented on a change in pull request #29795:
URL: https://github.com/apache/spark/pull/29795#discussion_r494698625
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeCreator.scala
##########
@@ -541,57 +541,105 @@ case class StringToMap(text: Expression, pairDelim:
Expression, keyValueDelim: E
}
/**
- * Adds/replaces field in struct by name.
+ * Represents an operation to be applied to the fields of a struct.
*/
-case class WithFields(
- structExpr: Expression,
- names: Seq[String],
- valExprs: Seq[Expression]) extends Unevaluable {
+trait StructFieldsOperation {
- assert(names.length == valExprs.length)
+ val resolver: Resolver = SQLConf.get.resolver
+
+ /**
+ * Returns an updated list of StructFields and Expressions that will
ultimately be used
+ * as the fields argument for [[StructType]] and as the children argument for
+ * [[CreateNamedStruct]] respectively inside of [[UpdateFields]].
+ */
+ def apply(values: Seq[(StructField, Expression)]): Seq[(StructField,
Expression)]
+}
+
+/**
+ * Add or replace a field by name.
+ *
+ * We extend [[Unevaluable]] here to ensure that [[UpdateFields]] can include
it as part of its
+ * children, and thereby enable the analyzer to resolve and transform valExpr
as necessary.
+ */
+case class WithField(name: String, valExpr: Expression)
+ extends Unevaluable with StructFieldsOperation {
+
+ override def apply(values: Seq[(StructField, Expression)]):
Seq[(StructField, Expression)] = {
+ val newFieldExpr = (StructField(name, valExpr.dataType, valExpr.nullable),
valExpr)
+ if (values.exists { case (field, _) => resolver(field.name, name) }) {
+ values.map {
+ case (field, _) if resolver(field.name, name) => newFieldExpr
+ case x => x
+ }
+ } else {
+ values :+ newFieldExpr
+ }
+ }
+
+ override def children: Seq[Expression] = valExpr :: Nil
+
+ override def dataType: DataType = throw new UnresolvedException(this,
"dataType")
Review comment:
done
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]