You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2023/10/05 14:45:05 UTC

[spark] branch master updated: [SPARK-36112][SQL] Support correlated EXISTS and IN subqueries using DecorrelateInnerQuery framework

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new e8eb2459e1c [SPARK-36112][SQL] Support correlated EXISTS and IN subqueries using DecorrelateInnerQuery framework
e8eb2459e1c is described below

commit e8eb2459e1c1591eedc6b96738407e2a0b91264c
Author: Andrey Gubichev <an...@databricks.com>
AuthorDate: Thu Oct 5 22:44:45 2023 +0800

    [SPARK-36112][SQL] Support correlated EXISTS and IN subqueries using DecorrelateInnerQuery framework
    
    ### What changes were proposed in this pull request?
    
    Currently, `DecorrelateInnerQuery` is only enabled for scalar and lateral subqueries. This PR enables `DecorrelateInnerQuery` for IN/EXISTS subqueries
    
    ### Why are the changes needed?
    
    See above.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Users can run more EXISTS/IN subqueries, including correlated subqueries with aggregates and limits etc.
    
    ### How was this patch tested?
    
    Existing unit tests and new query tests.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No
    
    Closes #43111 from agubichev/correlated_exists.
    
    Authored-by: Andrey Gubichev <an...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../sql/catalyst/analysis/CheckAnalysis.scala      |  10 +-
 .../catalyst/optimizer/DecorrelateInnerQuery.scala |  22 +-
 .../spark/sql/catalyst/optimizer/subquery.scala    |  57 ++-
 .../org/apache/spark/sql/internal/SQLConf.scala    |  12 +
 .../sql/catalyst/analysis/AnalysisErrorSuite.scala |  33 +-
 .../catalyst/analysis/ResolveSubquerySuite.scala   |  14 -
 .../exists-subquery/exists-aggregate.sql.out       |  41 +-
 .../exists-subquery/exists-count-bug.sql.out       | 246 +++++++++++
 .../exists-joins-and-set-ops.sql.out               | 322 ++++++++------
 .../exists-subquery/exists-orderby-limit.sql.out   | 253 +++++++++++
 .../subquery/in-subquery/in-count-bug.sql.out      | 378 +++++++++++++++++
 .../subquery/in-subquery/in-group-by.sql.out       |  42 +-
 .../subquery/in-subquery/in-limit.sql.out          | 366 ++++++++++++++++
 .../subquery/in-subquery/in-set-operations.sql.out | 470 +++++++++++++--------
 .../subquery/exists-subquery/exists-count-bug.sql  |  37 ++
 .../exists-subquery/exists-orderby-limit.sql       |  73 +++-
 .../inputs/subquery/in-subquery/in-count-bug.sql   |  50 +++
 .../inputs/subquery/in-subquery/in-limit.sql       | 109 +++++
 .../sql-tests/results/join-lateral.sql.out         |   1 -
 .../exists-subquery/exists-aggregate.sql.out       |  25 +-
 .../exists-subquery/exists-count-bug.sql.out       | 142 +++++++
 .../exists-joins-and-set-ops.sql.out               | 168 ++------
 .../exists-subquery/exists-orderby-limit.sql.out   | 155 +++++++
 .../subquery/in-subquery/in-count-bug.sql.out      | 182 ++++++++
 .../subquery/in-subquery/in-group-by.sql.out       |  23 +-
 .../results/subquery/in-subquery/in-limit.sql.out  | 198 +++++++++
 .../subquery/in-subquery/in-set-operations.sql.out | 278 ++++--------
 .../scala/org/apache/spark/sql/SubquerySuite.scala | 104 ++---
 28 files changed, 2993 insertions(+), 818 deletions(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala
index de453f6bc49..64f1cafd03f 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala
@@ -1138,9 +1138,11 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB
       isScalar: Boolean = false,
       isLateral: Boolean = false): Unit = {
     // Some query shapes are only supported with the DecorrelateInnerQuery framework.
-    // Currently we only use this new framework for scalar and lateral subqueries.
+    // Support for Exists and IN subqueries is subject to a separate config flag
+    // 'decorrelateInnerQueryEnabledForExistsIn'.
     val usingDecorrelateInnerQueryFramework =
-      (isScalar || isLateral) && SQLConf.get.decorrelateInnerQueryEnabled
+      (SQLConf.get.decorrelateInnerQueryEnabledForExistsIn || isScalar || isLateral) &&
+        SQLConf.get.decorrelateInnerQueryEnabled
 
     // Validate that correlated aggregate expression do not contain a mixture
     // of outer and local references.
@@ -1402,6 +1404,10 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB
           failOnInvalidOuterReference(l)
           checkPlan(input, aggregated, canContainOuter)
 
+        case o @ Offset(_, input) =>
+          failOnInvalidOuterReference(o)
+          checkPlan(input, aggregated, canContainOuter)
+
         // Category 4: Any other operators not in the above 3 categories
         // cannot be on a correlation path, that is they are allowed only
         // under a correlation point but they and their descendant operators
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala
index 1cb7c8f3ac6..08d581d6ddf 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala
@@ -22,6 +22,7 @@ import scala.collection.mutable.ArrayBuffer
 import org.apache.spark.SparkException
 import org.apache.spark.sql.catalyst.expressions._
 import org.apache.spark.sql.catalyst.expressions.SubExprUtils._
+import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
 import org.apache.spark.sql.catalyst.plans._
 import org.apache.spark.sql.catalyst.plans.logical._
 import org.apache.spark.sql.catalyst.trees.TreePattern.OUTER_REFERENCE
@@ -461,6 +462,22 @@ object DecorrelateInnerQuery extends PredicateHelper {
       p.mapChildren(rewriteDomainJoins(outerPlan, _, conditions))
   }
 
+  private def isCountBugFree(aggregateExpressions: Seq[NamedExpression]): Boolean = {
+    // The COUNT bug only appears if an aggregate expression returns a non-NULL result on an empty
+    // input.
+    // Typical example (hence the name) is COUNT(*) that returns 0 from an empty result.
+    // However, SUM(x) IS NULL is another case that returns 0, and in general any IS/NOT IS and CASE
+    // expressions are suspect (and the combination of those).
+    // For now we conservatively accept only those expressions that are guaranteed to be safe.
+    aggregateExpressions.forall {
+      case _ : AttributeReference => true
+      case Alias(_: AttributeReference, _) => true
+      case Alias(_: Literal, _) => true
+      case Alias(a: AggregateExpression, _) if a.aggregateFunction.defaultResult == None => true
+      case _ => false
+    }
+  }
+
   def apply(
       innerPlan: LogicalPlan,
       outerPlan: LogicalPlan,
@@ -710,6 +727,8 @@ object DecorrelateInnerQuery extends PredicateHelper {
           case a @ Aggregate(groupingExpressions, aggregateExpressions, child) =>
             val outerReferences = collectOuterReferences(a.expressions)
             val newOuterReferences = parentOuterReferences ++ outerReferences
+            val countBugSusceptible = groupingExpressions.isEmpty &&
+              !isCountBugFree(aggregateExpressions)
             val (newChild, joinCond, outerReferenceMap) =
               decorrelate(child, newOuterReferences, aggregated = true, underSetOp)
             // Replace all outer references in grouping and aggregate expressions, and keep
@@ -772,7 +791,8 @@ object DecorrelateInnerQuery extends PredicateHelper {
             // | 0 | 2    | true       | 2                              |
             // | 0 | null | null       | 0                              |  <--- correct result
             // +---+------+------------+--------------------------------+
-            if (groupingExpressions.isEmpty && handleCountBug) {
+            // TODO(a.gubichev): retire the 'handleCountBug' parameter.
+            if (countBugSusceptible && handleCountBug) {
               // Evaluate the aggregate expressions with zero tuples.
               val resultMap = RewriteCorrelatedScalarSubquery.evalAggregateOnZeroTups(newAggregate)
               val alwaysTrue = Alias(Literal.TrueLiteral, "alwaysTrue")()
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala
index 91cd838ad61..08cbd29b20f 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala
@@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.optimizer
 import scala.collection.mutable.ArrayBuffer
 
 import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
 import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases
 import org.apache.spark.sql.catalyst.expressions._
 import org.apache.spark.sql.catalyst.expressions.ScalarSubquery._
@@ -63,6 +64,18 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
     Join(outerPlan, dedupSubplan, joinType, condition, JoinHint(None, subHint))
   }
 
+  private def rewriteDomainJoinsIfPresent(
+      outerPlan: LogicalPlan,
+      sub: LogicalPlan,
+      joinCond: Option[Expression]): LogicalPlan = {
+    if (SQLConf.get.decorrelateInnerQueryEnabledForExistsIn) {
+      DecorrelateInnerQuery.rewriteDomainJoins(outerPlan,
+        sub, splitConjunctivePredicates(joinCond.get))
+    } else {
+      sub
+    }
+  }
+
   private def dedupSubqueryOnSelfJoin(
       outerPlan: LogicalPlan,
       subplan: LogicalPlan,
@@ -118,16 +131,19 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
       withSubquery.foldLeft(newFilter) {
         case (p, Exists(sub, _, _, conditions, subHint)) =>
           val (joinCond, outerPlan) = rewriteExistentialExpr(conditions, p)
-          buildJoin(outerPlan, sub, LeftSemi, joinCond, subHint)
+          buildJoin(outerPlan, rewriteDomainJoinsIfPresent(outerPlan, sub, joinCond),
+            LeftSemi, joinCond, subHint)
         case (p, Not(Exists(sub, _, _, conditions, subHint))) =>
           val (joinCond, outerPlan) = rewriteExistentialExpr(conditions, p)
-          buildJoin(outerPlan, sub, LeftAnti, joinCond, subHint)
+          buildJoin(outerPlan, rewriteDomainJoinsIfPresent(outerPlan, sub, joinCond),
+            LeftAnti, joinCond, subHint)
         case (p, InSubquery(values, ListQuery(sub, _, _, _, conditions, subHint))) =>
           // Deduplicate conflicting attributes if any.
           val newSub = dedupSubqueryOnSelfJoin(p, sub, Some(values))
           val inConditions = values.zip(newSub.output).map(EqualTo.tupled)
           val (joinCond, outerPlan) = rewriteExistentialExpr(inConditions ++ conditions, p)
-          Join(outerPlan, newSub, LeftSemi, joinCond, JoinHint(None, subHint))
+          Join(outerPlan, rewriteDomainJoinsIfPresent(outerPlan, newSub, joinCond),
+            LeftSemi, joinCond, JoinHint(None, subHint))
         case (p, Not(InSubquery(values, ListQuery(sub, _, _, _, conditions, subHint)))) =>
           // This is a NULL-aware (left) anti join (NAAJ) e.g. col NOT IN expr
           // Construct the condition. A NULL in one of the conditions is regarded as a positive
@@ -153,7 +169,8 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
           // will have the final conditions in the LEFT ANTI as
           // (A.A1 = B.B1 OR ISNULL(A.A1 = B.B1)) AND (B.B2 = A.A2) AND B.B3 > 1
           val finalJoinCond = (nullAwareJoinConds ++ conditions).reduceLeft(And)
-          Join(outerPlan, newSub, LeftAnti, Option(finalJoinCond), JoinHint(None, subHint))
+          Join(outerPlan, rewriteDomainJoinsIfPresent(outerPlan, newSub, Some(finalJoinCond)),
+            LeftAnti, Option(finalJoinCond), JoinHint(None, subHint))
         case (p, predicate) =>
           val (newCond, inputPlan) = rewriteExistentialExpr(Seq(predicate), p)
           Project(p.output, Filter(newCond.get, inputPlan))
@@ -185,8 +202,10 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
         case Exists(sub, _, _, conditions, subHint) =>
           val exists = AttributeReference("exists", BooleanType, nullable = false)()
           val existenceJoin = ExistenceJoin(exists)
+          val newCondition = conditions.reduceLeftOption(And)
           newPlan =
-            buildJoin(newPlan, sub, existenceJoin, conditions.reduceLeftOption(And), subHint)
+            buildJoin(newPlan, rewriteDomainJoinsIfPresent(newPlan, sub, newCondition),
+              existenceJoin, newCondition, subHint)
           exists
         case Not(InSubquery(values, ListQuery(sub, _, _, _, conditions, subHint))) =>
           val exists = AttributeReference("exists", BooleanType, nullable = false)()
@@ -208,7 +227,9 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
           val nullAwareJoinConds = inConditions.map(c => Or(c, IsNull(c)))
           val finalJoinCond = (nullAwareJoinConds ++ conditions).reduceLeft(And)
           val joinHint = JoinHint(None, subHint)
-          newPlan = Join(newPlan, newSub, ExistenceJoin(exists), Some(finalJoinCond), joinHint)
+          newPlan = Join(newPlan,
+            rewriteDomainJoinsIfPresent(newPlan, newSub, Some(finalJoinCond)),
+            ExistenceJoin(exists), Some(finalJoinCond), joinHint)
           Not(exists)
         case InSubquery(values, ListQuery(sub, _, _, _, conditions, subHint)) =>
           val exists = AttributeReference("exists", BooleanType, nullable = false)()
@@ -217,7 +238,8 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] with PredicateHelper {
           val inConditions = values.zip(newSub.output).map(EqualTo.tupled)
           val newConditions = (inConditions ++ conditions).reduceLeftOption(And)
           val joinHint = JoinHint(None, subHint)
-          newPlan = Join(newPlan, newSub, ExistenceJoin(exists), newConditions, joinHint)
+          newPlan = Join(newPlan, rewriteDomainJoinsIfPresent(newPlan, newSub, newConditions),
+            ExistenceJoin(exists), newConditions, joinHint)
           exists
       }
     }
@@ -283,6 +305,15 @@ object PullupCorrelatedPredicates extends Rule[LogicalPlan] with PredicateHelper
         } else {
           a
         }
+
+      case l @ Limit(_, _) if predicateMap.nonEmpty =>
+        // LIMIT is not supported in PullupCorrelatedPredicates, EXISTS/IN subqueries should be
+        // handled by DecorrelateInnerQuery.
+        throw new AnalysisException(
+          errorClass =
+            "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
+          origin = l.origin,
+          messageParameters = Map("treeNode" -> l.toString))
       case p =>
         p
     }
@@ -344,10 +375,18 @@ object PullupCorrelatedPredicates extends Rule[LogicalPlan] with PredicateHelper
         ScalarSubquery(newPlan, children, exprId, getJoinCondition(newCond, conditions),
           hint, Some(mayHaveCountBug))
       case Exists(sub, children, exprId, conditions, hint) if children.nonEmpty =>
-        val (newPlan, newCond) = pullOutCorrelatedPredicates(sub, plan)
+        val (newPlan, newCond) = if (SQLConf.get.decorrelateInnerQueryEnabledForExistsIn) {
+          decorrelate(sub, plan, handleCountBug = true)
+        } else {
+          pullOutCorrelatedPredicates(sub, plan)
+        }
         Exists(newPlan, children, exprId, getJoinCondition(newCond, conditions), hint)
       case ListQuery(sub, children, exprId, numCols, conditions, hint) if children.nonEmpty =>
-        val (newPlan, newCond) = pullOutCorrelatedPredicates(sub, plan)
+        val (newPlan, newCond) = if (SQLConf.get.decorrelateInnerQueryEnabledForExistsIn) {
+          decorrelate(sub, plan, handleCountBug = true)
+        } else {
+          pullOutCorrelatedPredicates(sub, plan)
+        }
         val joinCond = getJoinCondition(newCond, conditions)
         ListQuery(newPlan, children, exprId, numCols, joinCond, hint)
       case LateralSubquery(sub, children, exprId, conditions, hint) if children.nonEmpty =>
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
index bc2be84d7be..db6cc2736c9 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
@@ -3407,6 +3407,15 @@ object SQLConf {
       .booleanConf
       .createWithDefault(true)
 
+  val DECORRELATE_EXISTS_IN_SUBQUERY_LEGACY_INCORRECT_COUNT_HANDLING_ENABLED =
+    buildConf("spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled")
+      .internal()
+      .doc("If enabled, revert to legacy incorrect behavior for certain EXISTS/IN subqueries " +
+           "with COUNT or similar aggregates.")
+      .version("4.0.0")
+      .booleanConf
+      .createWithDefault(false)
+
   val DECORRELATE_SUBQUERY_LEGACY_INCORRECT_COUNT_HANDLING_ENABLED =
     buildConf("spark.sql.optimizer.decorrelateSubqueryLegacyIncorrectCountHandling.enabled")
       .internal()
@@ -5292,6 +5301,9 @@ class SQLConf extends Serializable with Logging with SqlApiConf {
 
   def decorrelateInnerQueryEnabled: Boolean = getConf(SQLConf.DECORRELATE_INNER_QUERY_ENABLED)
 
+  def decorrelateInnerQueryEnabledForExistsIn: Boolean =
+    !getConf(SQLConf.DECORRELATE_EXISTS_IN_SUBQUERY_LEGACY_INCORRECT_COUNT_HANDLING_ENABLED)
+
   def maxConcurrentOutputFileWriters: Int = getConf(SQLConf.MAX_CONCURRENT_OUTPUT_FILE_WRITERS)
 
   def plannedWriteEnabled: Boolean = getConf(SQLConf.PLANNED_WRITE_ENABLED)
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
index 2a2b40117bb..0676d383479 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
@@ -1022,18 +1022,12 @@ class AnalysisErrorSuite extends AnalysisTest {
     assertAnalysisError(plan2, "Accessing outer query column is not allowed in" :: Nil)
 
     val plan3 = Filter(
-      Exists(Intersect(LocalRelation(b),
-        Filter(EqualTo(UnresolvedAttribute("a"), c), LocalRelation(c)), isAll = true)),
-      LocalRelation(a))
-    assertAnalysisError(plan3, "Accessing outer query column is not allowed in" :: Nil)
-
-    val plan5 = Filter(
       Exists(
         Sample(0.0, 0.5, false, 1L,
           Filter(EqualTo(UnresolvedAttribute("a"), b), LocalRelation(b))).select("b")
       ),
       LocalRelation(a))
-    assertAnalysisError(plan5,
+    assertAnalysisError(plan3,
                         "Accessing outer query column is not allowed in" :: Nil)
   }
 
@@ -1157,31 +1151,6 @@ class AnalysisErrorSuite extends AnalysisTest {
       "objectName" -> "`top.aField`",
       "proposal" -> "`top`"))
 
-  test("SPARK-35080: Unsupported correlated equality predicates in subquery") {
-    val a = AttributeReference("a", IntegerType)()
-    val b = AttributeReference("b", IntegerType)()
-    val c = AttributeReference("c", IntegerType)()
-    val d = AttributeReference("d", DoubleType)()
-    val t1 = LocalRelation(a, b, d)
-    val t2 = LocalRelation(c)
-    val conditions = Seq(
-      (abs($"a") === $"c", "abs(a#x) = outer(c#x)"),
-      (abs($"a") <=> $"c", "abs(a#x) <=> outer(c#x)"),
-      ($"a" + 1 === $"c", "(a#x + 1) = outer(c#x)"),
-      ($"a" + $"b" === $"c", "(a#x + b#x) = outer(c#x)"),
-      ($"a" + $"c" === $"b", "(a#x + outer(c#x)) = b#x"),
-      (And($"a" === $"c", Cast($"d", IntegerType) === $"c"), "CAST(d#x AS INT) = outer(c#x)"))
-    conditions.foreach { case (cond, msg) =>
-      val plan = Project(
-        Exists(
-          Aggregate(Nil, count(Literal(1)).as("cnt") :: Nil,
-            Filter(cond, t1))
-        ).as("sub") :: Nil,
-        t2)
-      assertAnalysisError(plan, s"Correlated column is not allowed in predicate: ($msg)" :: Nil)
-    }
-  }
-
   test("SPARK-35673: fail if the plan still contains UnresolvedHint after analysis") {
     val hintName = "some_random_hint_that_does_not_exist"
     val plan = UnresolvedHint(hintName, Seq.empty,
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveSubquerySuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveSubquerySuite.scala
index 120c6702475..af46c3e7b82 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveSubquerySuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveSubquerySuite.scala
@@ -17,7 +17,6 @@
 
 package org.apache.spark.sql.catalyst.analysis
 
-import org.apache.spark.sql.AnalysisException
 import org.apache.spark.sql.catalyst.dsl.expressions._
 import org.apache.spark.sql.catalyst.dsl.plans._
 import org.apache.spark.sql.catalyst.expressions.{CreateArray, Expression, GetStructField, InSubquery, LateralSubquery, ListQuery, OuterReference, ScalarSubquery}
@@ -48,19 +47,6 @@ class ResolveSubquerySuite extends AnalysisTest {
       condition: Option[Expression] = None): LateralJoin =
     LateralJoin(left, LateralSubquery(right), joinType, condition)
 
-  test("SPARK-17251 Improve `OuterReference` to be `NamedExpression`") {
-    val expr = Filter(
-      InSubquery(Seq(a), ListQuery(Project(Seq(UnresolvedAttribute("a")), t2))), t1)
-    checkError(
-      exception = intercept[AnalysisException] {
-        SimpleAnalyzer.checkAnalysis(SimpleAnalyzer.ResolveSubquery(expr))
-      },
-      errorClass = "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.CORRELATED_REFERENCE",
-      parameters = Map("sqlExprs" -> "\"a\""),
-      matchPVals = true
-    )
-  }
-
   test("SPARK-29145 Support subquery in join condition") {
     val expr = Join(t1,
       t2,
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-aggregate.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-aggregate.sql.out
index 549a87f37d3..f026a330773 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-aggregate.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-aggregate.sql.out
@@ -319,18 +319,29 @@ WHERE EXISTS(SELECT RANK() OVER (PARTITION BY hiredate ORDER BY salary) AS s
                     FROM EMP, DEPT where EMP.dept_id = DEPT.dept_id
                         AND DEPT.dept_name < BONUS.emp_name)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "(dept_name#x < outer(emp_name#x))\nFilter ((dept_id#x = dept_id#x) AND (dept_name#x < outer(emp_name#x)))\n+- Join Inner\n   :- SubqueryAlias emp\n   :  +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])\n   :     +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]\n   :        +- Project [id#x, emp_name#x, hiredat [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 34,
-    "stopIndex" : 224,
-    "fragment" : "SELECT RANK() OVER (PARTITION BY hiredate ORDER BY salary) AS s\n                    FROM EMP, DEPT where EMP.dept_id = DEPT.dept_id\n                        AND DEPT.dept_name < BONUS.emp_name"
-  } ]
-}
+Project [emp_name#x, bonus_amt#x]
++- Filter exists#x [emp_name#x]
+   :  +- Project [s#x]
+   :     +- Project [hiredate#x, salary#x, s#x, s#x]
+   :        +- Window [rank(salary#x) windowspecdefinition(hiredate#x, salary#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS s#x], [hiredate#x], [salary#x ASC NULLS FIRST]
+   :           +- Project [hiredate#x, salary#x]
+   :              +- Filter ((dept_id#x = dept_id#x) AND (dept_name#x < outer(emp_name#x)))
+   :                 +- Join Inner
+   :                    :- SubqueryAlias emp
+   :                    :  +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+   :                    :     +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+   :                    :        +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   :                    :           +- SubqueryAlias EMP
+   :                    :              +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   :                    +- SubqueryAlias dept
+   :                       +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                          +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                             +- Project [dept_id#x, dept_name#x, state#x]
+   :                                +- SubqueryAlias DEPT
+   :                                   +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias bonus
+      +- View (`BONUS`, [emp_name#x,bonus_amt#x])
+         +- Project [cast(emp_name#x as string) AS emp_name#x, cast(bonus_amt#x as double) AS bonus_amt#x]
+            +- Project [emp_name#x, bonus_amt#x]
+               +- SubqueryAlias BONUS
+                  +- LocalRelation [emp_name#x, bonus_amt#x]
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-count-bug.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-count-bug.sql.out
new file mode 100644
index 00000000000..b5e609dedd7
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-count-bug.sql.out
@@ -0,0 +1,246 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+create temporary view t1(c1, c2) as values (0, 1), (1, 2)
+-- !query analysis
+CreateViewCommand `t1`, [(c1,None), (c2,None)], values (0, 1), (1, 2), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+create temporary view t2(c1, c2) as values (0, 2), (0, 3)
+-- !query analysis
+CreateViewCommand `t2`, [(c1,None), (c2,None)], values (0, 2), (0, 3), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5)
+-- !query analysis
+CreateViewCommand `t3`, [(c1,None), (c2,None)], values (0, 3), (1, 4), (2, 5), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter exists#x [c1#x]
+   :  +- Aggregate [count(1) AS count(1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT exists#x [c1#x]
+   :  +- Aggregate [count(1) AS count(1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query analysis
+Project [c1#x, c2#x, exists#x [c1#x] AS exists(c1)#x]
+:  +- Aggregate [count(1) AS count(1)#xL]
+:     +- Filter (c1#x = outer(c1#x))
+:        +- SubqueryAlias t2
+:           +- View (`t2`, [c1#x,c2#x])
+:              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                 +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query analysis
+Project [c1#x, c2#x, NOT exists#x [c1#x] AS (NOT exists(c1))#x]
+:  +- Aggregate [count(1) AS count(1)#xL]
+:     +- Filter (c1#x = outer(c1#x))
+:        +- SubqueryAlias t2
+:           +- View (`t2`, [c1#x,c2#x])
+:              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                 +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter (exists#x [c1#x] OR NOT exists#x [c1#x])
+   :  :- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :  :  +- Filter (c1#x = outer(c1#x))
+   :  :     +- SubqueryAlias t2
+   :  :        +- View (`t2`, [c1#x,c2#x])
+   :  :           +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :              +- LocalRelation [col1#x, col2#x]
+   :  +- Aggregate [(count(1) - cast(1 as bigint)) AS (count(1) - 1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where
+ (exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists(select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ exists(select count(*) from t2 where t2.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter ((exists#x [c1#x] OR NOT exists#x [c1#x]) AND exists#x [c2#x])
+   :  :- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :  :  +- Filter (c1#x = outer(c1#x))
+   :  :     +- SubqueryAlias t2
+   :  :        +- View (`t2`, [c1#x,c2#x])
+   :  :           +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :              +- LocalRelation [col1#x, col2#x]
+   :  :- Aggregate [(count(1) - cast(1 as bigint)) AS (count(1) - 1)#xL]
+   :  :  +- Filter (c1#x = outer(c1#x))
+   :  :     +- SubqueryAlias t2
+   :  :        +- View (`t2`, [c1#x,c2#x])
+   :  :           +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :              +- LocalRelation [col1#x, col2#x]
+   :  +- Aggregate [count(1) AS count(1)#xL]
+   :     +- Filter (c1#x = outer(c2#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled,Some(true))
+
+
+-- !query
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter exists#x [c1#x]
+   :  +- Aggregate [count(1) AS count(1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT exists#x [c1#x]
+   :  +- Aggregate [count(1) AS count(1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query analysis
+Project [c1#x, c2#x, exists#x [c1#x] AS exists(c1)#x]
+:  +- Aggregate [count(1) AS count(1)#xL]
+:     +- Filter (c1#x = outer(c1#x))
+:        +- SubqueryAlias t2
+:           +- View (`t2`, [c1#x,c2#x])
+:              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                 +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query analysis
+Project [c1#x, c2#x, NOT exists#x [c1#x] AS (NOT exists(c1))#x]
+:  +- Aggregate [count(1) AS count(1)#xL]
+:     +- Filter (c1#x = outer(c1#x))
+:        +- SubqueryAlias t2
+:           +- View (`t2`, [c1#x,c2#x])
+:              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                 +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter (exists#x [c1#x] OR NOT exists#x [c1#x])
+   :  :- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :  :  +- Filter (c1#x = outer(c1#x))
+   :  :     +- SubqueryAlias t2
+   :  :        +- View (`t2`, [c1#x,c2#x])
+   :  :           +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :              +- LocalRelation [col1#x, col2#x]
+   :  +- Aggregate [(count(1) - cast(1 as bigint)) AS (count(1) - 1)#xL]
+   :     +- Filter (c1#x = outer(c1#x))
+   :        +- SubqueryAlias t2
+   :           +- View (`t2`, [c1#x,c2#x])
+   :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                 +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled,Some(false))
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
index 7d098783eba..5503098df3f 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
@@ -685,21 +685,32 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x && dept_id#x]
+   :  +- Distinct
+   :     +- Union false, false
+   :        :- Project [dept_id#x, dept_name#x, state#x]
+   :        :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :        :     +- SubqueryAlias dept
+   :        :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :        :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :        :              +- Project [dept_id#x, dept_name#x, state#x]
+   :        :                 +- SubqueryAlias DEPT
+   :        :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :        +- Project [dept_id#x, dept_name#x, state#x]
+   :           +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :              +- SubqueryAlias dept
+   :                 +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                    +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                       +- Project [dept_id#x, dept_name#x, state#x]
+   :                          +- SubqueryAlias DEPT
+   :                             +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -713,21 +724,32 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter NOT exists#x [dept_id#x && dept_id#x]
+   :  +- Distinct
+   :     +- Union false, false
+   :        :- Project [dept_id#x, dept_name#x, state#x]
+   :        :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :        :     +- SubqueryAlias dept
+   :        :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :        :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :        :              +- Project [dept_id#x, dept_name#x, state#x]
+   :        :                 +- SubqueryAlias DEPT
+   :        :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :        +- Project [dept_id#x, dept_name#x, state#x]
+   :           +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :              +- SubqueryAlias dept
+   :                 +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                    +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                       +- Project [dept_id#x, dept_name#x, state#x]
+   :                          +- SubqueryAlias DEPT
+   :                             +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -741,21 +763,31 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x && dept_id#x]
+   :  +- Intersect All true
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -769,21 +801,31 @@ WHERE EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 130,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x && dept_id#x]
+   :  +- Intersect false
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -797,21 +839,31 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x && dept_id#x]
+   :  +- Except All true
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -825,21 +877,31 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x && dept_id#x]
+   :  +- Except false
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -853,21 +915,31 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter NOT exists#x [dept_id#x && dept_id#x]
+   :  +- Intersect All true
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
 -- !query
@@ -881,18 +953,28 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept 
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter NOT exists#x [dept_id#x && dept_id#x]
+   :  +- Except false
+   :     :- Project [dept_id#x, dept_name#x, state#x]
+   :     :  +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))
+   :     :     +- SubqueryAlias dept
+   :     :        +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :     :           +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :     :              +- Project [dept_id#x, dept_name#x, state#x]
+   :     :                 +- SubqueryAlias DEPT
+   :     :                    +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   :     +- Project [dept_id#x, dept_name#x, state#x]
+   :        +- Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = TX))
+   :           +- SubqueryAlias dept
+   :              +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                 +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                    +- Project [dept_id#x, dept_name#x, state#x]
+   :                       +- SubqueryAlias DEPT
+   :                          +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-orderby-limit.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-orderby-limit.sql.out
index c688eb20257..c291c228cbe 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-orderby-limit.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/exists-subquery/exists-orderby-limit.sql.out
@@ -139,6 +139,72 @@ Sort [hiredate#x DESC NULLS LAST], true
                      +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 1)
+ORDER  BY hiredate
+-- !query analysis
+Sort [hiredate#x ASC NULLS FIRST], true
++- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   +- Filter exists#x [dept_id#x]
+      :  +- GlobalLimit 1
+      :     +- LocalLimit 1
+      :        +- Project [dept_id#x]
+      :           +- Sort [state#x ASC NULLS FIRST], true
+      :              +- Project [dept_id#x, state#x]
+      :                 +- Filter (outer(dept_id#x) = dept_id#x)
+      :                    +- SubqueryAlias dept
+      :                       +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+      :                          +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+      :                             +- Project [dept_id#x, dept_name#x, state#x]
+      :                                +- SubqueryAlias DEPT
+      :                                   +- LocalRelation [dept_id#x, dept_name#x, state#x]
+      +- SubqueryAlias emp
+         +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+            +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+               +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+                  +- SubqueryAlias EMP
+                     +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 0)
+ORDER  BY hiredate
+-- !query analysis
+Sort [hiredate#x ASC NULLS FIRST], true
++- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   +- Filter exists#x [dept_id#x]
+      :  +- GlobalLimit 0
+      :     +- LocalLimit 0
+      :        +- Project [dept_id#x]
+      :           +- Sort [state#x ASC NULLS FIRST], true
+      :              +- Project [dept_id#x, state#x]
+      :                 +- Filter (outer(dept_id#x) = dept_id#x)
+      :                    +- SubqueryAlias dept
+      :                       +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+      :                          +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+      :                             +- Project [dept_id#x, dept_name#x, state#x]
+      :                                +- SubqueryAlias DEPT
+      :                                   +- LocalRelation [dept_id#x, dept_name#x, state#x]
+      +- SubqueryAlias emp
+         +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+            +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+               +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+                  +- SubqueryAlias EMP
+                     +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT * 
 FROM   emp 
@@ -227,6 +293,72 @@ Aggregate [count(1) AS count(1)#xL]
                   +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 1)
+ORDER  BY hiredate
+-- !query analysis
+Sort [hiredate#x ASC NULLS FIRST], true
++- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   +- Filter NOT exists#x [dept_id#x]
+      :  +- GlobalLimit 1
+      :     +- LocalLimit 1
+      :        +- Project [dept_id#x]
+      :           +- Sort [state#x ASC NULLS FIRST], true
+      :              +- Project [dept_id#x, state#x]
+      :                 +- Filter (outer(dept_id#x) = dept_id#x)
+      :                    +- SubqueryAlias dept
+      :                       +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+      :                          +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+      :                             +- Project [dept_id#x, dept_name#x, state#x]
+      :                                +- SubqueryAlias DEPT
+      :                                   +- LocalRelation [dept_id#x, dept_name#x, state#x]
+      +- SubqueryAlias emp
+         +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+            +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+               +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+                  +- SubqueryAlias EMP
+                     +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
+-- !query
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 0)
+ORDER  BY hiredate
+-- !query analysis
+Sort [hiredate#x ASC NULLS FIRST], true
++- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+   +- Filter NOT exists#x [dept_id#x]
+      :  +- GlobalLimit 0
+      :     +- LocalLimit 0
+      :        +- Project [dept_id#x]
+      :           +- Sort [state#x ASC NULLS FIRST], true
+      :              +- Project [dept_id#x, state#x]
+      :                 +- Filter (outer(dept_id#x) = dept_id#x)
+      :                    +- SubqueryAlias dept
+      :                       +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+      :                          +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+      :                             +- Project [dept_id#x, dept_name#x, state#x]
+      :                                +- SubqueryAlias DEPT
+      :                                   +- LocalRelation [dept_id#x, dept_name#x, state#x]
+      +- SubqueryAlias emp
+         +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+            +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+               +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+                  +- SubqueryAlias EMP
+                     +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT * 
 FROM   emp 
@@ -339,6 +471,40 @@ Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
                   +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT emp_name
+FROM   emp
+WHERE  NOT EXISTS (SELECT max(dept.dept_id) a
+                   FROM   dept
+                   WHERE  dept.dept_id = emp.dept_id
+                   GROUP  BY state
+                   ORDER  BY state
+                   LIMIT 2
+                   OFFSET 1)
+-- !query analysis
+Project [emp_name#x]
++- Filter NOT exists#x [dept_id#x]
+   :  +- GlobalLimit 2
+   :     +- LocalLimit 2
+   :        +- Offset 1
+   :           +- Project [a#x]
+   :              +- Sort [state#x ASC NULLS FIRST], true
+   :                 +- Aggregate [state#x], [max(dept_id#x) AS a#x, state#x]
+   :                    +- Filter (dept_id#x = outer(dept_id#x))
+   :                       +- SubqueryAlias dept
+   :                          +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                             +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                                +- Project [dept_id#x, dept_name#x, state#x]
+   :                                   +- SubqueryAlias DEPT
+   :                                      +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -369,6 +535,34 @@ Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
                   +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id > emp.dept_id
+               LIMIT  1)
+-- !query analysis
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x]
+   :  +- GlobalLimit 1
+   :     +- LocalLimit 1
+   :        +- Project [dept_name#x]
+   :           +- Filter (dept_id#x > outer(dept_id#x))
+   :              +- SubqueryAlias dept
+   :                 +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                    +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                       +- Project [dept_id#x, dept_name#x, state#x]
+   :                          +- SubqueryAlias DEPT
+   :                             +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -398,6 +592,35 @@ Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
                   +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT max(dept.dept_id)
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               GROUP  BY state
+               LIMIT  1)
+-- !query analysis
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x]
+   :  +- GlobalLimit 1
+   :     +- LocalLimit 1
+   :        +- Aggregate [state#x], [max(dept_id#x) AS max(dept_id)#x]
+   :           +- Filter NOT (dept_id#x = outer(dept_id#x))
+   :              +- SubqueryAlias dept
+   :                 +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                    +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                       +- Project [dept_id#x, dept_name#x, state#x]
+   :                          +- SubqueryAlias DEPT
+   :                             +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -459,6 +682,36 @@ Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
                   +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               LIMIT  1
+               OFFSET 2)
+-- !query analysis
+Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
++- Filter exists#x [dept_id#x]
+   :  +- GlobalLimit 1
+   :     +- LocalLimit 1
+   :        +- Offset 2
+   :           +- Project [dept_name#x]
+   :              +- Filter NOT (dept_id#x = outer(dept_id#x))
+   :                 +- SubqueryAlias dept
+   :                    +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])
+   :                       +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]
+   :                          +- Project [dept_id#x, dept_name#x, state#x]
+   :                             +- SubqueryAlias DEPT
+   :                                +- LocalRelation [dept_id#x, dept_name#x, state#x]
+   +- SubqueryAlias emp
+      +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])
+         +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]
+            +- Project [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+               +- SubqueryAlias EMP
+                  +- LocalRelation [id#x, emp_name#x, hiredate#x, salary#x, dept_id#x]
+
+
 -- !query
 SELECT *
 FROM   emp
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-count-bug.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-count-bug.sql.out
new file mode 100644
index 00000000000..b553508d874
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-count-bug.sql.out
@@ -0,0 +1,378 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+create temporary view t1(c1, c2) as values (0, 1), (1, 2)
+-- !query analysis
+CreateViewCommand `t1`, [(c1,None), (c2,None)], values (0, 1), (1, 2), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+create temporary view t2(c1, c2) as values (0, 2), (0, 3)
+-- !query analysis
+CreateViewCommand `t2`, [(c1,None), (c2,None)], values (0, 2), (0, 3), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5)
+-- !query analysis
+CreateViewCommand `t3`, [(c1,None), (c2,None)], values (0, 3), (1, 4), (2, 5), false, false, LocalTempView, true
+   +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter cast(c1#x as bigint) IN (list#x [c1#x])
+   :  +- Project [(count(1) + 1)#xL]
+   :     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :        +- Filter (c1#x = outer(c1#x))
+   :           +- SubqueryAlias t2
+   :              +- View (`t2`, [c1#x,c2#x])
+   :                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query analysis
+Project [c1#x, c2#x, cast(c1#x as bigint) IN (list#x [c1#x]) AS (c1 IN (listquery(c1)))#x]
+:  +- Project [(count(1) + 1)#xL]
+:     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+:        +- Filter (c1#x = outer(c1#x))
+:           +- SubqueryAlias t2
+:              +- View (`t2`, [c1#x,c2#x])
+:                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                    +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query analysis
+Project [c1#x, c2#x, NOT cast(c1#x as bigint) IN (list#x [c1#x]) AS (NOT (c1 IN (listquery(c1))))#x]
+:  +- Project [(count(1) + 1)#xL]
+:     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+:        +- Filter (c1#x = outer(c1#x))
+:           +- SubqueryAlias t2
+:              +- View (`t2`, [c1#x,c2#x])
+:                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                    +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where
+ c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter (cast(c1#x as bigint) IN (list#x [c1#x]) OR cast(c2#x as bigint) IN (list#x [c1#x]))
+   :  :- Project [(count(1) + 1)#xL]
+   :  :  +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :  :     +- Filter (c1#x = outer(c1#x))
+   :  :        +- SubqueryAlias t2
+   :  :           +- View (`t2`, [c1#x,c2#x])
+   :  :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :                 +- LocalRelation [col1#x, col2#x]
+   :  +- Project [(count(1) - 1)#xL]
+   :     +- Aggregate [(count(1) - cast(1 as bigint)) AS (count(1) - 1)#xL]
+   :        +- Filter (c1#x = outer(c1#x))
+   :           +- SubqueryAlias t2
+   :              +- View (`t2`, [c1#x,c2#x])
+   :                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where
+ (c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ c1 NOT in (select count(*) from t2 where t2.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter ((cast(c1#x as bigint) IN (list#x [c1#x]) OR cast(c2#x as bigint) IN (list#x [c1#x])) AND NOT cast(c1#x as bigint) IN (list#x [c2#x]))
+   :  :- Project [(count(1) + 1)#xL]
+   :  :  +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :  :     +- Filter (c1#x = outer(c1#x))
+   :  :        +- SubqueryAlias t2
+   :  :           +- View (`t2`, [c1#x,c2#x])
+   :  :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :                 +- LocalRelation [col1#x, col2#x]
+   :  :- Project [(count(1) - 1)#xL]
+   :  :  +- Aggregate [(count(1) - cast(1 as bigint)) AS (count(1) - 1)#xL]
+   :  :     +- Filter (c1#x = outer(c1#x))
+   :  :        +- SubqueryAlias t2
+   :  :           +- View (`t2`, [c1#x,c2#x])
+   :  :              +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :  :                 +- LocalRelation [col1#x, col2#x]
+   :  +- Project [count(1)#xL]
+   :     +- Aggregate [count(1) AS count(1)#xL]
+   :        +- Filter (c1#x = outer(c2#x))
+   :           +- SubqueryAlias t2
+   :              +- View (`t2`, [c1#x,c2#x])
+   :                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter c1#x IN (list#x [c1#x])
+   :  +- Project [1#x]
+   :     +- Filter (count(1)#xL = cast(0 as bigint))
+   :        +- Aggregate [1 AS 1#x, count(1) AS count(1)#xL]
+   :           +- Filter (c1#x = outer(c1#x))
+   :              +- SubqueryAlias t2
+   :                 +- View (`t2`, [c1#x,c2#x])
+   :                    +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                       +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT c1#x IN (list#x [c1#x])
+   :  +- Project [1#x]
+   :     +- Filter (count(1)#xL = cast(0 as bigint))
+   :        +- Aggregate [1 AS 1#x, count(1) AS count(1)#xL]
+   :           +- Filter (c1#x = outer(c1#x))
+   :              +- SubqueryAlias t2
+   :                 +- View (`t2`, [c1#x,c2#x])
+   :                    +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                       +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter cast(c1#x as bigint) IN (list#x [])
+   :  +- Project [count(1)#xL]
+   :     +- Aggregate [count(1) AS count(1)#xL]
+   :        +- Project [c1#x, c2#x, c2#x]
+   :           +- Filter (c1#x = c2#x)
+   :              +- Project [c1#x, c2#x, c2#x, c1#x]
+   :                 +- Join Inner, (c1#x = c1#x)
+   :                    :- SubqueryAlias t1
+   :                    :  +- View (`t1`, [c1#x,c2#x])
+   :                    :     +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    :        +- LocalRelation [col1#x, col2#x]
+   :                    +- SubqueryAlias t3
+   :                       +- View (`t3`, [c1#x,c2#x])
+   :                          +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                             +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT cast(c1#x as bigint) IN (list#x [])
+   :  +- Project [(count(1) + 1)#xL]
+   :     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :        +- Project [c1#x, c2#x, c2#x]
+   :           +- Filter (c1#x = c2#x)
+   :              +- Project [c1#x, c2#x, c2#x, c1#x]
+   :                 +- Join Inner, (c1#x = c1#x)
+   :                    :- SubqueryAlias t1
+   :                    :  +- View (`t1`, [c1#x,c2#x])
+   :                    :     +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    :        +- LocalRelation [col1#x, col2#x]
+   :                    +- SubqueryAlias t3
+   :                       +- View (`t3`, [c1#x,c2#x])
+   :                          +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                             +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled,Some(true))
+
+
+-- !query
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter cast(c1#x as bigint) IN (list#x [c1#x])
+   :  +- Project [(count(1) + 1)#xL]
+   :     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :        +- Filter (c1#x = outer(c1#x))
+   :           +- SubqueryAlias t2
+   :              +- View (`t2`, [c1#x,c2#x])
+   :                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query analysis
+Project [c1#x, c2#x, cast(c1#x as bigint) IN (list#x [c1#x]) AS (c1 IN (listquery(c1)))#x]
+:  +- Project [(count(1) + 1)#xL]
+:     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+:        +- Filter (c1#x = outer(c1#x))
+:           +- SubqueryAlias t2
+:              +- View (`t2`, [c1#x,c2#x])
+:                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                    +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query analysis
+Project [c1#x, c2#x, NOT cast(c1#x as bigint) IN (list#x [c1#x]) AS (NOT (c1 IN (listquery(c1))))#x]
+:  +- Project [(count(1) + 1)#xL]
+:     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+:        +- Filter (c1#x = outer(c1#x))
+:           +- SubqueryAlias t2
+:              +- View (`t2`, [c1#x,c2#x])
+:                 +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+:                    +- LocalRelation [col1#x, col2#x]
++- SubqueryAlias t1
+   +- View (`t1`, [c1#x,c2#x])
+      +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+         +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter c1#x IN (list#x [c1#x])
+   :  +- Project [1#x]
+   :     +- Filter (count(1)#xL = cast(0 as bigint))
+   :        +- Aggregate [1 AS 1#x, count(1) AS count(1)#xL]
+   :           +- Filter (c1#x = outer(c1#x))
+   :              +- SubqueryAlias t2
+   :                 +- View (`t2`, [c1#x,c2#x])
+   :                    +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                       +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT c1#x IN (list#x [c1#x])
+   :  +- Project [1#x]
+   :     +- Filter (count(1)#xL = cast(0 as bigint))
+   :        +- Aggregate [1 AS 1#x, count(1) AS count(1)#xL]
+   :           +- Filter (c1#x = outer(c1#x))
+   :              +- SubqueryAlias t2
+   :                 +- View (`t2`, [c1#x,c2#x])
+   :                    +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                       +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter cast(c1#x as bigint) IN (list#x [])
+   :  +- Project [count(1)#xL]
+   :     +- Aggregate [count(1) AS count(1)#xL]
+   :        +- Project [c1#x, c2#x, c2#x]
+   :           +- Filter (c1#x = c2#x)
+   :              +- Project [c1#x, c2#x, c2#x, c1#x]
+   :                 +- Join Inner, (c1#x = c1#x)
+   :                    :- SubqueryAlias t1
+   :                    :  +- View (`t1`, [c1#x,c2#x])
+   :                    :     +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    :        +- LocalRelation [col1#x, col2#x]
+   :                    +- SubqueryAlias t3
+   :                       +- View (`t3`, [c1#x,c2#x])
+   :                          +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                             +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query analysis
+Project [c1#x, c2#x]
++- Filter NOT cast(c1#x as bigint) IN (list#x [])
+   :  +- Project [(count(1) + 1)#xL]
+   :     +- Aggregate [(count(1) + cast(1 as bigint)) AS (count(1) + 1)#xL]
+   :        +- Project [c1#x, c2#x, c2#x]
+   :           +- Filter (c1#x = c2#x)
+   :              +- Project [c1#x, c2#x, c2#x, c1#x]
+   :                 +- Join Inner, (c1#x = c1#x)
+   :                    :- SubqueryAlias t1
+   :                    :  +- View (`t1`, [c1#x,c2#x])
+   :                    :     +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                    :        +- LocalRelation [col1#x, col2#x]
+   :                    +- SubqueryAlias t3
+   :                       +- View (`t3`, [c1#x,c2#x])
+   :                          +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+   :                             +- LocalRelation [col1#x, col2#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [c1#x,c2#x])
+         +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x]
+            +- LocalRelation [col1#x, col2#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled,Some(false))
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-group-by.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-group-by.sql.out
index b249a75ab6d..4ac00b80414 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-group-by.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-group-by.sql.out
@@ -647,18 +647,30 @@ from t1
 where t1f IN (SELECT RANK() OVER (partition by t3c  order by t2b) as s
                              FROM t2, t3 where t2.t2c = t3.t3c and t2.t2a < t1.t1a)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "(t2a#x < outer(t1a#x))\nFilter ((t2c#x = t3c#x) AND (t2a#x < outer(t1a#x)))\n+- Join Inner\n   :- SubqueryAlias t2\n   :  +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n   :     +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 34,
-    "stopIndex" : 172,
-    "fragment" : "SELECT RANK() OVER (partition by t3c  order by t2b) as s\n                             FROM t2, t3 where t2.t2c = t3.t3c and t2.t2a < t1.t1a"
-  } ]
-}
+Project [t1a#x]
++- Filter t1f#x IN (list#x [t1a#x])
+   :  +- Project [cast(s#x as double) AS s#x]
+   :     +- Project [s#x]
+   :        +- Project [t3c#x, t2b#x, s#x, s#x]
+   :           +- Window [rank(t2b#x) windowspecdefinition(t3c#x, t2b#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS s#x], [t3c#x], [t2b#x ASC NULLS FIRST]
+   :              +- Project [t3c#x, t2b#x]
+   :                 +- Filter ((t2c#x = t3c#x) AND (t2a#x < outer(t1a#x)))
+   :                    +- Join Inner
+   :                       :- SubqueryAlias t2
+   :                       :  +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :                       :     +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :                       :        +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                       :           +- SubqueryAlias t2
+   :                       :              +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                       +- SubqueryAlias t3
+   :                          +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :                             +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                                +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                                   +- SubqueryAlias t3
+   :                                      +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-limit.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-limit.sql.out
index 4308bad2109..adfebbba394 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-limit.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-limit.sql.out
@@ -134,6 +134,37 @@ GlobalLimit 2
                         +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               LIMIT 10)
+LIMIT  2
+-- !query analysis
+GlobalLimit 2
++- LocalLimit 2
+   +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+      +- Filter t1a#x IN (list#x [t1d#xL])
+         :  +- GlobalLimit 10
+         :     +- LocalLimit 10
+         :        +- Project [t2a#x]
+         :           +- Filter (outer(t1d#xL) = t2d#xL)
+         :              +- SubqueryAlias t2
+         :                 +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+         :                    +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+         :                       +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+         :                          +- SubqueryAlias t2
+         :                             +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+         +- SubqueryAlias t1
+            +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+               +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+                  +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                     +- SubqueryAlias t1
+                        +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -201,6 +232,37 @@ GlobalLimit 1
                            +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b <= t1d
+               LIMIT  2)
+LIMIT 4
+-- !query analysis
+GlobalLimit 4
++- LocalLimit 4
+   +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+      +- Filter t1c#x IN (list#x [t1d#xL])
+         :  +- GlobalLimit 2
+         :     +- LocalLimit 2
+         :        +- Project [t2c#x]
+         :           +- Filter (cast(t2b#x as bigint) <= outer(t1d#xL))
+         :              +- SubqueryAlias t2
+         :                 +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+         :                    +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+         :                       +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+         :                          +- SubqueryAlias t2
+         :                             +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+         +- SubqueryAlias t1
+            +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+               +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+                  +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                     +- SubqueryAlias t1
+                        +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -265,6 +327,44 @@ GlobalLimit 1
                            +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d IN (SELECT t2d
+               FROM   t2
+               WHERE t2b <= t1d
+               ORDER  BY t2c, t2d
+               LIMIT 2)
+GROUP  BY t1b
+ORDER  BY t1b DESC NULLS FIRST
+LIMIT  1
+-- !query analysis
+GlobalLimit 1
++- LocalLimit 1
+   +- Sort [t1b#x DESC NULLS FIRST], true
+      +- Aggregate [t1b#x], [count(distinct t1a#x) AS count(DISTINCT t1a)#xL, t1b#x]
+         +- Filter t1d#xL IN (list#x [t1d#xL])
+            :  +- GlobalLimit 2
+            :     +- LocalLimit 2
+            :        +- Project [t2d#xL]
+            :           +- Sort [t2c#x ASC NULLS FIRST, t2d#xL ASC NULLS FIRST], true
+            :              +- Project [t2d#xL, t2c#x]
+            :                 +- Filter (cast(t2b#x as bigint) <= outer(t1d#xL))
+            :                    +- SubqueryAlias t2
+            :                       +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+            :                          +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+            :                             +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+            :                                +- SubqueryAlias t2
+            :                                   +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+            +- SubqueryAlias t1
+               +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+                  +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+                     +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                        +- SubqueryAlias t1
+                           +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -295,6 +395,41 @@ GlobalLimit 2
                            +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d > t2d
+               ORDER BY t2a DESC
+               LIMIT 3)
+LIMIT  2
+OFFSET 2
+-- !query analysis
+GlobalLimit 2
++- LocalLimit 2
+   +- Offset 2
+      +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+         +- Filter t1a#x IN (list#x [t1d#xL])
+            :  +- GlobalLimit 3
+            :     +- LocalLimit 3
+            :        +- Sort [t2a#x DESC NULLS LAST], true
+            :           +- Project [t2a#x]
+            :              +- Filter (outer(t1d#xL) > t2d#xL)
+            :                 +- SubqueryAlias t2
+            :                    +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+            :                       +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+            :                          +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+            :                             +- SubqueryAlias t2
+            :                                +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+            +- SubqueryAlias t1
+               +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+                  +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+                     +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                        +- SubqueryAlias t1
+                           +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -398,6 +533,64 @@ Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
                   +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b <= t1d
+                   LIMIT  2)
+-- !query analysis
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1b#x IN (list#x [t1d#xL])
+   :  +- GlobalLimit 2
+   :     +- LocalLimit 2
+   :        +- Project [t2b#x]
+   :           +- Filter (cast(t2b#x as bigint) <= outer(t1d#xL))
+   :              +- SubqueryAlias t2
+   :                 +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :                    +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :                       +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                          +- SubqueryAlias t2
+   :                             +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2)
+-- !query analysis
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1b#x IN (list#x [t1b#x])
+   :  +- GlobalLimit 2
+   :     +- LocalLimit 2
+   :        +- Offset 2
+   :           +- Project [t2b#x]
+   :              +- Filter (t2b#x = outer(t1b#x))
+   :                 +- SubqueryAlias t2
+   :                    +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :                       +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :                          +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                             +- SubqueryAlias t2
+   :                                +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -438,6 +631,48 @@ GlobalLimit 1
                               +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d NOT IN (SELECT t2d
+                   FROM   t2
+                   WHERE t2b > t1b
+                   ORDER  BY t2b DESC nulls first, t2d
+                   LIMIT 1
+                   OFFSET 1)
+GROUP  BY t1b
+ORDER BY t1b NULLS last
+LIMIT  1
+OFFSET 1
+-- !query analysis
+GlobalLimit 1
++- LocalLimit 1
+   +- Offset 1
+      +- Sort [t1b#x ASC NULLS LAST], true
+         +- Aggregate [t1b#x], [count(distinct t1a#x) AS count(DISTINCT t1a)#xL, t1b#x]
+            +- Filter NOT t1d#xL IN (list#x [t1b#x])
+               :  +- GlobalLimit 1
+               :     +- LocalLimit 1
+               :        +- Offset 1
+               :           +- Project [t2d#xL]
+               :              +- Sort [t2b#x DESC NULLS FIRST, t2d#xL ASC NULLS FIRST], true
+               :                 +- Project [t2d#xL, t2b#x]
+               :                    +- Filter (t2b#x > outer(t1b#x))
+               :                       +- SubqueryAlias t2
+               :                          +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+               :                             +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+               :                                +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+               :                                   +- SubqueryAlias t2
+               :                                      +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+               +- SubqueryAlias t1
+                  +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+                     +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+                        +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                           +- SubqueryAlias t1
+                              +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -465,6 +700,37 @@ Offset 2
                      +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               ORDER BY t2a
+               OFFSET 2)
+OFFSET 2
+-- !query analysis
+Offset 2
++- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+   +- Filter t1a#x IN (list#x [t1d#xL])
+      :  +- Offset 2
+      :     +- Sort [t2a#x ASC NULLS FIRST], true
+      :        +- Project [t2a#x]
+      :           +- Filter (outer(t1d#xL) = t2d#xL)
+      :              +- SubqueryAlias t2
+      :                 +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+      :                    +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+      :                       +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+      :                          +- SubqueryAlias t2
+      :                             +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+      +- SubqueryAlias t1
+         +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+            +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+               +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                  +- SubqueryAlias t1
+                     +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -494,6 +760,37 @@ Offset 4
                      +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b < t1b
+               ORDER BY t2c
+               OFFSET 2)
+OFFSET 1
+-- !query analysis
+Offset 1
++- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+   +- Filter t1c#x IN (list#x [t1b#x])
+      :  +- Offset 2
+      :     +- Sort [t2c#x ASC NULLS FIRST], true
+      :        +- Project [t2c#x]
+      :           +- Filter (t2b#x < outer(t1b#x))
+      :              +- SubqueryAlias t2
+      :                 +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+      :                    +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+      :                       +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+      :                          +- SubqueryAlias t2
+      :                             +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+      +- SubqueryAlias t1
+         +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+            +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+               +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+                  +- SubqueryAlias t1
+                     +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -555,6 +852,33 @@ Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
                   +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
+-- !query
+SELECT count(*)
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b < t1b
+                   OFFSET 2)
+-- !query analysis
+Aggregate [count(1) AS count(1)#xL]
++- Filter NOT t1b#x IN (list#x [t1b#x])
+   :  +- Offset 2
+   :     +- Project [t2b#x]
+   :        +- Filter (t2b#x < outer(t1b#x))
+   :           +- SubqueryAlias t2
+   :              +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :                 +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :                    +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                       +- SubqueryAlias t2
+   :                          +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -587,3 +911,45 @@ Offset 1
                   +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
                      +- SubqueryAlias t1
                         +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsIn.enabled = false
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsIn.enabled,Some(false))
+
+
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2)
+-- !query analysis
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1b#x IN (list#x [t1b#x])
+   :  +- GlobalLimit 2
+   :     +- LocalLimit 2
+   :        +- Offset 2
+   :           +- Project [t2b#x]
+   :              +- Filter (t2b#x = outer(t1b#x))
+   :                 +- SubqueryAlias t2
+   :                    +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :                       +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :                          +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :                             +- SubqueryAlias t2
+   :                                +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsIn.enabled = true
+-- !query analysis
+SetCommand (spark.sql.optimizer.decorrelateExistsIn.enabled,Some(true))
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-set-operations.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-set-operations.sql.out
index f088251de62..9b76a27da27 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-set-operations.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/subquery/in-subquery/in-set-operations.sql.out
@@ -1313,21 +1313,30 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Union false, false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1340,21 +1349,31 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Distinct
+   :     +- Union false, false
+   :        :- Project [t2a#x]
+   :        :  +- Filter (t2b#x = outer(t1b#x))
+   :        :     +- SubqueryAlias t2
+   :        :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :        :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :        :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :        :                 +- SubqueryAlias t2
+   :        :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :        +- Project [t3a#x]
+   :           +- SubqueryAlias t3
+   :              +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :                 +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                    +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                       +- SubqueryAlias t3
+   :                          +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1367,21 +1386,30 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Intersect All true
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1394,21 +1422,30 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Intersect false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1421,21 +1458,30 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Except All true
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1448,21 +1494,30 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter t1a#x IN (list#x [t1b#x])
+   :  +- Except false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1475,21 +1530,30 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Union false, false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1502,21 +1566,31 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Distinct
+   :     +- Union false, false
+   :        :- Project [t2a#x]
+   :        :  +- Filter (t2b#x = outer(t1b#x))
+   :        :     +- SubqueryAlias t2
+   :        :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :        :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :        :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :        :                 +- SubqueryAlias t2
+   :        :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :        +- Project [t3a#x]
+   :           +- SubqueryAlias t3
+   :              +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :                 +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                    +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                       +- SubqueryAlias t3
+   :                          +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1529,21 +1603,30 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Intersect All true
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1556,21 +1639,30 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Intersect false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1583,21 +1675,30 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Except All true
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
 
 
 -- !query
@@ -1610,18 +1711,27 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query analysis
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
++- Filter NOT t1a#x IN (list#x [t1b#x])
+   :  +- Except false
+   :     :- Project [t2a#x]
+   :     :  +- Filter (t2b#x = outer(t1b#x))
+   :     :     +- SubqueryAlias t2
+   :     :        +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])
+   :     :           +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]
+   :     :              +- Project [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     :                 +- SubqueryAlias t2
+   :     :                    +- LocalRelation [t2a#x, t2b#x, t2c#x, t2d#xL, t2e#x, t2f#x, t2g#x, t2h#x, t2i#x]
+   :     +- Project [t3a#x]
+   :        +- SubqueryAlias t3
+   :           +- View (`t3`, [t3a#x,t3b#x,t3c#x,t3d#xL,t3e#x,t3f#x,t3g#x,t3h#x,t3i#x])
+   :              +- Project [cast(t3a#x as string) AS t3a#x, cast(t3b#x as smallint) AS t3b#x, cast(t3c#x as int) AS t3c#x, cast(t3d#xL as bigint) AS t3d#xL, cast(t3e#x as float) AS t3e#x, cast(t3f#x as double) AS t3f#x, cast(t3g#x as decimal(4,0)) AS t3g#x, cast(t3h#x as timestamp) AS t3h#x, cast(t3i#x as date) AS t3i#x]
+   :                 +- Project [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   :                    +- SubqueryAlias t3
+   :                       +- LocalRelation [t3a#x, t3b#x, t3c#x, t3d#xL, t3e#x, t3f#x, t3g#x, t3h#x, t3i#x]
+   +- SubqueryAlias t1
+      +- View (`t1`, [t1a#x,t1b#x,t1c#x,t1d#xL,t1e#x,t1f#x,t1g#x,t1h#x,t1i#x])
+         +- Project [cast(t1a#x as string) AS t1a#x, cast(t1b#x as smallint) AS t1b#x, cast(t1c#x as int) AS t1c#x, cast(t1d#xL as bigint) AS t1d#xL, cast(t1e#x as float) AS t1e#x, cast(t1f#x as double) AS t1f#x, cast(t1g#x as decimal(4,0)) AS t1g#x, cast(t1h#x as timestamp) AS t1h#x, cast(t1i#x as date) AS t1i#x]
+            +- Project [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
+               +- SubqueryAlias t1
+                  +- LocalRelation [t1a#x, t1b#x, t1c#x, t1d#xL, t1e#x, t1f#x, t1g#x, t1h#x, t1i#x]
diff --git a/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-count-bug.sql b/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-count-bug.sql
new file mode 100644
index 00000000000..1af7a235683
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-count-bug.sql
@@ -0,0 +1,37 @@
+create temporary view t1(c1, c2) as values (0, 1), (1, 2);
+create temporary view t2(c1, c2) as values (0, 2), (0, 3);
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5);
+
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1);
+
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1);
+
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1;
+
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1;
+
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1);
+
+
+select * from t1 where
+ (exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists(select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ exists(select count(*) from t2 where t2.c1 = t1.c2);
+
+-- With legacy behavior flag set, some answers are not correct.
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true;
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1);
+
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1);
+
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1;
+
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1;
+
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1);
+
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false;
diff --git a/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-orderby-limit.sql b/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-orderby-limit.sql
index 8e6b49fea8f..d920a413ab8 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-orderby-limit.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/subquery/exists-subquery/exists-orderby-limit.sql
@@ -55,7 +55,25 @@ WHERE  EXISTS (SELECT dept.dept_id
                FROM   dept 
                WHERE  emp.dept_id = dept.dept_id 
                ORDER  BY state) 
-ORDER  BY hiredate DESC; 
+ORDER  BY hiredate DESC;
+
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 1)
+ORDER  BY hiredate;
+
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 0)
+ORDER  BY hiredate;
 
 -- order by with not exists 
 -- TC.01.03
@@ -85,6 +103,24 @@ WHERE  NOT EXISTS (SELECT max(dept.dept_id) a
                    GROUP  BY dept_id 
                    ORDER  BY dept_id); 
 
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 1)
+ORDER  BY hiredate;
+
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 0)
+ORDER  BY hiredate;
+
 -- limit in the exists subquery block.
 -- TC.02.01
 SELECT * 
@@ -120,7 +156,17 @@ WHERE  NOT EXISTS (SELECT max(dept.dept_id)
                    FROM   dept 
                    WHERE  dept.dept_id > 100 
                    GROUP  BY state 
-                   LIMIT  1); 
+                   LIMIT  1);
+
+SELECT emp_name
+FROM   emp
+WHERE  NOT EXISTS (SELECT max(dept.dept_id) a
+                   FROM   dept
+                   WHERE  dept.dept_id = emp.dept_id
+                   GROUP  BY state
+                   ORDER  BY state
+                   LIMIT 2
+                   OFFSET 1);
 
 -- limit and offset in the exists subquery block.
 -- TC.03.01
@@ -132,6 +178,13 @@ WHERE  EXISTS (SELECT dept.dept_name
                LIMIT  1
                OFFSET 2);
 
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id > emp.dept_id
+               LIMIT  1);
+
 -- limit and offset in the exists subquery block with aggregate.
 -- TC.03.02
 SELECT *
@@ -142,6 +195,14 @@ WHERE  EXISTS (SELECT max(dept.dept_id)
                LIMIT  1
                OFFSET 2);
 
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT max(dept.dept_id)
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               GROUP  BY state
+               LIMIT  1);
+
 -- limit and offset in the not exists subquery block.
 -- TC.03.03
 SELECT *
@@ -163,6 +224,14 @@ WHERE  NOT EXISTS (SELECT max(dept.dept_id)
                    LIMIT  1
                    OFFSET 2);
 
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               LIMIT  1
+               OFFSET 2);
+
 -- offset in the exists subquery block.
 -- TC.04.01
 SELECT *
diff --git a/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-count-bug.sql b/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-count-bug.sql
new file mode 100644
index 00000000000..4840f02511b
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-count-bug.sql
@@ -0,0 +1,50 @@
+create temporary view t1(c1, c2) as values (0, 1), (1, 2);
+create temporary view t2(c1, c2) as values (0, 2), (0, 3);
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5);
+
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1);
+
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1;
+
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1;
+
+select * from t1 where
+ c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1);
+
+select * from t1 where
+ (c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ c1 NOT in (select count(*) from t2 where t2.c1 = t1.c2);
+
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0);
+
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0);
+
+
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2);
+
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2);
+
+
+-- With legacy behavior flag set, some answers are not correct.
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true;
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1);
+
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1;
+
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1;
+
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0);
+
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0);
+
+
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2);
+
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2);
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false;
diff --git a/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-limit.sql b/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-limit.sql
index b9b062a5f7a..5f06f159f0b 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-limit.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/subquery/in-subquery/in-limit.sql
@@ -60,6 +60,16 @@ WHERE  t1a IN (SELECT t2a
                WHERE  t1d = t2d)
 LIMIT  2;
 
+-- correlated IN subquery
+-- LIMIT on both parent and subquery sides
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               LIMIT 10)
+LIMIT  2;
+
 -- TC 01.02
 SELECT *
 FROM   t1
@@ -81,6 +91,14 @@ GROUP  BY t1b
 ORDER  BY t1b DESC NULLS FIRST
 LIMIT  1;
 
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b <= t1d
+               LIMIT  2)
+LIMIT 4;
+
 -- LIMIT with NOT IN
 -- TC 01.04
 SELECT *
@@ -102,6 +120,18 @@ GROUP  BY t1b
 ORDER BY t1b NULLS last
 LIMIT  1;
 
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d IN (SELECT t2d
+               FROM   t2
+               WHERE t2b <= t1d
+               ORDER  BY t2c, t2d
+               LIMIT 2)
+GROUP  BY t1b
+ORDER  BY t1b DESC NULLS FIRST
+LIMIT  1;
+
 -- LIMIT and OFFSET in parent side
 -- TC 02.01
 SELECT *
@@ -112,6 +142,16 @@ WHERE  t1a IN (SELECT t2a
 LIMIT  2
 OFFSET 2;
 
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d > t2d
+               ORDER BY t2a DESC
+               LIMIT 3)
+LIMIT  2
+OFFSET 2;
+
 -- TC 02.02
 SELECT *
 FROM   t1
@@ -146,6 +186,21 @@ WHERE  t1b NOT IN (SELECT t2b
                    LIMIT  2
                    OFFSET 2);
 
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b <= t1d
+                   LIMIT  2);
+
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2);
+
 -- TC 02.05
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -160,6 +215,20 @@ ORDER BY t1b NULLS last
 LIMIT  1
 OFFSET 1;
 
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d NOT IN (SELECT t2d
+                   FROM   t2
+                   WHERE t2b > t1b
+                   ORDER  BY t2b DESC nulls first, t2d
+                   LIMIT 1
+                   OFFSET 1)
+GROUP  BY t1b
+ORDER BY t1b NULLS last
+LIMIT  1
+OFFSET 1;
+
 -- OFFSET in parent side
 -- TC 03.01
 SELECT *
@@ -169,6 +238,16 @@ WHERE  t1a IN (SELECT t2a
                WHERE  t1d = t2d)
 OFFSET 2;
 
+-- OFFSET in correlated subquery
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               ORDER BY t2a
+               OFFSET 2)
+OFFSET 2;
+
 -- TC 03.02
 SELECT *
 FROM   t1
@@ -178,6 +257,15 @@ WHERE  t1c IN (SELECT t2c
                OFFSET 2)
 OFFSET 4;
 
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b < t1b
+               ORDER BY t2c
+               OFFSET 2)
+OFFSET 1;
+
 -- TC 03.03
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -199,6 +287,14 @@ WHERE  t1b NOT IN (SELECT t2b
                    WHERE  t2b > 6
                    OFFSET 2);
 
+-- OFFSET with NOT IN correlated
+SELECT count(*)
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b < t1b
+                   OFFSET 2);
+
 -- TC 03.05
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -210,3 +306,16 @@ WHERE  t1d NOT IN (SELECT t2d
 GROUP  BY t1b
 ORDER BY t1b NULLS last
 OFFSET 1;
+
+
+set spark.sql.optimizer.decorrelateExistsIn.enabled = false;
+-- LIMIT is not supported in correlated IN, unless the DECORRELATE_EXISTS_AND_IN_SUBQUERIES
+-- is enabled.
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2);
+set spark.sql.optimizer.decorrelateExistsIn.enabled = true;
diff --git a/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out b/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out
index 9cf3cb8e1c3..f563324b8b7 100644
--- a/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out
@@ -758,7 +758,6 @@ SELECT * FROM t1, LATERAL (SELECT SUM(cnt) FROM (SELECT COUNT(*) cnt FROM t2 WHE
 struct<c1:int,c2:int,sum(cnt):bigint>
 -- !query output
 0	1	2
-1	2	NULL
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-aggregate.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-aggregate.sql.out
index f62efdcbad4..ef720d927d8 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-aggregate.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-aggregate.sql.out
@@ -187,20 +187,13 @@ WHERE EXISTS(SELECT RANK() OVER (PARTITION BY hiredate ORDER BY salary) AS s
                     FROM EMP, DEPT where EMP.dept_id = DEPT.dept_id
                         AND DEPT.dept_name < BONUS.emp_name)
 -- !query schema
-struct<>
+struct<emp_name:string,bonus_amt:double>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "(dept_name#x < outer(emp_name#x))\nFilter ((dept_id#x = dept_id#x) AND (dept_name#x < outer(emp_name#x)))\n+- Join Inner\n   :- SubqueryAlias emp\n   :  +- View (`EMP`, [id#x,emp_name#x,hiredate#x,salary#x,dept_id#x])\n   :     +- Project [cast(id#x as int) AS id#x, cast(emp_name#x as string) AS emp_name#x, cast(hiredate#x as date) AS hiredate#x, cast(salary#x as double) AS salary#x, cast(dept_id#x as int) AS dept_id#x]\n   :        +- Project [id#x, emp_name#x, hiredat [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 34,
-    "stopIndex" : 224,
-    "fragment" : "SELECT RANK() OVER (PARTITION BY hiredate ORDER BY salary) AS s\n                    FROM EMP, DEPT where EMP.dept_id = DEPT.dept_id\n                        AND DEPT.dept_name < BONUS.emp_name"
-  } ]
-}
+emp 1	10.0
+emp 1	20.0
+emp 2	100.0
+emp 2	300.0
+emp 3	300.0
+emp 4	100.0
+emp 5	1000.0
+emp 6 - no dept	500.0
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-count-bug.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-count-bug.sql.out
new file mode 100644
index 00000000000..2e5b31747cb
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-count-bug.sql.out
@@ -0,0 +1,142 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+create temporary view t1(c1, c2) as values (0, 1), (1, 2)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+create temporary view t2(c1, c2) as values (0, 2), (0, 3)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+
+
+
+-- !query
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query schema
+struct<c1:int,c2:int,exists(c1):boolean>
+-- !query output
+0	1	true
+1	2	true
+
+
+-- !query
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query schema
+struct<c1:int,c2:int,(NOT exists(c1)):boolean>
+-- !query output
+0	1	false
+1	2	false
+
+
+-- !query
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+select * from t1 where
+ (exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists(select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ exists(select count(*) from t2 where t2.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled	true
+
+
+-- !query
+select * from t1 where exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+select * from t1 where not exists (select count(*) from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+1	2
+
+
+-- !query
+select *, exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query schema
+struct<c1:int,c2:int,exists(c1):boolean>
+-- !query output
+0	1	true
+1	2	false
+
+
+-- !query
+select *, not exists (select count(*) from t2 where t2.c1 = t1.c1) from t1
+-- !query schema
+struct<c1:int,c2:int,(NOT exists(c1)):boolean>
+-- !query output
+0	1	false
+1	2	true
+
+
+-- !query
+select * from t1 where
+ exists(select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ not exists (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled	false
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
index a78af33accd..d030b92ed4a 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-joins-and-set-ops.sql.out
@@ -371,23 +371,12 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
+400	emp 4	2005-01-01	400.0	30
 
 
 -- !query
@@ -401,23 +390,13 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+300	emp 3	2002-01-01	300.0	20
+500	emp 5	2001-01-01	400.0	NULL
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
 
 
 -- !query
@@ -431,23 +410,9 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+
 
 
 -- !query
@@ -461,23 +426,9 @@ WHERE EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 130,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+
 
 
 -- !query
@@ -491,23 +442,11 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
 
 
 -- !query
@@ -521,23 +460,11 @@ WHERE  EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 36,
-    "stopIndex" : 131,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
 
 
 -- !query
@@ -551,23 +478,17 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
+300	emp 3	2002-01-01	300.0	20
+400	emp 4	2005-01-01	400.0	30
+500	emp 5	2001-01-01	400.0	NULL
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
 
 
 -- !query
@@ -581,20 +502,11 @@ WHERE NOT EXISTS (SELECT *
                FROM   dept 
                WHERE  dept_id = emp.dept_id and state = "TX")
 -- !query schema
-struct<>
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter ((dept_id#x = outer(dept_id#x)) AND (state#x = CA))\n+- SubqueryAlias dept\n   +- View (`DEPT`, [dept_id#x,dept_name#x,state#x])\n      +- Project [cast(dept_id#x as int) AS dept_id#x, cast(dept_name#x as string) AS dept_name#x, cast(state#x as string) AS state#x]\n         +- Project [dept_id#x, dept_name#x, state#x]\n            +- SubqueryAlias DEPT\n               +- LocalRelation [dept_id#x, dept_name#x, state#x]\n"
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 134,
-    "fragment" : "SELECT *\n               FROM   dept\n               WHERE  dept_id = emp.dept_id and state = \"CA\""
-  } ]
-}
+300	emp 3	2002-01-01	300.0	20
+400	emp 4	2005-01-01	400.0	30
+500	emp 5	2001-01-01	400.0	NULL
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-orderby-limit.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-orderby-limit.sql.out
index 9f7aa09cb21..344cf54a96d 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-orderby-limit.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/exists-subquery/exists-orderby-limit.sql.out
@@ -88,6 +88,41 @@ struct<id:int,hiredate:date>
 300	2002-01-01
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 1)
+ORDER  BY hiredate
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+300	emp 3	2002-01-01	300.0	20
+200	emp 2	2003-01-01	200.0	10
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+400	emp 4	2005-01-01	400.0	30
+800	emp 8	2016-01-01	150.0	70
+
+
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_id
+               FROM   dept
+               WHERE  emp.dept_id = dept.dept_id
+               ORDER  BY state
+               LIMIT 0)
+ORDER  BY hiredate
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+
+
+
 -- !query
 SELECT * 
 FROM   emp 
@@ -134,6 +169,46 @@ struct<count(1):bigint>
 3
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 1)
+ORDER  BY hiredate
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+500	emp 5	2001-01-01	400.0	NULL
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+
+
+-- !query
+SELECT *
+FROM   emp
+WHERE  NOT EXISTS (SELECT dept.dept_id
+                   FROM   dept
+                   WHERE  emp.dept_id = dept.dept_id
+                   ORDER  BY state
+                   LIMIT 0)
+ORDER  BY hiredate
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+500	emp 5	2001-01-01	400.0	NULL
+600	emp 6 - no dept	2001-01-01	400.0	100
+300	emp 3	2002-01-01	300.0	20
+200	emp 2	2003-01-01	200.0	10
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+400	emp 4	2005-01-01	400.0	30
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
+
+
 -- !query
 SELECT * 
 FROM   emp 
@@ -219,6 +294,27 @@ struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 800	emp 8	2016-01-01	150.0	70
 
 
+-- !query
+SELECT emp_name
+FROM   emp
+WHERE  NOT EXISTS (SELECT max(dept.dept_id) a
+                   FROM   dept
+                   WHERE  dept.dept_id = emp.dept_id
+                   GROUP  BY state
+                   ORDER  BY state
+                   LIMIT 2
+                   OFFSET 1)
+-- !query schema
+struct<emp_name:string>
+-- !query output
+emp 1
+emp 1
+emp 2
+emp 5
+emp 6 - no dept
+emp 7
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -241,6 +337,23 @@ struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 800	emp 8	2016-01-01	150.0	70
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id > emp.dept_id
+               LIMIT  1)
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
+300	emp 3	2002-01-01	300.0	20
+400	emp 4	2005-01-01	400.0	30
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -263,6 +376,27 @@ struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 800	emp 8	2016-01-01	150.0	70
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT max(dept.dept_id)
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               GROUP  BY state
+               LIMIT  1)
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
+300	emp 3	2002-01-01	300.0	20
+400	emp 4	2005-01-01	400.0	30
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
+
+
 -- !query
 SELECT *
 FROM   emp
@@ -308,6 +442,27 @@ struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
 800	emp 8	2016-01-01	150.0	70
 
 
+-- !query
+SELECT *
+FROM   emp
+WHERE  EXISTS (SELECT dept.dept_name
+               FROM   dept
+               WHERE  dept.dept_id <> emp.dept_id
+               LIMIT  1
+               OFFSET 2)
+-- !query schema
+struct<id:int,emp_name:string,hiredate:date,salary:double,dept_id:int>
+-- !query output
+100	emp 1	2005-01-01	100.0	10
+100	emp 1	2005-01-01	100.0	10
+200	emp 2	2003-01-01	200.0	10
+300	emp 3	2002-01-01	300.0	20
+400	emp 4	2005-01-01	400.0	30
+600	emp 6 - no dept	2001-01-01	400.0	100
+700	emp 7	2010-01-01	400.0	100
+800	emp 8	2016-01-01	150.0	70
+
+
 -- !query
 SELECT *
 FROM   emp
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-count-bug.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-count-bug.sql.out
new file mode 100644
index 00000000000..147f023ed8e
--- /dev/null
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-count-bug.sql.out
@@ -0,0 +1,182 @@
+-- Automatically generated by SQLQueryTestSuite
+-- !query
+create temporary view t1(c1, c2) as values (0, 1), (1, 2)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+create temporary view t2(c1, c2) as values (0, 2), (0, 3)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+create temporary view t3(c1, c2) as values (0, 3), (1, 4), (2, 5)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+1	2
+
+
+-- !query
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query schema
+struct<c1:int,c2:int,(c1 IN (listquery(c1))):boolean>
+-- !query output
+0	1	false
+1	2	true
+
+
+-- !query
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query schema
+struct<c1:int,c2:int,(NOT (c1 IN (listquery(c1)))):boolean>
+-- !query output
+0	1	true
+1	2	false
+
+
+-- !query
+select * from t1 where
+ c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+select * from t1 where
+ (c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1) OR
+ c2 in (select count(*) - 1 from t2 where t2.c1 = t1.c1)) AND
+ c1 NOT in (select count(*) from t2 where t2.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+1	2
+
+
+-- !query
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+1	2
+
+
+-- !query
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = true
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled	true
+
+
+-- !query
+select * from t1 where c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+
+
+
+-- !query
+select *, c1 in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query schema
+struct<c1:int,c2:int,(c1 IN (listquery(c1))):boolean>
+-- !query output
+0	1	false
+1	2	false
+
+
+-- !query
+select *, c1 not in (select count(*) + 1 from t2 where t2.c1 = t1.c1)
+from t1
+-- !query schema
+struct<c1:int,c2:int,(NOT (c1 IN (listquery(c1)))):boolean>
+-- !query output
+0	1	true
+1	2	true
+
+
+-- !query
+select * from t1 where c1 in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+
+
+
+-- !query
+select * from t1 where c1 not in (select 1 from t2 where t2.c1 = t1.c1 having count(*) = 0)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+1	2
+
+
+-- !query
+select * from t1 where c1 in (select count(*) from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+select * from t1 where c1 not in (select count(*) + 1 from t1 join t3 using (c1) where t3.c1 = t1.c2)
+-- !query schema
+struct<c1:int,c2:int>
+-- !query output
+0	1
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled = false
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsSubqueryLegacyIncorrectCountHandling.enabled	false
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-group-by.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-group-by.sql.out
index b2456891f92..7d85d40b14e 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-group-by.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-group-by.sql.out
@@ -360,20 +360,11 @@ from t1
 where t1f IN (SELECT RANK() OVER (partition by t3c  order by t2b) as s
                              FROM t2, t3 where t2.t2c = t3.t3c and t2.t2a < t1.t1a)
 -- !query schema
-struct<>
+struct<t1a:string>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "(t2a#x < outer(t1a#x))\nFilter ((t2c#x = t3c#x) AND (t2a#x < outer(t1a#x)))\n+- Join Inner\n   :- SubqueryAlias t2\n   :  +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n   :     +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 34,
-    "stopIndex" : 172,
-    "fragment" : "SELECT RANK() OVER (partition by t3c  order by t2b) as s\n                             FROM t2, t3 where t2.t2c = t3.t3c and t2.t2a < t1.t1a"
-  } ]
-}
+t1d
+t1d
+t1d
+t1e
+t1e
+t1e
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-limit.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-limit.sql.out
index c1186c9db53..e688c44629e 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-limit.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-limit.sql.out
@@ -77,6 +77,21 @@ val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               LIMIT 10)
+LIMIT  2
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -111,6 +126,21 @@ struct<count(DISTINCT t1a):bigint,t1b:smallint>
 1	NULL
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b <= t1d
+               LIMIT  2)
+LIMIT 4
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -144,6 +174,24 @@ struct<count(DISTINCT t1a):bigint,t1b:smallint>
 1	6
 
 
+-- !query
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d IN (SELECT t2d
+               FROM   t2
+               WHERE t2b <= t1d
+               ORDER  BY t2c, t2d
+               LIMIT 2)
+GROUP  BY t1b
+ORDER  BY t1b DESC NULLS FIRST
+LIMIT  1
+-- !query schema
+struct<count(DISTINCT t1a):bigint,t1b:smallint>
+-- !query output
+1	NULL
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -159,6 +207,22 @@ val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d > t2d
+               ORDER BY t2a DESC
+               LIMIT 3)
+LIMIT  2
+OFFSET 2
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -215,6 +279,41 @@ val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
 val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b <= t1d
+                   LIMIT  2)
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+
+
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2)
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -234,6 +333,26 @@ struct<count(DISTINCT t1a):bigint,t1b:smallint>
 2	8
 
 
+-- !query
+SELECT Count(DISTINCT( t1a )),
+       t1b
+FROM   t1
+WHERE  t1d NOT IN (SELECT t2d
+                   FROM   t2
+                   WHERE t2b > t1b
+                   ORDER  BY t2b DESC nulls first, t2d
+                   LIMIT 1
+                   OFFSET 1)
+GROUP  BY t1b
+ORDER BY t1b NULLS last
+LIMIT  1
+OFFSET 1
+-- !query schema
+struct<count(DISTINCT t1a):bigint,t1b:smallint>
+-- !query output
+2	10
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -248,6 +367,22 @@ val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1a IN (SELECT t2a
+               FROM   t2
+               WHERE  t1d = t2d
+               ORDER BY t2a
+               OFFSET 2)
+OFFSET 2
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+
+
 -- !query
 SELECT *
 FROM   t1
@@ -263,6 +398,21 @@ val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
 val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
 
 
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1c IN (SELECT t2c
+               FROM   t2
+               WHERE  t2b < t1b
+               ORDER BY t2c
+               OFFSET 2)
+OFFSET 1
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -297,6 +447,19 @@ val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
 val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
 
 
+-- !query
+SELECT count(*)
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b < t1b
+                   OFFSET 2)
+-- !query schema
+struct<count(1):bigint>
+-- !query output
+12
+
+
 -- !query
 SELECT Count(DISTINCT( t1a )),
        t1b
@@ -314,3 +477,38 @@ struct<count(DISTINCT t1a):bigint,t1b:smallint>
 2	10
 1	16
 1	NULL
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsIn.enabled = false
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsIn.enabled	false
+
+
+-- !query
+SELECT *
+FROM   t1
+WHERE  t1b NOT IN (SELECT t2b
+                   FROM   t2
+                   WHERE  t2b = t1b
+                   LIMIT  2
+                   OFFSET 2)
+-- !query schema
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
+-- !query output
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+
+
+-- !query
+set spark.sql.optimizer.decorrelateExistsIn.enabled = true
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.optimizer.decorrelateExistsIn.enabled	true
diff --git a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-set-operations.sql.out b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-set-operations.sql.out
index 1d3cdf51719..13e1d8b5625 100644
--- a/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-set-operations.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/subquery/in-subquery/in-set-operations.sql.out
@@ -602,23 +602,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 
 
 -- !query
@@ -631,23 +617,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 
 
 -- !query
@@ -660,23 +632,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 
 
 -- !query
@@ -689,23 +647,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
 
 
 -- !query
@@ -718,23 +662,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+
 
 
 -- !query
@@ -747,23 +677,9 @@ WHERE  t1a IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 35,
-    "stopIndex" : 100,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+
 
 
 -- !query
@@ -776,23 +692,19 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
 -- !query
@@ -805,23 +717,19 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
 -- !query
@@ -834,23 +742,19 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
 -- !query
@@ -863,23 +767,19 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
 -- !query
@@ -892,23 +792,20 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
 
 
 -- !query
@@ -921,20 +818,17 @@ WHERE  t1a NOT IN (SELECT t2a
                SELECT t3a
                FROM   t3)
 -- !query schema
-struct<>
+struct<t1a:string,t1b:smallint,t1c:int,t1d:bigint,t1e:float,t1f:double,t1g:decimal(4,0),t1h:timestamp,t1i:date>
 -- !query output
-org.apache.spark.sql.catalyst.ExtendedAnalysisException
-{
-  "errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.ACCESSING_OUTER_QUERY_COLUMN_IS_NOT_ALLOWED",
-  "sqlState" : "0A000",
-  "messageParameters" : {
-    "treeNode" : "Filter (t2b#x = outer(t1b#x))\n+- SubqueryAlias t2\n   +- View (`t2`, [t2a#x,t2b#x,t2c#x,t2d#xL,t2e#x,t2f#x,t2g#x,t2h#x,t2i#x])\n      +- Project [cast(t2a#x as string) AS t2a#x, cast(t2b#x as smallint) AS t2b#x, cast(t2c#x as int) AS t2c#x, cast(t2d#xL as bigint) AS t2d#xL, cast(t2e#x as float) AS t2e#x, cast(t2f#x as double) AS t2f#x, cast(t2g#x as decimal(4,0)) AS t2g#x, cast(t2h#x as timestamp) AS t2h#x, cast(t2i#x as date) AS t2i#x]\n         +- Project [t2a#x, t2b [...]
-  },
-  "queryContext" : [ {
-    "objectType" : "",
-    "objectName" : "",
-    "startIndex" : 39,
-    "stopIndex" : 104,
-    "fragment" : "SELECT t2a\n               FROM   t2\n               WHERE t2b = t1b"
-  } ]
-}
+val1a	16	12	10	15.0	20.0	2000	2014-07-04 01:01:00	2014-07-04
+val1a	16	12	21	15.0	20.0	2000	2014-06-04 01:02:00.001	2014-06-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:00:00	2014-04-04
+val1a	6	8	10	15.0	20.0	2000	2014-04-04 01:02:00.001	2014-04-04
+val1b	8	16	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1c	8	16	19	17.0	25.0	2600	2014-05-04 01:02:00.001	2014-05-05
+val1d	10	NULL	12	17.0	25.0	2600	2015-05-04 01:01:00	2015-05-04
+val1d	NULL	16	19	17.0	25.0	2600	2014-07-04 01:02:00.001	NULL
+val1d	NULL	16	22	17.0	25.0	2600	2014-06-04 01:01:00	NULL
+val1e	10	NULL	19	17.0	25.0	2600	2014-05-04 01:01:00	2014-05-04
+val1e	10	NULL	19	17.0	25.0	2600	2014-09-04 01:02:00.001	2014-09-04
+val1e	10	NULL	25	17.0	25.0	2600	2014-08-04 01:01:00	2014-08-04
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
index 17f20dbcdd3..37fea85fcd2 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
@@ -767,32 +767,30 @@ class SubquerySuite extends QueryTest
       Seq((2, 1)).toDF("c1", "c2").createOrReplaceTempView("t3")
       Seq((1, 1), (2, 2)).toDF("c1", "c2").createOrReplaceTempView("t4")
 
-      // Simplest case
-      val exception1 = intercept[AnalysisException] {
+      checkAnswer(
         sql(
           """
             | select t1.c1
             | from   t1
             | where  t1.c1 in (select max(t2.c1)
             |                  from   t2
-            |                  where  t1.c2 >= t2.c2)""".stripMargin).collect()
-      }
-      checkErrorMatchPVals(
-        exception1,
-        errorClass = "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY." +
-          "CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-        parameters = Map("treeNode" -> "(?s).*"),
-        sqlState = None,
-        context = ExpectedContext(
-          fragment =
-            """select max(t2.c1)
-              |                  from   t2
-              |                  where  t1.c2 >= t2.c2""".stripMargin,
-          start = 44,
-          stop = 128))
+            |                  where  t1.c2 >= t2.c2)""".stripMargin),
+        Nil)
+
+      // Same but with a COUNT aggregate
+      checkAnswer(
+        sql(
+          """
+            | select t1.c1
+            | from   t1
+            | where  t1.c1 in (select count(t2.c1)
+            |                  from   t2
+            |                  where  t1.c2 <= t2.c2)""".stripMargin),
+        Row(1) :: Nil)
+
 
       // Add a HAVING on top and augmented within an OR predicate
-      val exception2 = intercept[AnalysisException] {
+      checkAnswer(
         sql(
           """
             | select t1.c1
@@ -801,25 +799,10 @@ class SubquerySuite extends QueryTest
             |                  from   t2
             |                  where  t1.c2 >= t2.c2
             |                  having count(*) > 0 )
-            |         or t1.c2 >= 0""".stripMargin).collect()
-      }
-      checkErrorMatchPVals(
-        exception2,
-        errorClass = "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY." +
-          "CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-        parameters = Map("treeNode" -> "(?s).*"),
-        sqlState = None,
-        context = ExpectedContext(
-          fragment =
-            """select max(t2.c1)
-              |                  from   t2
-              |                  where  t1.c2 >= t2.c2
-              |                  having count(*) > 0""".stripMargin,
-          start = 44,
-          stop = 166))
+            |         or t1.c2 >= 0""".stripMargin),
+        Row(1) :: Nil)
 
-      // Add a HAVING on top and augmented within an OR predicate
-      val exception3 = intercept[AnalysisException] {
+      checkAnswer(
         sql(
           """
             | select t1.c1
@@ -829,47 +812,18 @@ class SubquerySuite extends QueryTest
             |                   from   t2
             |                   where  t1.c2 = t2.c2
             |                          or t3.c2 = t2.c2)
-            |        )""".stripMargin).collect()
-      }
-      checkErrorMatchPVals(
-        exception3,
-        errorClass = "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY." +
-          "CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-        parameters = Map("treeNode" -> "(?s).*"),
-        sqlState = None,
-        context = ExpectedContext(
-          fragment =
-            """select max(t2.c1)
-              |                   from   t2
-              |                   where  t1.c2 = t2.c2
-              |                          or t3.c2 = t2.c2""".stripMargin,
-          start = 77,
-          stop = 205))
-
-      // In Window expression: changing the data set to
-      // demonstrate if this query ran, it would return incorrect result.
-      val exception4 = intercept[AnalysisException] {
+            |        )""".stripMargin),
+        Row(1) :: Nil)
+
+      checkAnswer(
         sql(
           """
-          | select c1
-          | from   t3
-          | where  c1 in (select max(t4.c1) over ()
-          |               from   t4
-          |               where t3.c2 >= t4.c2)""".stripMargin).collect()
-      }
-      checkErrorMatchPVals(
-        exception4,
-        errorClass = "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY." +
-          "CORRELATED_COLUMN_IS_NOT_ALLOWED_IN_PREDICATE",
-        parameters = Map("treeNode" -> "(?s).*"),
-        sqlState = None,
-        context = ExpectedContext(
-          fragment =
-            """select max(t4.c1) over ()
-              |               from   t4
-              |               where t3.c2 >= t4.c2""".stripMargin,
-          start = 38,
-          stop = 123))
+            | select c1
+            | from   t3
+            | where  c1 in (select max(t4.c1) over ()
+            |               from   t4
+            |               where t3.c2 <= t4.c2)""".stripMargin),
+        Row(2) :: Nil)
     }
   }
   // This restriction applies to


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org