Skip to content
Snippets Groups Projects
Commit 0f09f022 authored by gatorsmile's avatar gatorsmile Committed by Cheng Lian
Browse files

[SPARK-13205][SQL] SQL Generation Support for Self Join

This PR addresses two issues:
  - Self join does not work in SQL Generation
  - When creating new instances for `LogicalRelation`, `metastoreTableIdentifier` is lost.

liancheng Could you please review the code changes? Thank you!

Author: gatorsmile <gatorsmile@gmail.com>

Closes #11084 from gatorsmile/selfJoinInSQLGen.
parent 663cc400
No related branches found
No related tags found
No related merge requests found
...@@ -76,7 +76,11 @@ case class LogicalRelation( ...@@ -76,7 +76,11 @@ case class LogicalRelation(
/** Used to lookup original attribute capitalization */ /** Used to lookup original attribute capitalization */
val attributeMap: AttributeMap[AttributeReference] = AttributeMap(output.map(o => (o, o))) val attributeMap: AttributeMap[AttributeReference] = AttributeMap(output.map(o => (o, o)))
def newInstance(): this.type = LogicalRelation(relation).asInstanceOf[this.type] def newInstance(): this.type =
LogicalRelation(
relation,
expectedOutputAttributes,
metastoreTableIdentifier).asInstanceOf[this.type]
override def simpleString: String = s"Relation[${output.mkString(",")}] $relation" override def simpleString: String = s"Relation[${output.mkString(",")}] $relation"
} }
...@@ -142,7 +142,15 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi ...@@ -142,7 +142,15 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
Some(s"`$database`.`$table`") Some(s"`$database`.`$table`")
case Subquery(alias, child) => case Subquery(alias, child) =>
toSQL(child).map(childSQL => s"($childSQL) AS $alias") toSQL(child).map( childSQL =>
child match {
// Parentheses is not used for persisted data source relations
// e.g., select x.c1 from (t1) as x inner join (t1) as y on x.c1 = y.c1
case Subquery(_, _: LogicalRelation | _: MetastoreRelation) =>
s"$childSQL AS $alias"
case _ =>
s"($childSQL) AS $alias"
})
case Join(left, right, joinType, condition) => case Join(left, right, joinType, condition) =>
for { for {
......
...@@ -104,6 +104,14 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils { ...@@ -104,6 +104,14 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils {
checkHiveQl("SELECT id FROM t0 UNION ALL SELECT CAST(id AS INT) AS id FROM t0") checkHiveQl("SELECT id FROM t0 UNION ALL SELECT CAST(id AS INT) AS id FROM t0")
} }
test("self join") {
checkHiveQl("SELECT x.key FROM t1 x JOIN t1 y ON x.key = y.key")
}
test("self join with group by") {
checkHiveQl("SELECT x.key, COUNT(*) FROM t1 x JOIN t1 y ON x.key = y.key group by x.key")
}
test("three-child union") { test("three-child union") {
checkHiveQl("SELECT id FROM t0 UNION ALL SELECT id FROM t0 UNION ALL SELECT id FROM t0") checkHiveQl("SELECT id FROM t0 UNION ALL SELECT id FROM t0 UNION ALL SELECT id FROM t0")
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment