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 @@ -2320,6 +2320,26 @@ regularBody[boolean topLevel]
)
|
selectStatement[topLevel]
|
(LPAREN selectStatement0[true]) => nestedSetOpSelectStatement[topLevel]
;

nestedSetOpSelectStatement[boolean topLevel]
:
(
LPAREN s=selectStatement0[topLevel] RPAREN -> {$s.tree}
)
(set=setOpSelectStatement[$nestedSetOpSelectStatement.tree, topLevel])
Copy link
Contributor

Choose a reason for hiding this comment

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

Discussion: This works but is quite had to follow (passing the matched tree to the next rule). Isn't there a more simple & concise approach possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've tried few approaches unsuccessfully. I will try if there is more simple approach tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made a little change to support recursively nested UNION. I also updated the test. But it is basically the same approach.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it might be the simplest approach to support recursively nested UNION query.

-> {set == null}?
{$nestedSetOpSelectStatement.tree}
-> {$set.tree}
;

selectStatement0[boolean topLevel]
:
(selectStatement[true]) => selectStatement[topLevel]
|
(nestedSetOpSelectStatement[true]) => nestedSetOpSelectStatement[topLevel]
;

selectStatement[boolean topLevel]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,70 @@ class CatalystQlSuite extends PlanTest {
"from windowData")
}

test("nesting UNION") {
val parsed = parser.parsePlan(
"""
|SELECT `u_1`.`id` FROM (((SELECT `t0`.`id` FROM `default`.`t0`)
|UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) UNION ALL
|(SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
""".stripMargin)

val expected = Project(
UnresolvedAlias(UnresolvedAttribute("u_1.id"), None) :: Nil,
Subquery("u_1",
Union(
Union(
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None)),
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None)))))

comparePlans(parsed, expected)

val parsedSame = parser.parsePlan(
"""
|SELECT `u_1`.`id` FROM ((SELECT `t0`.`id` FROM `default`.`t0`)
|UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`) UNION ALL
|(SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
""".stripMargin)

comparePlans(parsedSame, expected)

val parsed2 = parser.parsePlan(
Copy link
Member Author

Choose a reason for hiding this comment

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

Recursively nested UNION.

"""
|SELECT `u_1`.`id` FROM ((((SELECT `t0`.`id` FROM `default`.`t0`)
|UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) UNION ALL
|(SELECT `t0`.`id` FROM `default`.`t0`))
|UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
""".stripMargin)

val expected2 = Project(
UnresolvedAlias(UnresolvedAttribute("u_1.id"), None) :: Nil,
Subquery("u_1",
Union(
Union(
Union(
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None)),
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
Project(
UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
UnresolvedRelation(TableIdentifier("t0", Some("default")), None)))))

comparePlans(parsed2, expected2)
}

test("subquery") {
parser.parsePlan("select (select max(b) from s) ss from t")
parser.parsePlan("select * from t where a = (select b from s)")
Expand Down