Skip to content

Commit

Permalink
[CARMEL-2691] Support % as well in the pattern spec for show like tab…
Browse files Browse the repository at this point in the history
…le 'xxx' (#6)
  • Loading branch information
wangyum authored and mingmwang committed Jul 28, 2020
1 parent 3e23e3d commit 1930e50
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ class Analyzer(
withPosition(u) {
q.resolveChildren(nameParts, resolver)
.orElse(resolveLiteralFunction(nameParts, u, q))
.orElse(resolveReferencedAlias(nameParts, u, q))
.getOrElse(u)
}
logDebug(s"Resolving $u to $result")
Expand Down Expand Up @@ -1605,6 +1606,25 @@ class Analyzer(
func.map(wrapper)
}

private def resolveReferencedAlias(
nameParts: Seq[String],
attribute: UnresolvedAttribute,
plan: LogicalPlan): Option[Expression] = {
def resolveAlias(exps: Seq[NamedExpression]) = {
val alias = exps
.filter(a => a.resolved && a.isInstanceOf[Alias] && resolver(a.name, nameParts.head))
if (alias.size > 1) {
throw new AnalysisException(s"Found duplicate alias when resolving '${nameParts.head}'")
}
alias.headOption
}
plan match {
case Aggregate(groups, aggs, _) if !groups.contains(attribute) => resolveAlias(aggs)
case Project(projectList, _) => resolveAlias(projectList)
case _ => None
}
}

/**
* Resolves the attribute, column value and extract value expressions(s) by traversing the
* input expression in bottom-up manner. In order to resolve the nested complex type fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,41 @@ class AnalysisSuite extends AnalysisTest with Matchers {
}
}
}

test("Resolve referenced alias") {
val a = testRelation.output.head
checkAnalysis(
testRelation.select((a + 1).as("a_alias"), ($"a_alias" + 1).as("a_alias_ref")),
testRelation.select((a + 1).as("a_alias"), (a + 1 + 1).as("a_alias_ref")))
checkAnalysis(
testRelation.groupBy(a.as("a1"))(
(min(a) + 1).as("min_a_alias"), ($"min_a_alias" + 1).as("min_a_alias_ref")),
testRelation.groupBy(a)(
(min(a) + 1).as("min_a_alias"), ((min(a) + 1) + 1).as("min_a_alias_ref")))
checkAnalysis(
testRelation.select((a + 1).as("a_alias"),
CaseWhen(Seq((Literal(true), Symbol("a_alias").attr)), Symbol("a")).as("a_alias_ref")),
testRelation.select((a + 1).as("a_alias"),
CaseWhen(Seq((Literal(true), a + 1)), a).as("a_alias_ref")))

val inputPlan = testRelation.select((a + 1).as("a_alias"), $"A_ALIAS" + 1)
Seq(false, true).foreach { caseSensitive =>
if (caseSensitive) {
assertAnalysisError(inputPlan, "cannot resolve '`A_ALIAS`'" :: Nil, caseSensitive)
} else {
assertAnalysisSuccess(inputPlan, caseSensitive)
}
}

assertAnalysisError(
testRelation.select(a.as("a_alias"), a.as("a_alias"), $"a_alias" + 1),
"Found duplicate alias when resolving 'a_alias'" :: Nil)

assertAnalysisError(
testRelation.select((a + 1).as("a_alias"), $"non_exist_alias" + 1),
"cannot resolve '`non_exist_alias`'" :: Nil)
assertAnalysisError(
testRelation.groupBy(a.as("a1"))((min(a) + 1).as("min_a_alias"), $"non_exist_alias" + 1),
"cannot resolve '`non_exist_alias`'" :: Nil)
}
}

0 comments on commit 1930e50

Please sign in to comment.