Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,13 @@ class SQLBuilder(logicalPlan: LogicalPlan) extends Logging {
ScalarSubquery(rewrite, Seq.empty, exprId)

case PredicateSubquery(query, conditions, false, exprId) =>
val plan = Project(Seq(Alias(Literal(1), "1")()),
Filter(conditions.reduce(And), addSubqueryIfNeeded(query)))
val subquery = addSubqueryIfNeeded(query)
val plan = if (conditions.isEmpty) {
subquery
} else {
Project(Seq(Alias(Literal(1), "1")()),
Filter(conditions.reduce(And), subquery))
}
Exists(plan, exprId)

case PredicateSubquery(query, conditions, true, exprId) =>
Expand Down
4 changes: 4 additions & 0 deletions sql/hive/src/test/resources/sqlgen/predicate_subquery.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is automatically generated by LogicalPlanToSQLSuite.
select * from t1 b where exists (select * from t1 a)
--------------------------------------------------------------------------------
SELECT `gen_attr` AS `a` FROM (SELECT `gen_attr` FROM (SELECT `a` AS `gen_attr` FROM `default`.`t1`) AS gen_subquery_0 WHERE EXISTS(SELECT `gen_attr` AS `a` FROM ((SELECT `gen_attr` FROM (SELECT `a` AS `gen_attr` FROM `default`.`t1`) AS gen_subquery_0) AS gen_subquery_1) AS gen_subquery_1)) AS b
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.util.control.NonFatal
import org.apache.spark.sql.Column
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SQLTestUtils

/**
Expand Down Expand Up @@ -927,6 +928,15 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils {
}
}

test("predicate subquery") {
withTable("t1") {
withSQLConf(SQLConf.CROSS_JOINS_ENABLED.key -> "true") {
sql("CREATE TABLE t1(a int)")
checkSQL("select * from t1 b where exists (select * from t1 a)", "predicate_subquery")
}
}
}

test("SPARK-14933 - select orc table") {
withTable("orc_t") {
sql("create table orc_t stored as orc as select 1 as c1, 'abc' as c2")
Expand Down