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

    https://github.com/apache/spark/pull/13876#discussion_r69808303
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -820,16 +820,25 @@ object ConstantFolding extends Rule[LogicalPlan] {
     }
     
     /**
    - * Replaces [[In (value, seq[Literal])]] with optimized version[[InSet 
(value, HashSet[Literal])]]
    - * which is much faster
    + * Optimize IN predicates:
    + * 1. Removes deterministic repetitions.
    + * 2. Replaces [[In (value, seq[Literal])]] with optimized version
    + *    [[InSet (value, HashSet[Literal])]] which is much faster.
      */
     case class OptimizeIn(conf: CatalystConf) extends Rule[LogicalPlan] {
       def apply(plan: LogicalPlan): LogicalPlan = plan transform {
         case q: LogicalPlan => q transformExpressionsDown {
    -      case In(v, list) if !list.exists(!_.isInstanceOf[Literal]) &&
    -          list.size > conf.optimizerInSetConversionThreshold =>
    -        val hSet = list.map(e => e.eval(EmptyRow))
    -        InSet(v, HashSet() ++ hSet)
    +      case i @ In(v, list) if list.forall(_.deterministic) =>
    +        val newList = ExpressionSet(list).toSeq
    +        if (newList.forall(_.isInstanceOf[Literal]) &&
    +            newList.size > conf.optimizerInSetConversionThreshold) {
    +          val hSet = newList.map(e => e.eval(EmptyRow))
    +          InSet(v, HashSet() ++ hSet)
    +        } else if (newList.length < list.length) {
    +          i.copy(v, newList)
    +        } else { // newList.length == list.length
    +          i
    --- End diff --
    
    I think this can bring some performance concerns because we are doing a lot 
of work in order to return the original query, and given the optimizer is 
iterative, it would spend a lot of cycles just doing this.
    
    Can we introduce a flag (lazy val) to the In expression to check whether it 
is optimizable? If it is not, then we shouldn't even go into the case. 
Something like
    
    ```
    case class In(...) {
      lazy val inSetConvertable: Boolean = list.forall(_.deterministic)
    }
    ```



---
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