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 @@ -457,7 +457,7 @@ object FoldablePropagation extends Rule[LogicalPlan] {
// join is not always picked from its children, but can also be null.
// TODO(cloud-fan): It seems more reasonable to use new attributes as the output attributes
// of outer join.
case j @ Join(_, _, Inner, _) =>
case j @ Join(_, _, Inner, _) if !stop =>
j.transformExpressions(replaceFoldable)

// We can fold the projections an expand holds. However expand changes the output columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ class FoldablePropagationSuite extends PlanTest {
comparePlans(optimized, correctAnswer)
}

test("Propagate in inner join") {
val ta = testRelation.select('a, Literal(1).as('tag))
.union(testRelation.select('a, Literal(2).as('tag)))
.subquery('ta)
val tb = testRelation.select('a, Literal(1).as('tag))
.union(testRelation.select('a, Literal(2).as('tag)))
.subquery('tb)
val query = ta.join(tb, Inner,
Some("ta.a".attr === "tb.a".attr && "ta.tag".attr === "tb.tag".attr))
val optimized = Optimize.execute(query.analyze)
val correctAnswer = query.analyze
comparePlans(optimized, correctAnswer)
}

test("Propagate in expand") {
val c1 = Literal(1).as('a)
val c2 = Literal(2).as('b)
Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/inner-join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TEMPORARY VIEW t1 AS SELECT * FROM VALUES (1) AS GROUPING(a);
CREATE TEMPORARY VIEW t2 AS SELECT * FROM VALUES (1) AS GROUPING(a);
CREATE TEMPORARY VIEW t3 AS SELECT * FROM VALUES (1), (1) AS GROUPING(a);
CREATE TEMPORARY VIEW t4 AS SELECT * FROM VALUES (1), (1) AS GROUPING(a);

CREATE TEMPORARY VIEW ta AS
SELECT a, 'a' AS tag FROM t1
UNION ALL
SELECT a, 'b' AS tag FROM t2;

CREATE TEMPORARY VIEW tb AS
SELECT a, 'a' AS tag FROM t3
UNION ALL
SELECT a, 'b' AS tag FROM t4;

-- SPARK-19766 Constant alias columns in INNER JOIN should not be folded by FoldablePropagation rule
SELECT tb.* FROM ta INNER JOIN tb ON ta.a = tb.a AND ta.tag = tb.tag;
68 changes: 68 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/inner-join.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 13
Copy link
Member

Choose a reason for hiding this comment

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

Actually, this number is wrong. Next time, please do not manually change this file. You should run the command to generate the file. @stanzhai

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!
I will pay attention to this next time.



-- !query 0
CREATE TEMPORARY VIEW t1 AS SELECT * FROM VALUES (1) AS GROUPING(a)
-- !query 0 schema
struct<>
-- !query 0 output



-- !query 1
CREATE TEMPORARY VIEW t2 AS SELECT * FROM VALUES (1) AS GROUPING(a)
-- !query 1 schema
struct<>
-- !query 1 output



-- !query 2
CREATE TEMPORARY VIEW t3 AS SELECT * FROM VALUES (1), (1) AS GROUPING(a)
-- !query 2 schema
struct<>
-- !query 2 output



-- !query 3
CREATE TEMPORARY VIEW t4 AS SELECT * FROM VALUES (1), (1) AS GROUPING(a)
-- !query 3 schema
struct<>
-- !query 3 output



-- !query 4
CREATE TEMPORARY VIEW ta AS
SELECT a, 'a' AS tag FROM t1
UNION ALL
SELECT a, 'b' AS tag FROM t2
-- !query 4 schema
struct<>
-- !query 4 output



-- !query 5
CREATE TEMPORARY VIEW tb AS
SELECT a, 'a' AS tag FROM t3
UNION ALL
SELECT a, 'b' AS tag FROM t4
-- !query 5 schema
struct<>
-- !query 5 output



-- !query 6
SELECT tb.* FROM ta INNER JOIN tb ON ta.a = tb.a AND ta.tag = tb.tag
-- !query 6 schema
struct<a:int,tag:string>
-- !query 6 output
1 a
1 a
1 b
1 b