Github user gczsjdy commented on a diff in the pull request:
https://github.com/apache/spark/pull/20135#discussion_r159235432
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
---
@@ -684,6 +685,34 @@ object TypeCoercion {
}
}
+ /**
+ * Coerces the types of [[Elt]] children to expected ones.
+ *
+ * If `spark.sql.function.eltOutputAsString` is false and all children
types are binary,
+ * the expected types are binary. Otherwise, the expected ones are
strings.
+ */
+ case class EltCoercion(conf: SQLConf) extends TypeCoercionRule {
+
+ override protected def coerceTypes(plan: LogicalPlan): LogicalPlan =
plan transform { case p =>
+ p transformExpressionsUp {
+ // Skip nodes if unresolved or not enough children
+ case c @ Elt(children) if !c.childrenResolved || children.size < 2
=> c
+ case c @ Elt(children) if conf.eltOutputAsString ||
+ !children.tail.map(_.dataType).forall(_ == BinaryType) =>
+ val index = children.head
+ val newIndex = ImplicitTypeCasts.implicitCast(index,
IntegerType).getOrElse(index)
+ val newInputs = children.tail.map { e =>
+ ImplicitTypeCasts.implicitCast(e, StringType).getOrElse(e)
+ }
+ c.copy(children = newIndex +: newInputs)
+ case c @ Elt(children) =>
+ val index = children.head
+ val newIndex = ImplicitTypeCasts.implicitCast(index,
IntegerType).getOrElse(index)
+ c.copy(children = newIndex +: children.tail)
--- End diff --
Is is better to merge the common parts of the last 2 cases? We can add one
more nested pattern match, but I'm not sure this is a better way.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]