Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

airframe-sql: Fix SQL generation for JOIN #2748

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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 @@ -122,22 +122,7 @@ object SQLGenerator extends LogSupport {

findNonEmpty(nonFilterChild).map { f =>
b += "FROM"
f match {
case _: Selection =>
b += s"(${printRelation(f)})"
case _: SetOperation =>
b += s"(${printRelation(f)})"
case _: Limit =>
b += s"(${printRelation(f)})"
case _: Filter =>
b += s"(${printRelation(f)})"
case _: Sort =>
b += s"(${printRelation(f)})"
case _: Distinct =>
b += s"(${printRelation(f)})"
case _ =>
b += printRelation(f)
}
b += printRelationWithParenthesesIfNecessary(f)
}

val filterSet = s match {
Expand Down Expand Up @@ -227,12 +212,8 @@ object SQLGenerator extends LogSupport {
case _ => s"(${r}) AS ${alias.sqlExpr}${c}"
}
case Join(joinType, left, right, cond, _) =>
val l = printRelation(left)
val r = right match {
case _: Selection => s"(${printRelation(right)})"
case _: SetOperation => s"(${printRelation(right)})"
case _ => printRelation(right)
}
val l = printRelationWithParenthesesIfNecessary(left)
val r = printRelationWithParenthesesIfNecessary(right)
val c = cond match {
case NaturalJoin(_) => ""
case JoinUsing(columns, _) => s" USING (${columns.map(_.sqlExpr).mkString(", ")})"
Expand Down Expand Up @@ -276,6 +257,18 @@ object SQLGenerator extends LogSupport {
}
}

def printRelationWithParenthesesIfNecessary(r: Relation): String = {
r match {
case _: Selection => s"(${printRelation(r)})"
case _: SetOperation => s"(${printRelation(r)})"
case _: Limit => s"(${printRelation(r)})"
case _: Filter => s"(${printRelation(r)})"
case _: Sort => s"(${printRelation(r)})"
case _: Distinct => s"(${printRelation(r)})"
case _ => printRelation(r)
}
}

def printDDL(e: DDL): String = {
e match {
case CreateSchema(name, ifNotExists, propsOpt, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,12 @@ class SQLGeneratorTest extends AirSpec {
val sql = SQLGenerator.print(resolvedPlan).toLowerCase
sql shouldBe "with t1 as (select id as xid from (select id as pid from a)) select xid from t1"
}

test("join with sub query at left side") {
val resolvedPlan =
SQLAnalyzer.analyze("select * from (select * from A) inner join A using (id)", "default", demoCatalog)

val sql = SQLGenerator.print(resolvedPlan).toLowerCase
sql shouldBe "select * from (select * from a) join a using (id)"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fix, the following invalid SQL is generated:

select * from select * from a join a using (id)

}
}