[SPARK-7595] [SQL] Window will cause resolve failed with self join
for example:
table: src(key string, value string)
sql: with v1 as(select key, count(value) over (partition by key) cnt_val from src), v2 as(select v1.key, v1_lag.cnt_val from v1, v1 v1_lag where v1.key = v1_lag.key) select * from v2 limit 5;
then will analyze fail when resolving conflicting references in Join:
'Limit 5
'Project [*]
'Subquery v2
'Project ['v1.key,'v1_lag.cnt_val]
'Filter ('v1.key = 'v1_lag.key)
'Join Inner, None
Subquery v1
Project [key#95,cnt_val#94L]
Window [key#95,value#96], [HiveWindowFunction#org.apache.hadoop.hive.ql.udf.generic.GenericUDAFCount(value#96) WindowSpecDefinition [key#95], [], ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS cnt_val#94L], WindowSpecDefinition [key#95], [], ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
Project [key#95,value#96]
MetastoreRelation default, src, None
Subquery v1_lag
Subquery v1
Project [key#97,cnt_val#94L]
Window [key#97,value#98], [HiveWindowFunction#org.apache.hadoop.hive.ql.udf.generic.GenericUDAFCount(value#98) WindowSpecDefinition [key#97], [], ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS cnt_val#94L], WindowSpecDefinition [key#97], [], ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
Project [key#97,value#98]
MetastoreRelation default, src, None
Conflicting attributes: cnt_val#94L
Author: linweizhong <linweizhong@huawei.com>
Closes #6114 from Sephiroth-Lin/spark-7595 and squashes the following commits:
f8f2637 [linweizhong] Add unit test
dfe9169 [linweizhong] Handle windowExpression with self join
(cherry picked from commit 13e652b61a
)
Signed-off-by: Michael Armbrust <michael@databricks.com>
This commit is contained in:
parent
9ab4db29ff
commit
c80e0cff25
|
@ -322,6 +322,11 @@ class Analyzer(
|
|||
case oldVersion @ Aggregate(_, aggregateExpressions, _)
|
||||
if findAliases(aggregateExpressions).intersect(conflictingAttributes).nonEmpty =>
|
||||
(oldVersion, oldVersion.copy(aggregateExpressions = newAliases(aggregateExpressions)))
|
||||
|
||||
case oldVersion @ Window(_, windowExpressions, _, child)
|
||||
if AttributeSet(windowExpressions.map(_.toAttribute)).intersect(conflictingAttributes)
|
||||
.nonEmpty =>
|
||||
(oldVersion, oldVersion.copy(windowExpressions = newAliases(windowExpressions)))
|
||||
}.headOption.getOrElse { // Only handle first case, others will be fixed on the next pass.
|
||||
sys.error(
|
||||
s"""
|
||||
|
|
|
@ -763,4 +763,14 @@ class SQLQuerySuite extends QueryTest {
|
|||
sql("SELECT CASE k WHEN 2 THEN 22 WHEN 4 THEN 44 ELSE 0 END, v FROM t"),
|
||||
Row(0, "1") :: Row(22, "2") :: Row(0, "3") :: Row(44, "4") :: Row(0, "5") :: Nil)
|
||||
}
|
||||
|
||||
test("SPARK-7595: Window will cause resolve failed with self join") {
|
||||
checkAnswer(sql(
|
||||
"""
|
||||
|with
|
||||
| v1 as (select key, count(value) over (partition by key) cnt_val from src),
|
||||
| v2 as (select v1.key, v1_lag.cnt_val from v1, v1 v1_lag where v1.key = v1_lag.key)
|
||||
| select * from v2 order by key limit 1
|
||||
""".stripMargin), Row(0, 3))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue