Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
04959c2
refactor analyzer adding a new object
anchovYu Nov 23, 2022
6f44c85
lca code
anchovYu Nov 23, 2022
725e5ac
add tests, refine logic
anchovYu Nov 28, 2022
660e1d2
move lca rule to a new file
anchovYu Nov 28, 2022
fd06094
rename conf
anchovYu Nov 28, 2022
7d4f80f
test failure
anchovYu Nov 29, 2022
b9704d5
small fix
anchovYu Nov 29, 2022
777f13a
temp commit, still in implementation
anchovYu Nov 29, 2022
09480ea
a temporary solution, but still fail certain cases
anchovYu Nov 30, 2022
c972738
working solution, needs some refinement
anchovYu Dec 1, 2022
97ee293
Merge remote-tracking branch 'apache/master' into SPARK-27561-refactor
anchovYu Dec 1, 2022
5785943
make changes to accomodate the recent refactor
anchovYu Dec 2, 2022
757cffb
introduce leaf exp in Project as well
anchovYu Dec 5, 2022
29de892
handle a corner case
anchovYu Dec 5, 2022
72991c6
add more tests; add check rule
anchovYu Dec 6, 2022
d45fe31
uplift the necessity to resolve expression in second phase; add more …
anchovYu Dec 8, 2022
1f55f73
address comments to add tests for LCA off
anchovYu Dec 8, 2022
f753529
revert the refactor, split LCA into two rules
anchovYu Dec 9, 2022
b9f706f
better refactor
anchovYu Dec 9, 2022
94d5c9e
address comments
anchovYu Dec 9, 2022
d2e75fd
Merge branch 'SPARK-27561-refactor' into SPARK-27561-agg
anchovYu Dec 9, 2022
edde37c
basic version passing all tests
anchovYu Dec 9, 2022
fb7b18c
update the logic, add and refactor tests
anchovYu Dec 12, 2022
3698cff
update comments
anchovYu Dec 13, 2022
e700d6a
add a corner case comment
anchovYu Dec 13, 2022
8d20986
address comments
anchovYu Dec 13, 2022
d952aa7
Merge branch 'SPARK-27561-refactor' into SPARK-27561-agg
anchovYu Dec 13, 2022
44d5a3d
Merge remote-tracking branch 'apache/master' into SPARK-27561-agg
anchovYu Dec 13, 2022
ccebc1c
revert some changes
anchovYu Dec 13, 2022
5540b70
fix few todos
anchovYu Dec 13, 2022
338ba11
Merge remote-tracking branch 'apache/master' into SPARK-27561-agg
anchovYu Dec 16, 2022
136a930
fix the failing test
anchovYu Dec 16, 2022
5076ad2
fix the missing_aggregate issue, turn on conf to see failed tests
anchovYu Dec 19, 2022
2f2dee5
remove few todos
anchovYu Dec 19, 2022
3a5509a
better fix to maintain aggregate error: only lift up in certain cases
anchovYu Dec 20, 2022
a23debb
Merge remote-tracking branch 'apache/master' into SPARK-27561-agg
anchovYu Dec 20, 2022
b200da0
typo
anchovYu Dec 20, 2022
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
Prev Previous commit
Next Next commit
add tests, refine logic
(cherry picked from commit 313b2c9)
  • Loading branch information
anchovYu committed Nov 28, 2022
commit 725e5ac9df65438f87f2c260ea5507aaf1a1bd2b
Original file line number Diff line number Diff line change
Expand Up @@ -1558,20 +1558,20 @@ class Analyzer(override val catalogManager: CatalogManager)
* resolved by other rules
* - in Aggregate TODO.
*
* For Project, it rewrites the Project plan by inserting a newly created Project plan between
* the original Project and its child, and updating the project list of the original Project plan.
* The project list of the new Project plan is the lateral column aliases that are referenced
* in the original project list. These aliases in the original project list are updated to
* attribute references.
* For Project, it rewrites by inserting a newly created Project plan between the original Project
* and its child, pushing the referenced lateral column aliases to this new Project, and updating
* the project list of the original Project.
*
* Before rewrite:
* Project [age AS a, a + 1]
* Project [age AS a, 'a + 1]
* +- Child
*
* After rewrite:
* Project [a, a + 1]
* +- Project [age AS a]
* Project [a, 'a + 1]
* +- Project [child output, age AS a]
* +- Child
*
* For Aggregate TODO.
*/
object ResolveLateralColumnAlias extends Rule[LogicalPlan] {
private case class AliasEntry(alias: Alias, index: Int)
Expand All @@ -1581,14 +1581,14 @@ class Analyzer(override val catalogManager: CatalogManager)
case p @ Project(projectList, child) if p.childrenResolved
&& !ResolveReferences.containsStar(projectList)
&& projectList.exists(_.containsPattern(UNRESOLVED_ATTRIBUTE)) =>
// TODO: delta

var aliasMap = CaseInsensitiveMap(Map[String, Seq[AliasEntry]]())
var referencedAliases = Seq[AliasEntry]()
def updateAliasMap(a: Alias, idx: Int): Unit = {
def insertIntoAliasMap(a: Alias, idx: Int): Unit = {
val prevAliases = aliasMap.getOrElse(a.name, Seq.empty[AliasEntry])
aliasMap += (a.name -> (prevAliases :+ AliasEntry(a, idx)))
}
def searchMatchedLCA(e: Expression): Unit = {
def lookUpLCA(e: Expression): Option[AliasEntry] = {
var matchedLCA: Option[AliasEntry] = None
e.transformWithPruning(_.containsPattern(UNRESOLVED_ATTRIBUTE)) {
case u: UnresolvedAttribute if aliasMap.contains(u.nameParts.head) &&
resolveExpressionByPlanChildren(u, p).isInstanceOf[UnresolvedAttribute] =>
Expand All @@ -1600,13 +1600,15 @@ class Analyzer(override val catalogManager: CatalogManager)
val referencedAlias = aliases.head
// Only resolved alias can be the lateral column alias
if (referencedAlias.alias.resolved) {
referencedAliases :+= referencedAlias
matchedLCA = Some(referencedAlias)
}
}
u
}
matchedLCA
}
projectList.zipWithIndex.foreach {

val referencedAliases = projectList.zipWithIndex.flatMap {
case (a: Alias, idx) =>
// Add all alias to the aliasMap. But note only resolved alias can be LCA and pushed
// down. Unresolved alias is added to the map to perform the ambiguous name check.
Expand All @@ -1615,13 +1617,13 @@ class Analyzer(override val catalogManager: CatalogManager)
// only 1 AS a is pushed down, even though 1 AS a, 'a + 1 AS b and 'b + 1 AS c are
// all added to the aliasMap. On the second round, when 'a + 1 AS b is resolved,
// it is pushed down.
searchMatchedLCA(a)
updateAliasMap(a, idx)
val matchedLCA = lookUpLCA(a)
insertIntoAliasMap(a, idx)
matchedLCA
case (e, _) =>
searchMatchedLCA(e)
}
lookUpLCA(e)
}.toSet

referencedAliases = referencedAliases.sortBy(_.index)
if (referencedAliases.isEmpty) {
p
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql

import org.scalactic.source.Position
import org.scalatest.Tag

import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession

class LateralColumnAliasSuite extends QueryTest with SharedSparkSession {
protected val testTable: String = "employee"

override def beforeAll(): Unit = {
super.beforeAll()
sql(s"CREATE TABLE $testTable (dept INTEGER, name String, salary INTEGER, bonus INTEGER) " +
s"using orc")
sql(
s"""
|INSERT INTO $testTable VALUES
| (1, 'amy', 10000, 1000),
| (2, 'alex', 12000, 1200),
| (1, 'cathy', 9000, 1200),
| (2, 'david', 10000, 1300),
| (6, 'jen', 12000, 1200)
|""".stripMargin)
}

override def afterAll(): Unit = {
try {
sql(s"DROP TABLE IF EXISTS $testTable")
} finally {
super.afterAll()
}
}

val lcaEnabled: Boolean = true
override protected def test(testName: String, testTags: Tag*)(testFun: => Any)
(implicit pos: Position): Unit = {
super.test(testName, testTags: _*) {
withSQLConf(SQLConf.LATERAL_COLUMN_ALIAS_ENABLED.key -> lcaEnabled.toString) {
testFun
}
}
}

test("Lateral alias in project") {
checkAnswer(sql(s"select dept as d, d + 1 as e from $testTable where name = 'amy'"),
Row(1, 2))

checkAnswer(
sql(
s"select salary * 2 as new_salary, new_salary + bonus from $testTable where name = 'amy'"),
Row(20000, 21000))
checkAnswer(
sql(
s"select salary * 2 as new_salary, new_salary + bonus * 2 as new_income from $testTable" +
s" where name = 'amy'"),
Row(20000, 22000))

checkAnswer(
sql(
"select salary * 2 as new_salary, (new_salary + bonus) * 3 - new_salary * 2 as " +
s"new_income from $testTable where name = 'amy'"),
Row(20000, 23000))

// When the lateral alias conflicts with the table column, it should resolved as the table
// column
checkAnswer(
sql(
"select salary * 2 as salary, salary * 2 + bonus as " +
s"new_income from $testTable where name = 'amy'"),
Row(20000, 21000))

checkAnswer(
sql(
"select salary * 2 as salary, (salary + bonus) * 3 - (salary + bonus) as " +
s"new_income from $testTable where name = 'amy'"),
Row(20000, 22000))

checkAnswer(
sql(
"select salary * 2 as salary, (salary + bonus) * 2 as bonus, " +
s"salary + bonus as prev_income, prev_income + bonus + salary from $testTable" +
" where name = 'amy'"),
Row(20000, 22000, 11000, 22000))

// Corner cases for resolution order
checkAnswer(
sql(s"SELECT salary * 1.5 AS d, d, 10000 AS d FROM $testTable WHERE name = 'jen'"),
Row(18000, 18000, 10000)
)
}
}