Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13505#discussion_r65821620
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/package.scala
 ---
    @@ -86,11 +88,42 @@ package object expressions  {
       /**
        * Helper functions for working with `Seq[Attribute]`.
        */
    -  implicit class AttributeSeq(attrs: Seq[Attribute]) {
    +  implicit class AttributeSeq(val attrs: Seq[Attribute]) {
         /** Creates a StructType with a schema matching this `Seq[Attribute]`. 
*/
         def toStructType: StructType = {
           StructType(attrs.map(a => StructField(a.name, a.dataType, 
a.nullable)))
         }
    +
    +    // It's possible that `attrs` is a linked list, which can lead to bad 
O(n^2) loops when
    +    // accessing attributes by their ordinals. To avoid this performance 
penalty, convert the input
    +    // to an array.
    +    private lazy val attrsArray = attrs.toArray
    +
    +    private lazy val exprIdToOrdinal = {
    +      val arr = attrsArray
    +      val map = Maps.newHashMapWithExpectedSize[ExprId, Int](arr.length)
    +      var index = 0
    +      while (index < arr.length) {
    +        val exprId = arr(index).exprId
    +        if (!map.containsKey(exprId)) {
    +          map.put(exprId, index)
    --- End diff --
    
    I was being conservative here in order to match the behavior of the old 
linear scan, which stopped upon finding the first entry. However, we can remove 
the need for this check if we iterate over `arr` in reverse-order.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to